const int ledPin = 2; // Pin connected to the LED
bool ledState = false; // Current state of the LED
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Turn off the LED initially
}
void loop() {
if (Serial.available() > 0) {
int command = Serial.parseInt(); // Read the incoming numeric command
// Process the received command using select-case structure
switch (command) {
case 1: // Command 1 to turn the LED on
digitalWrite(ledPin, HIGH); // Turn on the LED
ledState = true;
Serial.println("LED is ON");
break;
case 2: // Command 2 to turn the LED off
digitalWrite(ledPin, LOW); // Turn off the LED
ledState = false;
Serial.println("LED is OFF");
break;
case 3: // Command 3 to get the current LED state
if (ledState) {
Serial.println("LED is currently ON");
} else {
Serial.println("LED is currently OFF");
}
break;
default:
Serial.println("Invalid command. Available commands: 1 (ON), 2 (OFF), 3 (CURRENT)");
break;
}
// Read the remaining characters from the serial buffer to clear it
while (Serial.available() > 0) {
char _ = Serial.read();
}
}
}