// Define pin numbers
const int button1Pin = 2; // Pin for button 1
const int button2Pin = 4; // Pin for button 2
const int ledPin = 8; // Pin for the LED
// Variables to store the button states
int button1State = 0;
int button2State = 0;
int lastButton1State = 0;
int lastButton2State = 0;
int ledState = LOW;
void setup() {
// Set the button pins as input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
// Initialize LED as OFF
digitalWrite(ledPin, ledState);
}
void loop() {
// Read the current states of the buttons
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// Check if button 1 is pressed
if (button1State != lastButton1State) {
if (button1State == HIGH) {
ledState = !ledState; // Toggle the LED state
digitalWrite(ledPin, ledState);
}
delay(50); // Debounce delay
}
// Check if button 2 is pressed
if (button2State != lastButton2State) {
if (button2State == HIGH) {
ledState = !ledState; // Toggle the LED state
digitalWrite(ledPin, ledState);
}
delay(50); // Debounce delay
}
// Save the current button states for the next loop
lastButton1State = button1State;
lastButton2State = button2State;
}