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()
...
for sensor in active_sensors:loop inside yourwhileloop?TrueandFalseinstead of1and0; it's easier to understand. Also, usingif sensor1_actif:is equivalent to usingif sensor1_actif == True:.sensor1read()andsensor2read()? What makes them different? What do you do withxandy?const activeSensors = ['sensor1', 'sensor4']; activeSensors.forEach((sensor) => read (sensor));