int ledPin = 5;
int buttonPin = 2;
int torchState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// State change code
if (torchState == 0) {
if (digitalRead(buttonPin) == LOW) { // If the button is pressed
torchState = 1; // Change the torch state
delay(200); // Wait for the button to stop bouncing
}
}
if (torchState == 1) {
if (digitalRead(buttonPin) == LOW) { // If the button is pressed
torchState = 0; // Change the torch state
delay(200); // Wait for the button to stop bouncing
}
}
// LED control code
if (torchState == 0) {
digitalWrite(ledPin, LOW);
}
if (torchState == 1) {
digitalWrite(ledPin, HIGH);
}
}