/*#define LED1 2
#define SW 13
void setup() {
pinMode(LED1, OUTPUT);
pinMode(SW, INPUT);
}
void loop() {
if(!digitalRead(SW)){
digitalWrite(LED1, !digitalRead(LED1));
}
delay(50);
}
*/
const int switchPin = 13; // Switch pin
const int ledPin = 2; // LED pin
bool isOn = false; // Variable to track the state of the LED
void setup() {
pinMode(switchPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// Initially turn off the LED
digitalWrite(ledPin, LOW);
}
void loop() {
// Read the state of the switch
bool switchState = digitalRead(switchPin);
// Check if the switch is pressed
if (switchState == LOW) {
delay(50); // Debounce delay
// Check if the switch is still pressed after the debounce delay
if (digitalRead(switchPin) == LOW) {
// Toggle the state of the LED
isOn = !isOn;
digitalWrite(ledPin, isOn ? HIGH : LOW);
}
// Wait until the switch is released
while (digitalRead(switchPin) == LOW) {
delay(10);
}
}
}