// constants (or #define with no ; at end of line) won't change. They're used here to set pin numbers:
#define LED_PIN_R 8
#define BUTTON_PIN_R 7
#define LED_PIN_G 9
#define BUTTON_PIN_G 6
const int buzzer = 5; //buzzer to arduino pin 5
// Variables will change:
int led_R_State = HIGH; // the current state of the R output pin
int button_R_State; // the current reading from the R input pin
int lastButton_R_State = LOW; // the previous reading from the R input pin
int led_G_State = HIGH; // the current state of the G output pin
int button_G_State; // the current reading from the G input pin
int lastButton_G_State = LOW; // the previous reading from the G input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounce_R_Time = 0; // the last time the output pin was toggled
unsigned long lastDebounce_G_Time = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
// initialize serial:
// Serial.begin(9600);
pinMode(buzzer, OUTPUT); // Set buzzer as an output
// initialize pin mode:
pinMode(LED_PIN_R, OUTPUT);
pinMode(BUTTON_PIN_R, INPUT);
pinMode(LED_PIN_G, OUTPUT);
pinMode(BUTTON_PIN_G, INPUT);
// set initial LED state
digitalWrite(LED_PIN_R, HIGH);
digitalWrite(LED_PIN_G, HIGH);
}
void loop() {
// read the state of the RED switch into a local variable:
int reading = digitalRead(BUTTON_PIN_R);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButton_R_State) {
// reset the debouncing timer
lastDebounce_R_Time = millis();
}
if ((millis() - lastDebounce_R_Time) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button_R_State) {
button_R_State = reading;
// only toggle the LED if the new button state is HIGH
if (button_R_State == HIGH) {
digitalWrite(LED_PIN_R, HIGH);
digitalWrite(LED_PIN_G, LOW);
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(100); // ...for 1 sec
noTone(buzzer); // Stop sound...
}
}
}
lastButton_R_State = reading;
// read the state of the GREEN switch into a local variable:
int readingG = digitalRead(BUTTON_PIN_G);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (readingG != lastButton_G_State) {
// reset the debouncing timer
lastDebounce_G_Time = millis();
}
if ((millis() - lastDebounce_G_Time) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != button_G_State) {
button_G_State = readingG;
// only toggle the LED if the new button state is HIGH
if (button_G_State == HIGH) {
digitalWrite(LED_PIN_R, LOW);
digitalWrite(LED_PIN_G, HIGH);
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(100); // ...for 1 sec
noTone(buzzer); // Stop sound...
}
}
}
lastButton_G_State = reading;
}