seems to be not working i forgot to mention i am trying to simulate a stop light so when i press the button the light delays goes to yellow then red while at red the cross walk goes on . i think the traffic light is the best way to get started with programming my code is below it works the way i want it to but i have to be holding down the button which also means that red will stay on as long the button is HIGH i would like it to go back into the loop after red light.
feed back on my code greatly appreciated
const int red = 5; //red led is on pin 5
const int yellow = 4; //yellow led on pin 4
const int green = 3; //green led on pin 3
const int crosswalk = 6; //crosswalk button on pin 6
int buttonState = 0; // button state is LOW
void setup()
{
Serial.begin(9600);
pinMode(red, OUTPUT); //pins red- green set as Outputs
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(crosswalk, INPUT); //crosswalk button set as input device
}
void loop(){
buttonState = digitalRead(crosswalk);
if (buttonState == HIGH){
delay(500);
digitalWrite(yellow, LOW);
delay(500);
digitalWrite(red, HIGH);
delay(100);
}
else
{
digitalWrite(yellow, LOW);
Serial.println("red");
digitalWrite(red, HIGH); // set the LED on
delay(8000); // wait for eight seconds
digitalWrite(red, LOW); // set the LED off
delay(1000); // wait for a second
Serial.println("green");
digitalWrite(green, HIGH); // set the LED on
delay(5000); // wait for 5 seconds
digitalWrite(green, LOW); // set the LED off
delay(1000);
Serial.println("yellow");
digitalWrite(yellow, HIGH); // set the LED on
delay(3000); // wait for three seconds
digitalWrite(yellow, LOW); // set the LED off
delay(1000);
}
}