const int button[] = {1,2,3,4}; //Pin values for the outputs of the buzzer and the 2 leds
const int pin[] = {12,11,10}; //Pin values for button inputs
int ButtonState[] = {-1,-1,-1,-1}; //Stores current state of each button
int CurrState[] = {0,0}; //Previous state of buzzer and LED
int FinalState[] = {0,0}; //Changes current state to 1 if button 1 and 4 is pressed
bool HoldButton[] = {false,false,false,false}; //Used to check if button is held/pressed
int blink = 1500; //blink duration of first LED
unsigned long start; //Numerical value of start time after event
unsigned long current; //Numerical value of current program run-time
void setup() {
analogWrite(pin[2] ,5); //set led to low state
for (int c = 0; c < 3; c++){ //Sets the buzzer and leds to output state
pinMode(pin[c], OUTPUT);
}
for (int c = 0; c < 4; c++){ //Sets the button pins to input state
pinMode(button[c], INPUT_PULLUP);
}
start = millis(); //initialises start time of program
}
void loop() {
current = millis(); //recording current program runtime
if (current - start >= blink){ //checks if time between start and current time is less than blink duration
digitalWrite(pin[1], !digitalRead(pin[1])); //switches state of blinking led
start = current;
}
//Button 1
ButtonState[0] = digitalRead(button[0]); //reads value of first button
if (ButtonState[0] == LOW){ //if the button is pressed set current state to 1
CurrState[0] = 1;
HoldButton[0] = true; //if button is pressed set to true
}
else if ((ButtonState[0] = HIGH) && (HoldButton[0] = true)){ //if button was let go but held earlier
if (CurrState[0] != FinalState[0]){ //if Final state is off
tone(pin[0], 1000);//set buzzer to a tone of frequency 1000 Khz
FinalState[0]=1; //Set final state to 1
CurrState[0]=0; //Set Current state to 0
HoldButton[0]=false; //Button not held set to false
}
if (CurrState[0] == FinalState[0]){ //if Final State was off
noTone(pin[0]); //Turn the buzzer off
FinalState[0] = 0; //Set Final State to 0 since buzzer is off
CurrState[0] = 0; //Set current state to 0
HoldButton[0] = false; //button not held
}
}
//Button 2
ButtonState[1] = digitalRead(button[1]); //reading value from button 2
if (ButtonState[1] == LOW){ //if button is pressed
HoldButton[1] = true; //HoldButton is true
}
else if (ButtonState[1] == HIGH && HoldButton[1] == true){ //if button 2 was let go but held earlier
HoldButton[1] = false;
if (blink >= 300){ //check if blink duration is above or 300 milliseconds
blink = blink - 100; //if true reduce time by 100 milliseconds
}
}
//Button 3
ButtonState[2] = digitalRead(button[2]); //read input value from button 3
if (ButtonState[2] == LOW){ //if button is pressed
HoldButton[2] == true; //HoldButton is set to true
}
else if (ButtonState[2] == HIGH && HoldButton[2] == true){ //if button was held earlier but off
if (blink <= 3000){ //check duration is less than 3000 milliseconds
blink = blink + 100; //increase period by 100 milliseconds
HoldButton[2]=false; //set button to false
}
}
//Button 4
ButtonState[3] = digitalRead(button[3]); //reads value from button 4
if (ButtonState[3] == LOW){ //if button 4 is pressed
CurrState[1] = 1; //set current state to 1
HoldButton[3] = true; //HoldButton is set to true
}
else if ((ButtonState[3] == HIGH) && (HoldButton[3]==true)){ //if button was held ealier but now off
if (CurrState[1] != FinalState[1]){ //if current state not equal to Final state
analogWrite(pin[2], 200); //set led to high state
FinalState[1] = 1; //Final State to 1
CurrState[1] = 0; //Current state to 1
HoldButton[3] = false; //button is not held
}
if (CurrState[1] == FinalState[1]){ //If current state is 1
analogWrite(pin[2], 5); //set led to low state
FinalState[1] = 0; //set last state to 0 since LED is off
CurrState[1] = 0; //Current state set to 0
HoldButton[3] = false; //Button not held
}
}
}