naivxnaivet

Debounce Input (Button)

Nov 8th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //WITH RESISTOR ,
  2. //pinMode(buttonPin, INPUT);
  3. //digitalWrite(buttonPin, HIGH); //CLOSE
  4. //digitalWrite(buttonPin, LOW); //OPEN
  5. //
  6. //WITHOUT RESISTOR
  7. //pinMode(buttonPin, INPUT_PULLUP);
  8. //digitalWrite(buttonPin, LOW); //CLOSE
  9. //digitalWrite(buttonPin, HIGH); //OPEN
  10.  
  11.  
  12.  
  13. #define buttonPinRes 3
  14. int buttonStateRes;
  15. int lastButtonStateRes = LOW;
  16. unsigned long lastDebounceTimeRes = 0;
  17. unsigned long debounceDelay = 50;
  18. bool resSelected = false;
  19.  
  20. void setup()
  21. {
  22. Serial.begin(9600);
  23. pinMode(buttonPinRes, INPUT_PULLUP);
  24.  
  25. }
  26.  
  27. void loop() {
  28. readBtnRes();
  29. booleanCheck();
  30. }
  31.  
  32. void readBtnRes() {
  33. int reading = digitalRead(buttonPinRes);
  34. if (reading != lastButtonStateRes) {
  35. lastDebounceTimeRes = millis();
  36. }
  37. if ((millis() - lastDebounceTimeRes) > debounceDelay) {
  38. if (reading != buttonStateRes) {
  39. buttonStateRes = reading;
  40. if (buttonStateRes == LOW) {
  41. resSelected = true;
  42. }
  43. }
  44. }
  45. lastButtonStateRes = reading;
  46. }
  47.  
  48. void booleanCheck()
  49. {
  50. if(resSelected)
  51. {
  52. Serial.print("TRUE");
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment