How to use "while" correctly???

Hello,

Can anyone tell me why my code didn't work if I insert "while".
Without "while" everything works perfect, but I need delay until second sensor gets trigged...

I use two IR sensors for motion and relay for stairs lighting.
If first IR1 trigged, need to wait until second IR2 will be trigged...
If first IR2 trigged, need to wait until second IR1 will be trigged...

All delay time needs to be depended from sensors.

P.S. Sorry for my English:-)

int val1=0;
int val2=0;
int relay=8;

void setup()
{ 
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
}

void loop()
{
  val1=analogRead(A0);
  val2=analogRead(A1);
  Serial.println("IR1:");
  Serial.println(val1);
  Serial.println("IR2:");
  Serial.println(val2);

if (val1>=200 && val2<=200)
      //while (val2<=200)
      digitalWrite(relay, HIGH);
  else if (val2>=200 && val1<=200)
       //while (val1<=200)
        digitalWrite(relay, HIGH);
    else if (val2>=200 && val1>=200)
        //while (val2<200 && val1<200)
          digitalWrite(relay, HIGH);        
      else 
            digitalWrite(relay, LOW);
}

2vnt_IR.ino (618 Bytes)

You need to 1) analogRead during the while loop and 2) encapsulate your if--else statements with {}:

if (val1>=200 && val2<=200) {
  while (analogRead(A1) <= 200) ; //Wait
  digitalWrite(relay, HIGH);
} else if (val2>=200 && val1<=200)...

forgive me for hijacking this post. Ive never actually used a forum before, much less this one. I have a while loop that the micro and simulator both decide to ignore. I have a screenshot but how do i post it along with code? any help would be greatly appreciated. thank you!

forgive me for hijacking this post.

You obviously know you shouldn’t do this.

So no, you are not forgiven, very rude.

Start your own thread.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]