const int PINS[]={12,11,10};
unsigned long StartTime,CurrentTime,Duration=1500;
bool BuzzerOn=false,LED_On=false;
void setup() {
for (int j=0; j<3; j++){
pinMode(PINS[j], OUTPUT);
}
analogWrite(PINS[2], 236);
StartTime=millis();
Serial.begin(9600);
Serial.println("Choose an option from below:\n1. Toggle Buzzer ON/OFF\n2. Increase speed of Purple LED\n3. Decrease speed of Purple LED\n4. Toggle brightness of blue LED HIGH/LOW\n");
}
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;
}
//checking user input
if (Serial.available()){
switch(Serial.read()){
//Option 1
case '1':
if (BuzzerOn==false){
tone(PINS[0],1000);
BuzzerOn=true;
}
else{
noTone(PINS[0]);
BuzzerOn=false;
}
break;
//Option 2
case '2':
if (Duration>=200){
Duration=Duration-100;
}
break;
//Option 3
case '3':
Duration=Duration+100;
break;
//Option 4
case '4':
if (LED_On==false){
LED_On=true;
analogWrite(PINS[2],236);
}
else{
LED_On=false;
analogWrite(PINS[2],5);
}
}
}
}