I wrote a code to control 3 LED's that act like a stop light. The stoplight function works, however the button pressing does not turn it on or off. It simply stays on as long as the Arduino board is powered. Here is the code. I used to button function from the code in a "Getting Started with Arduino", book.
/* This is a simultion of a stop light.
Connect the color of LED specified by the define method into the pin # after
the type of color.
*/
#define green 3 //defines green LED
#define yellow 4 //defines yellow LED
#define red 5 //defines red LED
#define button 12 //defines button used to turn sequence on or off
int val = 0;
int old_val = 0;
int state = 0;
unsigned int minute = 60000;
int ten_seconds = 10000;
void setup()
{
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);
pinMode(button, INPUT);
}
void loop ()
{
val = digitalRead(button);
if ((val == HIGH) && (old_val == LOW))
{
state = 1 - state;
delay(10);
}
old_val = val;
if(state == 1)
{
digitalWrite(green,HIGH);
delay(minute);
digitalWrite(green,LOW);
digitalWrite(yellow,HIGH);
delay(ten_seconds);
digitalWrite(yellow,LOW);
digitalWrite(red,HIGH);
delay(minute);
digitalWrite(red,LOW);
}
else
{
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
}
}