Incomprehensible Error with my loops

So basically I have a project in class that requires me to use Arduino. It was a simple program using only two electromagnetic switches and an infrared sensor. So all of them were digital Input, I had to use the 3 of them to start a motor.
So there’s no problem over there, my program was simple just use the infrared sensor + one of the switch to start the motor and when the other one gets active it stops the motor. All was good with that, it was working but I had to change my digital infrared sensor by an analogic one. And that’s where the problem came, using the same program but changing slightly my if() it started to act strangely.

#define IRS A3
#define ILSL 2
#define ILSH 3
#define LED 5
float voltage;
float cc=5;
   
   void setup()
   {
       Serial.begin(9600);
       pinMode(IRS,INPUT);
       pinMode(ILSL, INPUT);
       pinMode(ILSH, INPUT);
       pinMode(LED, OUTPUT);
   }

void loop()

   {
      voltage = getVoltage();
      Serial.println(voltage);                  
      
    if((voltage>=1.5) && (digitalRead(ILSH)==1))
      do 
       {
         digitalWrite(LED,HIGH);
       } 
      while (digitalRead(ILSL) == 0);
    else digitalWrite(LED,LOW);
    
    if((voltage>=1.5) && (digitalRead(ILSL)==1))
      do 
       {
         digitalWrite(LED,HIGH);
       }  
      while (digitalRead(ILSH) == 0);
    else digitalWrite(LED,LOW);
   }


float getVoltage()
   {
       int sensor_value;
       int sum;  
       for (int i = 0;i < 20;i ++)
       {
           sensor_value = analogRead(IRS);
           sum += sensor_value;
       }
       sensor_value = sum / 20;
       
       voltage = (float)sensor_value*cc/1024;
       return voltage;
   } 
[code]

This program is a test on a led, ILSH and ILSL are the two switches and IRS the Infrared sensor. Basically when I try the second if() with my ILSL and IRS it puts the led on, then when I press ILSH it put it off. For the first if() with ILSH and IRS it puts the led on, but to turn off the led I have to press ILSL then ILSH to turn it off.I can't understand where's the error apart if it's a problem with the if else in a row.
Sorry for the English not my main language but I find it better to ask in it.

Your logic is flawed, try this:

void loop()
{
  voltage = getVoltage();
  Serial.println(voltage);     
  if((voltage>=1.5) && (digitalRead(ILSH)==1)
      digitalWrite(LED,HIGH);
   else digitalWrite(LED,LOW);
}

I need the motor (led in this case) to stay on until thE other ILS gets activate, isn't your code just turning it on then stopping it when I release the IRS or the ILS ?

void loop()
{
  voltage = getVoltage();
  Serial.println(voltage);     
  if((voltage>=1.5) && (digitalRead(ILSH)==1 && (digitalRead(ILSL)==0)
      digitalWrite(LED,HIGH);
   else digitalWrite(LED,LOW);
}

If the above does not work, post the code that worked without the analog input.

Do you really need to know the voltage? 1.5 V = ADC value of 307 (with 5V AREF):

if((analogRead(IRS)>=307) && (digitalRead(ILSH)==1)

avr_fred:

void loop()

{
  voltage = getVoltage();
  Serial.println(voltage);   
  if((voltage>=1.5) && (digitalRead(ILSH)==1 && (digitalRead(ILSL)==0)
      digitalWrite(LED,HIGH);
  else digitalWrite(LED,LOW);
}




If the above does not work, post the code that worked without the analog input.

Well, guess it saves some lines but still gets the problem that when I do the first if() to turn of the led I need to press one ILS then the other one.
Also basically my code with digital inputs replace the "voltage >=1.5" by a simple digitalRead()

outsider:
Do you really need to know the voltage? 1.5 V = ADC value of 307 (with 5V AREF):

if((analogRead(IRS)>=307) && (digitalRead(ILSH)==1)

I just had a curve (maybe not the good word, sorry for the English ^^") showing how much volts I get for how much centimeters of detection, the 1.5V are just between 5 and 17cm in that case

KurwaClown:
Well, guess it saves some lines but still gets the problem that when I do the first if() to turn of the led I need to press one ILS then the other one.
Also basically my code with digital inputs replace the "voltage >=1.5" by a simple digitalRead()

Nevermind error of my parts, the code like this doesn't work, it just lights on the led when the conditions are met, so the led is just on when you have the 3 condition and stop when one of them changes. So i can't keep the led up until and only until the ILS I want is activate

KurwaClown:
So i can't keep the led up until and only until the ILS I want is activate

is that a problem? If so, you might have to explain better.

And if you updated your code, please post the updated version (in a new reply).

sterretje:
is that a problem? If so, you might have to explain better.

And if you updated your code, please post the updated version (in a new reply).

Basically, I need if the ILSH is activated and the infrared sensor at less than 17cm to turn the led on, to turn off the led I need to press the the other ILS, so the ILSL. Same goes for the opposite when I start with ILSL I need to turn off the led with the ILSH one. And that's where the problem is, it works when I press the if whom start with ILSL but with the other one to turn off the led I need to press one ILS then another.

so, are you are looking to make a state change when a certain distance criteria is met...

why don't I see any variable called distance?

BulldogLowell:
so, are you are looking to make a state change when a certain distance criteria is met...

why don't I see any variable called distance?

I just typed the current needed in the if, don't want to add something for the distance since I know 1.5V is what I need.

KurwaClown:
I just typed the current needed in the if, don't want to add something for the distance since I know 1.5V is what I need.

But then, converting the ADC to a float is also not needed, as pointed out. floats are a particularly bad way to use comparisons for limit testing, by the way.

if you use for example centimeters instead of voltage, you may also eliminate the error that may come from ADC readings that are within the bounds of one centimeter unit.

to help solve your fundamental issue, refer to the StateChangeDetection example in the IDE.