// Define pins for buzzer and LEDs
#define BUZZER_PIN 5
#define LED_PIN_1 6
#define LED_PIN_2 9
#define LED_PIN_3 10
// Define variables for menu options
bool isBuzzerOn = false;
int ledBlinkSpeed = 500; // default blinking speed of LED
bool isLedBrightnessHigh = false; // default brightness of LED
void setup() {
// Initialize serial communication
Serial.begin(9600);
while (!Serial) {
; // Wait for the serial connection to be established
}
// Print the initial menu
Serial.println("Select a menu option:");
Serial.println("1: Toggle the buzzer");
Serial.println("2: Toggle LED 1");
Serial.println("3: Increase speed of LED 2");
Serial.println("4: Decrease speed of LED 2");
Serial.println("5: Toggle brightness of LED 3");
// Set pin modes
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_3, OUTPUT);
}
void loop() {
// Check if there is incoming data from serial
if (Serial.available() > 0) {
// Read the incoming byte
char menuOption = Serial.read();
// Execute the corresponding menu option
switch (menuOption) {
case '1':
// Toggle the buzzer on and off
isBuzzerOn = !isBuzzerOn;
digitalWrite(BUZZER_PIN, isBuzzerOn);
break;
case '2':
// Toggle the first LED on and off
digitalWrite(LED_PIN_1, !digitalRead(LED_PIN_1));
break;
case '3':
// Increase the speed of the second LED's blinking
ledBlinkSpeed -= 50;
if (ledBlinkSpeed < 50) {
ledBlinkSpeed = 50; // set minimum blinking speed
}
break;
case '4':
// Decrease the speed of the second LED's blinking
ledBlinkSpeed += 50;
if (ledBlinkSpeed > 1000) {
ledBlinkSpeed = 1000; // set maximum blinking speed
}
break;
case '5':
// Toggle the brightness of the third LED between high and low
isLedBrightnessHigh = !isLedBrightnessHigh;
if (isLedBrightnessHigh) {
analogWrite(LED_PIN_3, 255); // set high brightness
} else {
analogWrite(LED_PIN_3, 128); // set low brightness
}
break;
default:
// Invalid menu option, do nothing
break;
}
}
// Blink the second LED with the current blinking speed
static unsigned long previousMillis = 0;
if (millis() - previousMillis >= ledBlinkSpeed) {
previousMillis = millis();
digitalWrite(LED_PIN_2, !digitalRead(LED_PIN_2));
}
}