pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/bdubs-astro/DebounceSwitch

crossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-9e07ff8eaaaff3a3.css" /> GitHub - bdubs-astro/DebounceSwitch: de-bounce a mechanical switch · GitHub
Skip to content

bdubs-astro/DebounceSwitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DebounceSwitch

A c++ class for de-bouncing a mechanical switch.

  • The required parameters are:

    • the GPIO input pin #
    • a flag that's true if using an internal pullup resistor, or false otherwise
    • a pair of callback functions that get called when the input pin goes either low or high
    • a debounce delay in milliseconds
  • The initPin() member function must be called first. It configures the input pin, and returns the inital state of the switch.

  • The readPin(debounceDelay) member function returns the current state (de-bounced), and calls the appropriate callback function whenever the pin's state changes.

Sample Code:

#include <Arduino.h>
#include "DebounceSwitch.h"

#define DEVICE 0    // ! Arduino Nano Every = 0, ESP8266 = 1

// I/O ...
#if DEVICE == 0
// ? Arduino Nano Every
#define LED_BUILTIN 13          // ? built in LED on pin 13     
#define LED_ON HIGH             // ? active high
#define swPin 12                // ? Arduino Nano Every D12
#define intPullup true          // ? use internal pullup resistor?
#elif DEVICE == 1
// ? ESP8266
#define LED_BUILTIN 2           // ? built in LED on pin 2  
#define LED_ON LOW              // ? active low
#define swPin 13                // ? ESP8266 D7
#define intPullup false         // ? use internal pullup resistor?
#endif

#ifndef LED 
#define LED LED_BUILTIN          
#endif

// configure switch ...
void loCallback(), hiCallback();
DebounceSwitch mySwitch(swPin, intPullup, loCallback, hiCallback);

void setup() {
  // establish serial comms ...
  Serial.begin(115200);
  while(!Serial) {;}                // wait for connection
  delay(3000);                      // settling time
  Serial.println(); Serial.println("Serial monitor connected."); 
  Serial.println();

  // configure indicator LED ...
  pinMode(LED, OUTPUT); 

  // initialize switch ...
  bool swStateInit = mySwitch.initPin(); 
  digitalWrite(LED, swStateInit ? !LED_ON : LED_ON);  // ! active low

  const char *swStateStr[] = {"LOW", "HIGH"};
  Serial.print("Initial input state is ");
  Serial.print(swStateStr[swStateInit]);
  Serial.println("."); Serial.println();
}

void loop() {
  int debounceDelay = 50;           // (ms)
  mySwitch.readPin(debounceDelay); 
}

// callback functions ...
void loCallback() {
  digitalWrite(LED, LED_ON);                        // ! active low
  Serial.println("Input just went LOW."); Serial.println();
}

void hiCallback() {
  digitalWrite(LED, !LED_ON);                       // ! active low
  Serial.println("Input just went HIGH."); Serial.println();
}

Digital Inputs: Pullup vs. Pulldown Resistors

ESP8266 D1 Mini Pinout

Source: https://www.mischianti.org/2021/04/26/wemos-d1-mini-high-resolution-pinout-and-specs/

Source: https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/

ESP32 Dev Kit C v4 Pinout

Source: https://forum.fritzing.org/t/esp32s-hiletgo-dev-boad-with-pinout-template/5357

Source: https://www.mischianti.org/2021/07/17/esp32-devkitc-v4-high-resolution-pinout-and-specs/

Arduino Nano Every Pinout

Source: https://docs.arduino.cc/hardware/nano-every/

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy