// Black - ground wire
// Red - Power supply wire
// short leg on LED is cathode and should be connected to the ground
//Define LEDs
const int red = 12;
const int yellow = 11;
const int green = 10;
const int Button = 2;
// define time delays
int time_delay = 300;
int LEDchecker = 1;
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(Button, INPUT);
//must initialise pins all as off initally
initialise_pins();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Button) == LOW){
disco_lights();
}
}
/* if you wanted to be able to pause as soon as Button = HIGH, you could check the Button reading
between each delay
if (digitalRead(Button) == LOW){
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
delay(time_delay);
}
if (digitalRead(Button) == LOW){
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(time_delay);
}
*/
// another method
void disco_lights(){
if (digitalRead(Button) == LOW){
if (LEDchecker == 1){
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
LEDchecker = 2;
}
else{
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
LEDchecker = 1;
}
delay(time_delay);
}
}
//*/
// function to initialise pins
void initialise_pins(){
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
/*
// function for traffic lights
void disco_lights(){
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
delay(time_delay);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(time_delay);
digitalWrite(yellow, LOW);
}
*/
// your method and the method used are both correct, but the method used pauses the
// blinking mid blink, whilst yours waited for the loop to end.