// Two LED Toggle
const int BTN_PIN = 12;
const int LED1_PIN = 11;
const int LED2_PIN = 10;
bool led1State = false;
bool led2State = false;
int oldBtnState = HIGH; // pin idles HIGH
bool checkButton() {
bool wasPressed = false;
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
Serial.println("Button Pressed");
wasPressed = true;
} else { // was just released
Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
return wasPressed;
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
}
void loop() {
bool wasPressed = checkButton();
if (wasPressed) {
led1State = !led1State;
led2State = !led1State;
digitalWrite(LED1_PIN, led1State);
digitalWrite(LED2_PIN, led2State);
}
}