const int PINS[]={12,11,10},Button[]={2,3,4,5}; //initialise pins
int ButtonState[]={-1,-1,-1,-1};//set button state to neither high nor low
unsigned long StartTime,CurrentTime,Duration=1500;
bool BuzzerOn=false,LED_On=false,Pressed[] ={false,false,false,false};
//used to check whether button was pressed
void setup() {
for (int i=0; i<4; i++){
pinMode(Button[i], INPUT_PULLUP);//account for resistance in wires
}
for (int j=0; j<3; j++){
pinMode(PINS[j], OUTPUT);
}
analogWrite(PINS[2],236);
StartTime=millis();
}
void loop() {
//Ensure LED 2 is always blinking
CurrentTime=millis();
if ((CurrentTime-StartTime) >= Duration){
digitalWrite(PINS[1],!digitalRead(PINS[1])); //reverse the state of the second LED
StartTime=CurrentTime;
}
//Button 1
ButtonState[0]=digitalRead(Button[0]);
if (ButtonState[0]==LOW){
Pressed[0]=true;
}
else if (ButtonState[0]==HIGH && Pressed[0]==true){
if (BuzzerOn==false){
tone(PINS[0],1000);
Pressed[0]=false;
BuzzerOn=true;
}
else{
noTone(PINS[0]);
Pressed[0]=false;
BuzzerOn=false;
}
}
//Button 2
ButtonState[1]=digitalRead(Button[1]);
if (ButtonState[1]==LOW){
Pressed[1]=true;
}
else if (ButtonState[1]==HIGH && Pressed[1]==true){
Pressed[1]=false;
if (Duration>=200){
Duration=Duration-100;
}
}
//Button 3
ButtonState[2]=digitalRead(Button[2]);
if (ButtonState[2]==LOW){
Pressed[2]=true;
}
else if (ButtonState[2]==HIGH && Pressed[2]==true){
Pressed[2]=false;
Duration=Duration+100;
}
//Button 4
ButtonState[3]=digitalRead(Button[3]);
if (ButtonState[3]==LOW){
Pressed[3]=true;
}
else if (ButtonState[3]==HIGH && Pressed[3]==true){
if (LED_On==false){
analogWrite(PINS[2],236);
Pressed[3]=false;
LED_On=true;
}
else{
analogWrite(PINS[2],5);
Pressed[3]=false;
LED_On=false;
}
}
}