pleasedontcode

Debounced Toggle rev_01

Oct 6th, 2025
43
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 Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-06 21:27:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* give 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. /*
  33.   Button example on Arduino UNO:
  34.   - Button connected between pin 2 and GND
  35.   - Internal pull-up resistor enabled via INPUT_PULLUP
  36.   - LED on built-in pin 13 toggles on each press
  37.   - Debouncing implemented for reliable presses
  38. */
  39.  
  40. // Pin assignments
  41. const int BUTTON_PIN = 2;
  42. const int LED_PIN = 13;
  43.  
  44. // Debounce state
  45. int lastButtonState = HIGH;
  46. unsigned long lastDebounceTime = 0;
  47. const unsigned long DEBOUNCE_DELAY = 50; // milliseconds
  48.  
  49. // LED state
  50. int ledState = LOW;
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize serial for debugging (optional)
  55.     Serial.begin(9600);
  56.     // Initialize pins
  57.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  58.     pinMode(LED_PIN, OUTPUT);
  59.     digitalWrite(LED_PIN, ledState);
  60.     Serial.println("Button example started. Press the button to toggle LED.");
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     int reading = digitalRead(BUTTON_PIN);
  66.  
  67.     // If button state changed, reset debounce timer
  68.     if (reading != lastButtonState) {
  69.         lastDebounceTime = millis();
  70.     }
  71.  
  72.     // See if enough time has passed to debounce
  73.     if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
  74.         if (reading != lastButtonState) {
  75.             lastButtonState = reading;
  76.             // Button pressed (active LOW)
  77.             if (reading == LOW) {
  78.                 ledState = !ledState;
  79.                 digitalWrite(LED_PIN, ledState ? HIGH : LOW);
  80.                 Serial.print("Button pressed. LED is now ");
  81.                 Serial.println(ledState ? "ON" : "OFF");
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. /* END CODE */
  88.  
Advertisement
Add Comment
Please, Sign In to add comment