SwitchExample.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "Arduino.h"
  2. #include "avdweb_Switch.h"
  3. const byte pushButtonpin = 2;
  4. const byte toggleSwitchpin = 4;
  5. const byte multiresponseButtonpin = 12;
  6. const byte alleventsButtonpin = 13;
  7. int i;
  8. Switch pushButton = Switch(pushButtonpin); // button to GND, use internal 20K pullup resistor
  9. Switch toggleSwitch = Switch(toggleSwitchpin);
  10. Switch multiresponseButton = Switch(multiresponseButtonpin);
  11. Switch alleventsButton = Switch(alleventsButtonpin);
  12. // Other examples of constructors
  13. // Switch pushButtonVCC = Switch(pushButtonpin, INPUT, HIGH); // button to VCC, 10k pull-down resistor, no internal pull-up resistor, HIGH polarity
  14. // Switch pushButton1ms = Switch(pushButtonpin, INPUT_PULLUP, LOW, 1); // debounceTime 1ms
  15. void setup()
  16. { Serial.begin(9600);
  17. }
  18. void loop()
  19. { // pushButton simple events
  20. pushButton.poll();
  21. if(pushButton.switched()) Serial.print("pushButton switched ");
  22. if(pushButton.pushed()) {Serial.print("pushButton pushed "); Serial.print(++i); Serial.println(" times");}
  23. if(pushButton.released()) Serial.println("pushButton released");
  24. // toggleSwitch report status only when changed
  25. if(toggleSwitch.poll()) {Serial.print("toggleSwitch status changed to "); Serial.println(toggleSwitch.on());}
  26. // multiresponseButton complex events
  27. multiresponseButton.poll();
  28. if(multiresponseButton.longPress()) Serial.println("multiresponseButton longPress");
  29. if(multiresponseButton.doubleClick()) Serial.println("multiresponseButton doubleClick");
  30. if(multiresponseButton.singleClick()) Serial.println("multiresponseButton singleClick");
  31. // alleventsButton complex events
  32. alleventsButton.poll();
  33. if(alleventsButton.switched()) {Serial.println("all_e_B switched."); Serial.print(" all_e_B status to "); Serial.print(alleventsButton.on()); Serial.println(".");}
  34. if(alleventsButton.pushed()) {Serial.println(" all_e_B pushed.");}
  35. if(alleventsButton.released()) Serial.println(" all_e_B released.");
  36. if(alleventsButton.longPress()) Serial.println(" ==> all_e_B longPress.");
  37. if(alleventsButton.doubleClick()) Serial.println(" ==> all_e_B doubleClick.");
  38. if(alleventsButton.singleClick()) Serial.println(" ==> all_e_B singleClick.");
  39. }