stspringer

Script for either a Latched button press or a Momentary on Button press

Aug 22nd, 2023 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | Source Code | 0 0
  1. //Momentary on button switch or Latched button switch Demo
  2. //IF IN "LATCHED BUTTON MODE", WILL BLINK "Blink()" THE LED IF LATCHED BUTTON IS HELD DOWN kept "LOW" blinkLedPeriod = 300;
  3. //DECLARATIONS
  4.  
  5.  
  6. //CONSTANTS
  7. const byte Button_1 = 4; //pin 4
  8. const byte blinkLedPin = 13;
  9.  
  10. //INT
  11. int buttonState = HIGH; //this variable tracks the state of the button, low if not pressed, high if pressed
  12. //MOST IMPORTANT HERE
  13. int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
  14. int blinkLedPeriod = 300;
  15.  
  16. //LONG
  17. long lastDebounceTime = 0; // the last time the output pin was toggled
  18. long debounceDelay = 50; // the debounce time; increase if the output flickers
  19.  
  20. //UNSIGNED LONG
  21. unsigned long currentTime;
  22. unsigned long blinkLedStartTime;
  23.  
  24.  
  25.  
  26. //setup
  27. void setup()
  28. {
  29. Serial.begin(115200);
  30.  
  31. //PINMODE
  32. pinMode( Button_1, INPUT_PULLUP);
  33. pinMode(blinkLedPin, OUTPUT);
  34.  
  35. Serial.println(ledState);
  36. }
  37.  
  38. void loop()
  39. {
  40. currentTime = millis();
  41. button_1_press();
  42.  
  43. }//loop
  44.  
  45. void button_1_press()
  46. {
  47. //sample the state of the button - is it pressed or not?
  48. buttonState = digitalRead( Button_1);
  49.  
  50.  
  51. //filter out any noise by setting a time buffer
  52. if ( (millis() - lastDebounceTime) > debounceDelay)
  53. {
  54. //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
  55. if ( (buttonState == LOW) && (ledState < 0) )
  56. {
  57. Blink(); //call the Blink() function
  58. Serial.println("buttonState == LOW");
  59. Serial.println(ledState);
  60.  
  61. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //BEGIN: MOMENTARY ON BUTTON OPTION ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63. //to make a "Momentary on Button Switch", "uncomment" the two lines below, digitalWrite(blinkLedPin, HIGH); and ledState = -ledState; below else it's a Latched switch
  64.  
  65. //digitalWrite(blinkLedPin, HIGH); //turn an LED on /////////////////////////////////////
  66. //ledState = - ledState; //now the LED is on, we need to change the state
  67.  
  68.  
  69. lastDebounceTime = millis(); //set the current time
  70. }
  71. else if ( (buttonState == HIGH) && (ledState > 0) )
  72. {
  73.  
  74.  
  75. Serial.println("buttonState == HIGH");
  76. Serial.println(ledState);
  77.  
  78. //to make a "Momentary on Button Switch", "uncomment" the two lines below, digitalWrite(blinkLedPin, LOW); and ledState = -ledState; below else it's a Latched switch
  79.  
  80. //digitalWrite(blinkLedPin, LOW); //turn an LED off
  81. //ledState = - ledState; //now the LED is off, we need to change the state
  82.  
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. //END: MOMENTARY ON BUTTON OPTION ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85.  
  86.  
  87. lastDebounceTime = millis(); //set the current time
  88.  
  89. }//if button state
  90.  
  91. }//millis
  92.  
  93. }//button_1_presS
  94.  
  95.  
  96. void Blink() //function
  97. {
  98. //let's start with the single blinking LED
  99. if (currentTime - blinkLedStartTime >= blinkLedPeriod) //time to change the single LED state
  100. {
  101. digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
  102. blinkLedStartTime = currentTime;
  103. Serial.println(F("\tBLINK"));
  104. }
  105. }//BLINK
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment