// const int buttonPin = 2; // the pin that the pushbutton is attached to
// const int ledPin = 13; // the pin that the LED is attached to
// // Variables will change:
// int buttonPushCounter = 0; // counter for the number of button presses
// int buttonState = 0; // current state of the button
// int lastButtonState = 0; // previous state of the button
// void setup() {
// // initialize the button pin as a input:
// pinMode(buttonPin, INPUT);
// // initialize the LED as an output:
// pinMode(ledPin, OUTPUT);
// // initialize serial communication:
// Serial.begin(9600);
// }
// void loop() {
// // read the pushbutton input pin:
// buttonState = digitalRead(buttonPin);
// // compare the buttonState to its previous state
// if (buttonState != lastButtonState) {
// // if the state has changed, increment the counter
// if (buttonState == HIGH) {
// // if the current state is HIGH then the button went from off to on:
// buttonPushCounter++;
// Serial.println("on");
// Serial.print("number of button pushes: ");
// Serial.println(buttonPushCounter);
// } else {
// // if the current state is LOW then the button went from on to off:
// Serial.println("off");
// }
// // Delay a little bit to avoid bouncing
// delay(50);
// }
// // save the current state as the last state, for next time through the loop
// lastButtonState = buttonState;
// // turns on the LED every four button pushes by checking the modulo of the
// // button push counter. the modulo function gives you the remainder of the
// // division of two numbers:
// if (buttonPushCounter % 4 == 0) {
// digitalWrite(ledPin, HIGH);
// } else {
// digitalWrite(ledPin, LOW);
// }
// }
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-debounce
*/
// constants won't change. They're used here to set pin numbers:
// const int BUTTON_PIN = 2; // the number of the pushbutton pin
// const int ledPin = 13;
// const int DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers
// // Variables will change:
// int lastSteadyState = LOW; // the previous steady state from the input pin
// int lastFlickerableState = LOW; // the previous flickerable state from the input pin
// int currentState; // the current reading from the input pin
// // the following variables are unsigned longs because the time, measured in
// // milliseconds, will quickly become a bigger number than can be stored in an int.
// unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
// void setup() {
// // initialize serial communication at 9600 bits per second:
// Serial.begin(9600);
// // initialize the pushbutton pin as an pull-up input
// // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
// pinMode(BUTTON_PIN, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// }
// void loop() {
// // read the state of the switch/button:
// currentState = digitalRead(BUTTON_PIN);
// // check to see if you just pressed the button
// // (i.e. the input went from LOW to HIGH), and you've waited long enough
// // since the last press to ignore any noise:
// // If the switch/button changed, due to noise or pressing:
// if (currentState != lastFlickerableState) {
// // reset the debouncing timer
// lastDebounceTime = millis();
// // save the the last flickerable state
// lastFlickerableState = currentState;
// }
// if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
// // whatever the reading is at, it's been there for longer than the debounce
// // delay, so take it as the actual current state:
// // if the button state has changed:
// if (lastSteadyState == HIGH && currentState == LOW)
// {
// Serial.println("The button is pressed");
// digitalWrite(ledPin, HIGH);
// }
// else
// digitalWrite(ledPin, LOW);
// // else if (lastSteadyState == LOW && currentState == HIGH)
// // {
// // Serial.println("The button is released");
// // digitalWrite(ledPin, LOW);
// // }
// // save the the last steady state
// lastSteadyState = currentState;
// }
// }
// constants won't change. They're used here to set pin numbers:
// const int BUTTON_PIN = 2; // the number of the pushbutton pin
// const int ledPin = 13;
// const int DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers
// const int AnalogPin = A0;
// // Variables will change:
// int lastSteadyState = LOW; // the previous steady state from the input pin
// int lastFlickerableState = LOW; // the previous flickerable state from the input pin
// int currentState; // the current reading from the input pin
// // the following variables are unsigned longs because the time, measured in
// // milliseconds, will quickly become a bigger number than can be stored in an int.
// unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
// void setup() {
// // initialize serial communication at 9600 bits per second:
// Serial.begin(9600);
// // initialize the pushbutton pin as an pull-up input
// // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
// pinMode(BUTTON_PIN, INPUT_PULLUP);
// pinMode(ledPin, OUTPUT);
// }
// void loop() {
// // read the state of the switch/button:
// // currentState = digitalRead(BUTTON_PIN);
// currentState = analogRead(AnalogPin);
// // check to see if you just pressed the button
// // (i.e. the input went from LOW to HIGH), and you've waited long enough
// // since the last press to ignore any noise:
// // If the switch/button changed, due to noise or pressing:
// if (currentState != lastFlickerableState) {
// // reset the debouncing timer
// lastDebounceTime = millis();
// // save the the last flickerable state
// lastFlickerableState = currentState;
// }
// if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
// // whatever the reading is at, it's been there for longer than the debounce
// // delay, so take it as the actual current state:
// // if the button state has changed:
// if (lastSteadyState == HIGH && currentState == LOW)
// {
// Serial.println("The button is pressed");
// digitalWrite(ledPin, HIGH);
// }
// // else
// // digitalWrite(ledPin, LOW);
// // else if (lastSteadyState == LOW && currentState == HIGH)
// // {
// // Serial.println("The button is released");
// // digitalWrite(ledPin, LOW);
// // }
// // save the the last steady state
// lastSteadyState = currentState;
// }
// }
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}