pleasedontcode

Debounced Pushbutton rev_01

Oct 6th, 2025
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Debounced Pushbutton
  13.     - Source Code NOT compiled for: Arduino Opta Lite
  14.     - Source Code created on: 2025-10-06 21:19:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* give me example of button */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. const int buttonPin = 2; // Pushbutton connected to digital pin 2
  33. const int ledPin = 13;   // On-board LED
  34.  
  35. int buttonState = HIGH;
  36. int lastButtonState = HIGH;
  37. unsigned long lastDebounceTime = 0;
  38. unsigned long debounceDelay = 50;
  39. bool ledState = false;
  40.  
  41. void setup(void)
  42. {
  43.     // Example: Pushbutton with internal pull-up resistor toggling the LED
  44.     pinMode(buttonPin, INPUT_PULLUP);
  45.     pinMode(ledPin, OUTPUT);
  46.     digitalWrite(ledPin, LOW);
  47. }
  48.  
  49. void loop(void)
  50. {
  51.     int reading = digitalRead(buttonPin);
  52.  
  53.     if (reading != lastButtonState)
  54.     {
  55.         lastDebounceTime = millis();
  56.     }
  57.  
  58.     if ((millis() - lastDebounceTime) > debounceDelay)
  59.     {
  60.         if (reading != buttonState)
  61.         {
  62.             buttonState = reading;
  63.             if (buttonState == LOW)
  64.             {
  65.                 ledState = !ledState;
  66.                 digitalWrite(ledPin, ledState ? HIGH : LOW);
  67.             }
  68.         }
  69.     }
  70.  
  71.     lastButtonState = reading;
  72. }
  73.  
  74. /* END CODE */
  75.  
Advertisement
Add Comment
Please, Sign In to add comment