Arduino nano button control on led
This code does the following:
- Sets pin 12 as the switch input
- Sets pin 13 as the LED output
- Toggles the LED on/off each time the switch is pressed
- Allows continuous cycling of the LED state
- Provides a simple on/off mechanism with each button press
To use this:
1. Open Arduino IDE
2. Copy and paste this code
3. Upload to an Arduino Nano
4. Connect a switch to pin 12 and an LED to pin 13
Key features:
- Uses INPUT_PULLUP to simplify switch wiring
- Implements a simple state toggle mechanism
- Includes a small delay to prevent switch bounce
- Works in a continuous loop, ready to be triggered multiple times
const int switchPin = 12; // Define switch pin as pin 12
const int ledPin = 13; // Define LED pin as pin 13
int switchState = 0; // Variable to store switch state
bool isLedOn = false; // Variable to track LED state
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Set switch pin as input with pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
// Read the current state of the switch
int reading = digitalRead(switchPin);
// Check if switch is pressed (LOW indicates switch is pressed)
if (reading == LOW) {
// Toggle LED state
isLedOn = !isLedOn;
if (isLedOn) {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Delay to prevent multiple triggers from a single press
delay(250);
}
}
"This Content Sponsored by Buymote Shopping app
BuyMote E-Shopping Application is One of the Online Shopping App
Now Available on Play Store & App Store (Buymote E-Shopping)
Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8
Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication"
