Buttonhandling.ino 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. bool buttonCurrentState[3] = {HIGH, HIGH, HIGH};
  2. bool buttonLastState[3] = {HIGH, HIGH, HIGH};
  3. unsigned long buttonDownMillis[3] = {0, 0, 0};
  4. unsigned long buttonCurrentHoldTime[3] = {0, 0, 0};
  5. bool buttonFired[3] = {false, false, false};
  6. bool buttonHoldFired[3] = {false, false, false};
  7. unsigned long lastButtonPress[3] = {0, 0, 0};
  8. void buttonAction(byte btn) {
  9. lastSwitchSource[btn] = 0;
  10. relaisToggle(btn);
  11. switch (btn) {
  12. case 0:
  13. if (strlen(mqtt_topic_out_1) > 0) mqttclient.publish(mqtt_topic_out_1, mqtt_payload_out_1, mqtt_btnRetain);
  14. break;
  15. case 1:
  16. if (strlen(mqtt_topic_out_2) > 0) mqttclient.publish(mqtt_topic_out_2, mqtt_payload_out_2, mqtt_btnRetain);
  17. break;
  18. case 2:
  19. if (strlen(mqtt_topic_out_3) > 0) mqttclient.publish(mqtt_topic_out_3, mqtt_payload_out_3, mqtt_btnRetain);
  20. break;
  21. }
  22. }
  23. void buttonHoldAction(byte btn) {
  24. if (hldToRel[btn] > 0) {
  25. relaisToggle( hldToRel[ btn - 1 ] );
  26. }
  27. switch (btn) {
  28. case 0:
  29. if (strlen(mqtt_topic_out_hold_1) > 0) mqttclient.publish(mqtt_topic_out_hold_1, mqtt_payload_out_hold_1, mqtt_btnRetain);
  30. break;
  31. case 1:
  32. if (strlen(mqtt_topic_out_hold_2) > 0) mqttclient.publish(mqtt_topic_out_hold_2, mqtt_payload_out_hold_2, mqtt_btnRetain);
  33. break;
  34. case 2:
  35. if (strlen(mqtt_topic_out_hold_3) > 0) mqttclient.publish(mqtt_topic_out_hold_3, mqtt_payload_out_hold_3, mqtt_btnRetain);
  36. break;
  37. }
  38. if (led_enabled[btn]) {
  39. // blink LED when hold action is triggered
  40. bool ledState = digitalRead(leds_pins[btn]);
  41. digitalWrite(leds_pins[btn], !ledState);
  42. delay(500);
  43. digitalWrite(leds_pins[btn], ledState);
  44. }
  45. }
  46. void checkButtonStates() {
  47. for (int i = 0; i < BUTTONS_COUNT; i++) {
  48. if (button_enabled[i]) checkButtonState(i);
  49. }
  50. }
  51. void checkButtonState(byte btn) {
  52. buttonCurrentHoldTime[btn] = millis() - buttonDownMillis[btn];
  53. buttonCurrentState[btn] = digitalRead(buttons_pins[btn]);
  54. if (buttonCurrentState[btn] == LOW && buttonLastState[btn] == HIGH) { // button was unpressed and is millis() pressed
  55. buttonDownMillis[btn] = millis();
  56. buttonLastState[btn] = buttonCurrentState[btn];
  57. }
  58. else if (buttonCurrentState[btn] == LOW && buttonLastState[btn] == LOW) { // button is held
  59. if ( buttonCurrentHoldTime[btn] > debounceTime ) {
  60. buttonLastState[btn] = buttonCurrentState[btn];
  61. if ( buttonCurrentHoldTime[btn] > buttonHoldTime && !buttonHoldFired[btn] ) {
  62. // button is held longer
  63. if ( (millis() - lastButtonPress[btn]) > 500 ) { // avoid double triggering
  64. lastButtonPress[btn] = millis();
  65. Serial.print("Button ");
  66. Serial.print(btn + 1);
  67. Serial.println(" long pressed");
  68. buttonHoldAction(btn);
  69. buttonHoldFired[btn] = true;
  70. }
  71. }
  72. }
  73. }
  74. else if (buttonCurrentState[btn] == HIGH && buttonLastState[btn] == LOW) { // button is released again
  75. if (buttonCurrentHoldTime[btn] > debounceTime) { // entprellung
  76. buttonLastState[btn] = buttonCurrentState[btn];
  77. if (!buttonHoldFired[btn] && !buttonFired[btn]) {
  78. if ( (millis() - lastButtonPress[btn]) > 500 ) { // avoid double triggering
  79. lastButtonPress[btn] = millis();
  80. Serial.print("Button ");
  81. Serial.print(btn + 1);
  82. Serial.println(" short pressed");
  83. buttonAction(btn);
  84. }
  85. }
  86. buttonFired[btn] = false;
  87. buttonHoldFired[btn] = false;
  88. }
  89. }
  90. }