Help in Invoking an instantaneous response

Hi EveryOne. I just picked my Arduino and started to learn how to use it. I wrote this traffic light simulator with two modes.

[u]INFO:[/u] [u]Mode one[/u]

Flash the following sequence with a 100ms delay R Y G.

[u]Mode two[/u]

a normal traffic light simulation. Red on for 3 seconds>> Yellow on for 1/2 seconds>> Green on for 5 seconds>> Repeat.

To switch between modes I am using one pin as a digitalRead pin to determine the mode to run.

[u]PROBLEM:[/u]

If the Arduino is running in mode 2, traffic light simulator, and I trun on the switch to switch to mode 1, there is going to be a delay before it start to run mode 1. The entire sequence will finish fist and only after that will it run mode 1. I understand why this is happening. Arduino is executing the code line by line and does this in a loop. Since my if statement is at the start of the code, it would need to finish the simulation before it knows that it is time to switch to mode 1. Is there away to make the Arduino respond instantaneous when i press the button?

My code:

/* Traffic Light Simulator Red light on for 3 seconds Yellow light on for .5 seconds Green light on for 5 seconds */

int r=9,y=7,g=4; int m=11; int val; void setup(){ pinMode(m,INPUT); pinMode(r,OUTPUT); // Setting up the pins as outputs pinMode(g,OUTPUT); // Setting up the pins as outputs pinMode(y,OUTPUT); // Setting up the pins as outputs }

void loop (){ val = digitalRead( m ); if (val==LOW){

digitalWrite(r,HIGH); //Turn on Red Light delay(100); digitalWrite(r,LOW); //Turn off Red Light digitalWrite(y,HIGH); //Turn on Yellow Light delay(100); digitalWrite(y,LOW); //Turn off Yellow Light digitalWrite(g,HIGH); //Turn on Green Light delay(100); digitalWrite(g,LOW); //Turn off Green Light

} else{ digitalWrite(r,HIGH); //Turn on Red Light delay(3000); digitalWrite(r,LOW); //Turn off Red Light digitalWrite(y,HIGH); //Turn on Yellow Light delay(500); digitalWrite(y,LOW); //Turn off Yellow Light digitalWrite(g,HIGH); //Turn on Green Light delay(5000); digitalWrite(g,LOW); //Turn off Green Light } }

Is there away to make the Arduino respond instantaneous when i press the button?

Yes there is. As you found out, all those delays are blocking commands and therefore effects the cycle time of the main loop function. If you look at the arduino IDE example sketch called blink without delay, you will see a method of keeping track of time and making decision without having to block program flow. It uses the millis() function and variables you use to store past 'time stamps' and variables where you store the various fixed time intervals you need in your sketch. Simply calculating if the present millis() time is greater then a past saved time + the a desired interval, can be used to make program decisions.

Lefty

Thank you so much, very helpfull

As Lefty points out, BlinkWithoutDelay is a great starting point. Be careful to do the test this way:

// check for 3 seconds since someStartTime
if ( ( millis() - someStartTime ) > 3000 )

If you do it another way, the test will not function correctly when the time wraps around to 0 (which happens every 50 days or so.)

A convenient way to do this legibly is to write a well named function to help:

// place this function earlier in the file than the loop() function
unsigned long TimeSince( unsigned long startTimeInMillis )
{
    return millis() - startTimeInMillis;
}


// then, in place of the previous test...
if ( TimeSince( someStartTime  ) > 3000 )

Search the forum for 'state machine' too, it should get you some helpful examples

wildbill: Search the forum for 'state machine' too, it should get you some helpful examples

A funny fact from my life: University absolutely failed to explain to me what a (Finite) State Machine is or why I'd want one for anything but theoretical computer science. A couple of days with my Arduino, and hey, FSMs seem really cool.