const int buttonPin = A0; // The pin the button is connected to
const int appliancePins[] = {2 ,3 ,4}; // Pins connected to the appliances
const int numAppliances = 3; // Number of appliances
int currentState = 0; // Current state
bool lastButtonState = HIGH; // Previous state of the button
void setup() {
// Initialize button pin
pinMode(buttonPin, INPUT_PULLUP);
// Initialize appliance pins
for (int i = 0; i < numAppliances; i++) {
pinMode(appliancePins[i], OUTPUT);
digitalWrite(appliancePins[i], LOW); // Turn off all appliances initially
}
}
void loop() {
// Read the state of the button
bool buttonState = digitalRead(buttonPin);
// Check if the button is pressed and the state has changed
if (buttonState == LOW && lastButtonState == HIGH) {
// Move to the next state
currentState = (currentState + 1) % (numAppliances + 1);
// Toggle the corresponding appliance
if (currentState == 0) {
// Turn off all appliances if currentState is 0
for (int i = 0; i < numAppliances; i++) {
digitalWrite(appliancePins[i], LOW);
}
} else {
// Turn off all appliances
for (int i = 0; i < numAppliances; i++) {
digitalWrite(appliancePins[i], LOW);
}
// Turn on the appliance corresponding to the currentState
digitalWrite(appliancePins[currentState - 1], HIGH);
}
}
// Update the last button state
lastButtonState = buttonState;
}