3

I'm trying to find a way to optimize my code. For the moment I have a while loop in which several sensors are read out. But in the beginning of my code I first check what sensors that needs to be read out and I simply add an if function before every part of code. Is there a better way of doing this?

sensor1_actif=1
sensor2_actif=0

while True:
    if(sensor1_actif==1):
        x=sensor1read()
    
    if(sensor2_actif==1):
        y=sensor2read()

    ...
6
  • 1
    Maybe create a list of active sensors, and have a for sensor in active_sensors: loop inside your while loop? Commented Aug 11, 2022 at 17:19
  • 1
    You can use True and False instead of 1 and 0; it's easier to understand. Also, using if sensor1_actif: is equivalent to using if sensor1_actif == True:. Commented Aug 11, 2022 at 17:21
  • What are sensor1read() and sensor2read()? What makes them different? What do you do with x and y? Commented Aug 11, 2022 at 17:26
  • The sensors are analog values, so they have a different conversion thats why the different functions. De x and y are stored in an array and send to a database at the end of every loop Commented Aug 11, 2022 at 19:00
  • I don't know python very well so I will not try to give you a code snippet that might not work, but you simply need to do something similar to the below JS code, const activeSensors = ['sensor1', 'sensor4']; activeSensors.forEach((sensor) => read (sensor)); Commented Aug 11, 2022 at 20:14

2 Answers 2

2

You can use a for loop:

funcs = [sensor1read, sensor2read, ...] # Store the functions in order
while True:
    actifs = [sensor1_actif, sensor2_actif, ...] # Store the variables in order
    sensor_readings = [None for _ in range(len(funcs))]
    for i, (func, actif) in enumerate(zip(funcs, actifs)): # Iterate through
        if actif: # Equivalent to if actif != 0
            sensor_readings[i] = func()

This stores the outputs in a list. sensor_readings[0] will be the output of the first function, sensor_readings[1] the output of the second function, etc. If the function did not get called it defaults to None.

Sign up to request clarification or add additional context in comments.

1 Comment

OP is assigning the return of sensor1read etc to variables: so an equivalent solution needs to somehow do that too. Maybe a list sensor_readings?
0

Perhaps best to do this is with a control dictionary. Something like this:

def sensor1read():
    pass

def sensor2read():
    pass

CONTROL = {'sensor_1': {'active': True, 'func': sensor1read, 'val': None}, 'sensor_2': {'active': True, 'func': sensor2read, 'val': None}}

def read_sensors():
    for v in CONTROL.values():
        if v['active']:
            v['val'] = v['func']()

In this way you just set the 'active' value accordingly then this single function will update all values for all active sensors

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.