Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views3 pages

Momentary Push Button

The document contains two Arduino sketches for controlling an LED with a button. The first sketch turns the LED on or off based on the button's state, while the second sketch toggles the LED state each time the button is pressed. Both sketches utilize serial communication to print the LED status to the console.

Uploaded by

samundi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Momentary Push Button

The document contains two Arduino sketches for controlling an LED with a button. The first sketch turns the LED on or off based on the button's state, while the second sketch toggles the LED state each time the button is pressed. Both sketches utilize serial communication to print the LED status to the console.

Uploaded by

samundi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MOMENTARY PUSH BUTTON:

int button = 0;

void setup() {

Serial.begin(9600);

pinMode(2, OUTPUT); // LED connected to pin 2

pinMode(3, INPUT); // Button connected to pin 3

void loop() {

button = digitalRead(3); // Read button state

if (button == HIGH) {

digitalWrite(2, HIGH); // Turn LED ON

Serial.println("ON");

} else {

digitalWrite(2, LOW); // Turn LED OFF

Serial.println("OFF");

}
TOGGLE:

int buttonState = 0; // Current reading from button

int lastButtonState = 0; // Previous button state

int ledState = LOW; // LED state

void setup() {

Serial.begin(9600);

pinMode(2, OUTPUT); // LED on pin 2

pinMode(3, INPUT); // Button on pin 3

void loop() {

buttonState = digitalRead(3);

// Check if button state changed from LOW to HIGH

if (buttonState == HIGH && lastButtonState == LOW) {

// Toggle LED state

ledState = !ledState;

// Update LED

digitalWrite(2, ledState);

if (ledState == HIGH) {

Serial.println("ON");

} else {

Serial.println("OFF");

}
// Small delay for debouncing

delay(50);

// Save current state as last state

lastButtonState = buttonState;

You might also like