// Blue button has bouncing simulation disabled.
#define BUTTON_PIN A1
#define LED_red_PIN 5
#define LED_green_PIN 4
#define LED_yellow_PIN 6
void setup() {
// we setup the pins and connection
pinMode(LED_red_PIN, OUTPUT);
pinMode(LED_green_PIN, OUTPUT);
pinMode(LED_yellow_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LED_green_PIN, HIGH); // Green is ON
}
// global variables
char current_light = 'G'; // G, Y, R
//int last_button_state = HIGH;
void loop() {
// MAJOR REQUIREMENT: ONLY ONE LIGHT CAN BE ON AT AN TIME
//delay(1000);
int current_button_state = digitalRead((BUTTON_PIN));
// if the button is pressed, button_state = LOW
if (current_button_state == LOW) {
// if the button is pressed, then change the light:
// G -> Y -> R, or R -> Y -> G... and wait in Yellow for 1 second
if (current_light == 'G') { // go to Y -> R
// first turn OFF the Green
digitalWrite(LED_green_PIN, LOW);
// TURN ON the YELLOW, and wait for 1 second
digitalWrite(LED_yellow_PIN, HIGH);
delay(1000); // ms
digitalWrite(LED_yellow_PIN, LOW);
// TURN ON the RED
digitalWrite(LED_red_PIN, HIGH);
current_light = 'R';
}
else if (current_light == 'R') { // go to Y -> G
digitalWrite(LED_red_PIN, LOW);
// TURN ON the YELLOW, and wait for 1 second
digitalWrite(LED_yellow_PIN, HIGH);
delay(1000); // ms
digitalWrite(LED_yellow_PIN, LOW);
// TURN ON the green
digitalWrite(LED_green_PIN, HIGH);
current_light = 'G';
}
}
/*
int current_button_state = digitalRead((BUTTON_PIN));
// if the button is pressed, button_state = LOW
if (lastState != current_button_state) {
lastState = current_button_state;
if (current_button_state == HIGH) {
digitalWrite(LED_red_PIN, LOW); // off
digitalWrite(LED_green_PIN, HIGH); // on
}
if (current_button_state == LOW) {
digitalWrite(LED_red_PIN, HIGH); // on
digitalWrite(LED_green_PIN, LOW); // off
}
}*/
}