// Lab 3 Pass & Pass Plus
String imvalue;
const int buzzerpin = 5;
int tonestate = 0;
const int ledpinOne = 8;
const int ledpinTwo = 7;
const int ledpinThree = 6;
int ledOneState = LOW;
int ledTwoState = LOW; // ledState used to set the LED
unsigned long previousMillisTwo = 0; // will store last time LED was updated
int intervalTwo = 500;
int ledThreeState = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buzzerpin, OUTPUT);
pinMode(ledpinOne, OUTPUT);
pinMode(ledpinTwo, OUTPUT);
pinMode(ledpinThree, OUTPUT);
}
void loop() {
displaymenu(); // Main Display Menu text
while (Serial.available() == 0)
{
// LOOPING WHILE WAITING FOR VALUE
// Blinking LED
unsigned long currentMillisTwo = millis();
if (currentMillisTwo - previousMillisTwo >= intervalTwo) {
// save the last time you blinked the LED
previousMillisTwo = currentMillisTwo;
// if the LED is off turn it on and vice-versa:
if (ledTwoState == LOW) {
ledTwoState = HIGH;
}
else {
ledTwoState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledpinTwo, ledTwoState);
}
}
imvalue = Serial.readString(); // Read string in from Serial line imput
imvalue.trim(); // Remove whitespace
// OPTION 1 SELECTED
if (imvalue == "1") {
Serial.println("buzzer has been toggled");
if (ledOneState == HIGH) {
ledOneState = LOW;
}
else {
ledOneState = HIGH;
}
if (tonestate == 0) {
tonestate = 1000;
}
else {
tonestate = 0;
}
}
// OPTION 2 SELECTED
else if (imvalue == "2") {
Serial.println("led 2 blinking has been incremented");
intervalTwo = intervalTwo/2;
}
// OPTION 3 SELECTED
else if (imvalue == "3") {
Serial.println("led 2 blinking has been decremented");
intervalTwo = intervalTwo*2;
}
// OPTION 4 SELECTED
else if (imvalue == "4") {
ledThreeState += 20;
if (ledThreeState > 240){
Serial.println("Brightness reset");
ledThreeState = 0;
}
else {
Serial.println("led 3 brightness increased");
}
}
// IF ERROR
else {
Serial.println("Input was unsuccesful");
}
tone(buzzerpin, tonestate);
digitalWrite(ledpinOne, ledOneState); // Disp led 1
analogWrite(ledpinThree, ledThreeState);
//}
}
void displaymenu() {
Serial.println ("--------------- Menu ---------------");
Serial.println("- Toggle buzzer : Press 1");
Serial.println("- Led 2 increse speed : Press 2");
Serial.println("- Led 2 decrese speed : Press 3");
Serial.println("- Toggle brightness led 3 : Press 4");
}