const int ledpin = 12; // Pin for the LED
const int buttonpin = 13; // Pin for the button
byte buttonstate = 0;
byte ledstate = 0; // Initialize the LED state as off (0)
void setup() {
// Set the pin modes
pinMode(ledpin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(115200);
}
void loop() {
// Read the button state continuously
buttonstate = digitalRead(buttonpin);
Serial.println(buttonstate);
// If the button is pressed (HIGH)
if (buttonstate == HIGH) {
// Toggle the LED state
ledstate = 1 - ledstate;
// Turn the LED on or off depending on ledstate
if (ledstate == 1) {
digitalWrite(ledpin, HIGH); // LED on
} else {
digitalWrite(ledpin, LOW); // LED off
}
// Wait until the button is released before continuing
while (digitalRead(buttonpin) == HIGH);
}
}