Blinking LED With IR sensors

Hi Guys!

I just want to have your suggestions.
Im Using arduino ESP8266 nodemcu esp8266.

i was trying to blink the on-board LED using 2 IR sensors with 3 statements.

1.) When 1st sensor is High and 2nd sensor is LOW, Blink LED by 100ms,
2.) When 1st sensor is LOW and 2nd sensor is HIGH, Blink LED by 2000ms,
3.) When 1st sensor is HIGH and 2nd sensor is HIGH, Blink LED by 500ms,

Now my problem is when Statement 2 (which have 2 seconds delay blinking)is active then I suddenly change to 1st statement, the Code is still finishing for the 2nd statement before it change to 1st statement.

What I want is everytime I change states I want to run the code immediately. You could say it is like an emergency switch. everything stops to run the code of statement ive choosen.

CODE is below:

int read1 = 13;
int read2 = 15;

void setup() {
  Serial.begin(115200);
  pinMode(read1,INPUT);
  pinMode(read2,INPUT);

  pinMode(2, OUTPUT);
}

void loop() 
{
  while(digitalRead(read1) && digitalRead(read2))
   {
    digitalWrite(2,LOW);
    delay(500);
    digitalWrite(2,HIGH);
    delay(500);       
   }
   
if (digitalRead(read1))
  {
 digitalWrite(2,LOW);
 delay(100);
 digitalWrite(2,HIGH);
 delay(100);
  }
else if(digitalRead(read2))
   {
    digitalWrite(2,LOW);
   delay(2000);
    digitalWrite(2,HIGH);
    delay(2000);
   }
}

Please tell me if my explanation is not good enough.

Thanks.

What I want is everytime I change states I want to run the code immediately.

So you don't want to use delay at all.

You want to implement your code as a stare machine.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

Grumpy_Mike:
So you don't want to use delay at all.

You want to implement your code as a stare machine.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Thanks for the response,

I want to explain further.

I want the code to be interrupted immediately as soon as I activate the sensor.
It is like when the machine is running and I press emergency button, the machine should stop immediately
without finishing the entire loop of code.

because what is happening now is when the program is in 2nd state which has 2 seconds delay and then I tried to activate to 1st state which blinks really fast, the program finishes the 2 seconds delay first then run the code for the 1st state. what I want to do is even when i activate to 1st state it should interrupt the 2nd state and run the code immediately of the 1st state.

Thanks! Please Help cause it really bugs me.

I want to explain further.

Yes I understood all that from the start.

However, the answer is still exactly the same as I explained in reply #1.

Have you read those links?

because what is happening now is when the program is in 2nd state which has 2 seconds delay

That is why I said you MUST NOT use the delay function in your code.