Kennet
Published © GPL3+

De-bounced Button/ Serial Monitor Displaly

This program counts de-bounced button presses and displays it on the serial monitor.

IntermediateShowcase (no instructions)2 hours505
De-bounced Button/ Serial Monitor Displaly

Things used in this project

Story

Read more

Code

Debounced Button Code

C/C++
This code counts button presses from S2.
/*
  This code displays S2 button count on the serial monitor and
  attempts to use button debouncing.
  Click firmly and slowly for the best results.
  
  By Kennet Thurman
 */

// digital pin 5 has a pushbutton attached to it. Give it a name:
int pushButton = 5;
int buttonState;
int lastButtonState;

int buttonCount = 0;
int lastButtonCount;


long lastDebounceTime = 0;
long debounceDelay = 150;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600); 
  // make the on-board pushbutton's pin an input pullup:
  pinMode(pushButton, INPUT_PULLUP);
}

// the loop routine runs over and over again forever:
void loop() {

 buttonState = digitalRead(pushButton);

 if (buttonState != lastButtonState){
    lastDebounceTime = millis();
    buttonCount++;  
 } 
 if ((millis() - lastDebounceTime) > debounceDelay){
    buttonState = buttonState; 
 }
 
 if (buttonState == LOW){
 
  if (buttonCount != lastButtonCount){
      lastButtonCount = buttonCount/2; 
      Serial.println(lastButtonCount);
      Serial.println(" - "); 
  }
 }
  lastButtonState = buttonState;
  lastButtonCount = buttonCount;
}

Credits

Kennet

Kennet

1 project • 0 followers
Senior in Computer Engineering

Comments