#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int redButton = 1;
int greenButton = 2;
int amberButton = 3;
int redLED = 4;
int greenLED = 5;
int amberLED = 6;
int buzzer = 7;
int resetButton = 8;
bool waiting = false;
int redNote = 262;
int greenNote = 523;
int amberNote = 1047;
int duration = 250;
void setup() {
pinMode(redButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(amberButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(amberLED, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.init();
lcd.init();
lcd.backlight();
// Flash 4 times (each call flashes twice) to show ready
for (int i = 0; i < 2; i++) {
FlashLEDs();
}
}
void FlashLEDs() {
for (int i = 0; i < 2; i++){
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
digitalWrite(amberLED, HIGH);
delay(500);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(amberLED, LOW);
delay(500);
}
waiting = true;
}
bool checkButtons(){
// Check to see if a button has been pressed
// If there is a dead heat (very unlikely) both lights will show
bool pressed = false;
if (digitalRead(amberButton) == LOW){
digitalWrite(amberLED, HIGH);
tone(buzzer, amberNote, duration);
lcd.setCursor(3,0);
lcd.print("YELLOW");
pressed = true;
}
if (digitalRead(redButton) == LOW){
digitalWrite(redLED, HIGH);
tone(buzzer, redNote, duration);
lcd.setCursor(3,0);
lcd.print("RED");
pressed = true;
}
if (digitalRead(greenButton) == LOW){
digitalWrite(greenLED, HIGH);
tone(buzzer, greenNote, duration);
lcd.setCursor(3,0);
lcd.print("GREEN");
pressed = true;
}
return(pressed);
}
void loop() {
if (digitalRead(resetButton) == LOW)
{
FlashLEDs();
lcd.setCursor (3, 0);
lcd.print(" ");
}
if (waiting){
waiting = !checkButtons();
}
}