Arduino Basics: LED ON/OFF with One Push Button | Example Code | English Subtitle
ฝัง
- เผยแพร่เมื่อ 20 ก.ย. 2024
- Arduino Basics: LED ON/OFF with One Push Button | Example Code | English Subtitle
You can use a Relay to control High watt eqipements,
Code : code is on the comment below.
Facebook : / doit20-104218935882053
Please Subscribe my Channel. ThankYou!
const int ledPin = 13; // Pin connected to the LED
const int buttonPin = 2; // Pin connected to the button
// Variable to store the previous state of the button
int previousButtonState = HIGH;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize the button pin as an input
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
}
void loop() {
// Read the current state of the button
int buttonState = digitalRead(buttonPin);
// Check if the button state has changed from HIGH to LOW
if (buttonState == LOW && previousButtonState == HIGH) {
// Toggle the LED state
digitalWrite(ledPin, !digitalRead(ledPin));
}
// Update the previous button state
previousButtonState = buttonState;
}