Digital Input - Pushbutton - Pull Up / Pull Down Resistors

/* 
    button_debounce_pullup.ino

    Manages the debounce of a pushbutton or switch connected to a pullup resistor. 
*/

int pinLED = 5; 
int pinPushButtonPullUp = 6;
int buttonState = HIGH;     // buttonState & lastButtonState are initialized for a pullup resistor         
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // The switch debounce time.  50 to 100 ms

void setup() {

  pinMode(pinLED, OUTPUT);
  pinMode(pinPushButtonPullUp, INPUT);

  blinkLED(pinLED);
  
}   // setup()


void loop() {

  // Detect a change in the pushbutton state.
  // This logic is for a button using a pullup resistor. 
  buttonState = digitalRead(pinPushButtonPullUp); 
  // if millis() or timer wraps around, reset it
  if (lastDebounceTime > millis())  lastDebounceTime = millis();
  if (buttonState != lastButtonState) { 
    // Multiple changes in the buttonState can occur when a pushbutton is 
    // pressed, or a switch is toggled. Use a debounce timer to only react
    // to a change in button state after an interval of debounceDelay. 
    lastButtonState = buttonState;
    //  Check if enough time has passed to evaluate another pushbutton press.
    if ((millis() - lastDebounceTime) > debounceDelay) {
      lastDebounceTime = millis();
      if (buttonState == LOW) {
          digitalWrite(pinLED, HIGH);
      } else {
          digitalWrite(pinLED, LOW);
      } // buttonState
    } // millis()
  } // buttonState != lastButtonState

}   // loop()



void blinkLED(byte ledPIN){
  //  consumes 300 ms.
  for(int i = 5; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }    
} // blinkLED()

 

/* 
    button_debounce_pulldown.ino

    Manages the debounce of a pushbutton or switch connected to a pulldown resistor. 
*/

int pinLED = 5; 
int pinPushButtonPullDown = 7;
int buttonState = LOW;     // buttonState & lastButtonState are initialized for a pulldown resistor         
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // The switch debounce time.  50 to 100 ms

void setup() {

  pinMode(pinLED, OUTPUT);
  pinMode(pinPushButtonPullDown, INPUT);

  blinkLED(pinLED);

}   // setup()


void loop() {
  
  // Detect a change in the pushbutton state.
  // This logic is for a button using a pulldown resistor. 
  buttonState = digitalRead(pinPushButtonPullDown); 
  // if millis() or timer wraps around, reset it
  if (lastDebounceTime > millis())  lastDebounceTime = millis();
  if (buttonState != lastButtonState) { 
    // Multiple changes in the buttonState can occur when a pushbutton is 
    // pressed, or a switch is toggled. Use a debounce timer to only react
    // to a change in button state after an interval of debounceDelay. 
    lastButtonState = buttonState;
    //  Check if enough time has passed to evaluate another pushbutton press.
    if ((millis() - lastDebounceTime) > debounceDelay) {
      lastDebounceTime = millis();
      if (buttonState == HIGH) {
          digitalWrite(pinLED, HIGH);
      } else {
          digitalWrite(pinLED, LOW);
      } // buttonState
    } // millis()
  } // buttonState != lastButtonState

}   // loop()



void blinkLED(byte ledPIN){
  //  consumes 300 ms.
  for(int i = 5; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }    
} // blinkLED()

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.