Szerelo

Pressing short and long buttons is an example

Nov 10th, 2021 (edited)
1,504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.09 KB | None | 0 0
  1. // Button press short-long
  2.  
  3. #define Button1 A0
  4. #define Button2 A1
  5. #define Button3 A2
  6. #define Led1 A3
  7. #define Led2 A4
  8. #define Relay1 2
  9. #define Relay2 3
  10.  
  11.  
  12. unsigned int currentTimer1, currentTimer2;
  13. unsigned int timeTimer1=10, timeTimer2=15;
  14. unsigned long debounceDelay=50;    // the debounce time; increase if the output flickers
  15. unsigned long longPressDelay=1000;
  16. unsigned long lastDebounceTime1=0;  // the last time the output pin was toggled
  17. unsigned long lastDebounceTime2=0;
  18. unsigned long lastDebounceTime3=0;
  19. unsigned long currentMillis=0, previousMillis=0;
  20.  
  21. bool readingButton1, readingButton2, readingButton3;
  22. bool buttonShort1=false, buttonShort2=false, buttonShort3=false;
  23. bool buttonLong1=false, buttonLong2=false, buttonLong3=false;
  24. bool buttonShortPress1=false, buttonShortPress2=false, buttonShortPress3=false;
  25. bool buttonLongPress1=false, buttonLongPress2=false, buttonLongPress3=false;
  26. bool reading1=true, reading2=true, reading3=true;
  27. bool Relay1Status=false, Relay2Status=false;
  28. bool Led1Status=false, Led2Status=false;
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.   Serial.println("Start ...");
  33.   pinMode (Button1, INPUT_PULLUP );
  34.   pinMode (Button2, INPUT_PULLUP );
  35.   pinMode (Button3, INPUT_PULLUP );
  36.   pinMode (Led1, OUTPUT );
  37.   pinMode (Led2, OUTPUT );
  38.   pinMode (Relay1, OUTPUT );
  39.   pinMode (Relay2, OUTPUT );
  40.  
  41. }
  42.  
  43. void loop() {
  44.   currentMillis = millis();
  45.   if (currentMillis - previousMillis >= 1000) {   // Timer every 1 second due to tick() refresh
  46.     previousMillis = currentMillis;
  47.     tick();
  48.   }
  49.  
  50.   buttonCheck1();
  51.   buttonCheck2();
  52.   buttonCheck3();
  53.  
  54.   if (buttonShort1==true and readingButton1==HIGH){
  55.     reading1=false;
  56.     buttonShort1=false;
  57.     Serial.println("Button1 Short Press");
  58.     Led1Status=false;
  59.     if (Relay1Status==false) {
  60.       Relay1Status=true;
  61.       currentTimer1=timeTimer1;
  62.     } else {
  63.       Relay1Status=false;
  64.       currentTimer1=0;
  65.     }
  66.   }
  67.   if (buttonLong1==true){
  68.     reading1=false;
  69.     buttonLong1=false;
  70.     Serial.println("Button1 Long Press");
  71.     Relay1Status=true;
  72.     Led1Status=true;
  73.     currentTimer1=0;
  74.   }
  75.  
  76.   if (buttonShort2==true and readingButton2==HIGH){
  77.     reading2=false;
  78.     buttonShort2=false;
  79.     Serial.println("Button2 Short Press");
  80.     Led2Status=false;
  81.     if (Relay2Status==false) {
  82.       Relay2Status=true;
  83.       currentTimer2=timeTimer2;
  84.     } else {
  85.       Relay2Status=false;
  86.       currentTimer2=0;
  87.     }
  88.   }
  89.   if (buttonLong2==true){
  90.     reading2=false;
  91.     buttonLong2=false;
  92.     Serial.println("Button2 Long Press");
  93.     Relay2Status=true;
  94.     Led2Status=true;
  95.     currentTimer2=0;
  96.   }
  97.  
  98.   if (buttonShort3==true and readingButton3==HIGH){
  99.     reading3=false;
  100.     buttonShort3=false;
  101.     Serial.println("Button3 Short Press");
  102.     Led1Status=false;
  103.     Led2Status=false;
  104.     if (Relay1Status==Relay2Status) {
  105.       if (Relay1Status==false) {
  106.         Relay1Status=true;
  107.         currentTimer1=timeTimer1;
  108.         Relay2Status=true;
  109.         currentTimer2=timeTimer2;
  110.       } else {
  111.         Relay1Status=false;
  112.         currentTimer1=0;
  113.         Relay2Status=false;
  114.         currentTimer2=0;
  115.       }
  116.     } else {
  117.       Relay1Status=true;
  118.       currentTimer1=timeTimer1;
  119.       Relay2Status=true;
  120.       currentTimer2=timeTimer2;
  121.     }
  122.   }
  123.   if (buttonLong3==true){
  124.     reading3=false;
  125.     buttonLong3=false;
  126.     Serial.println("Button3 Long Press");
  127.     Led1Status=true;
  128.     Relay1Status=true;
  129.     currentTimer1=0;
  130.     Led2Status=true;
  131.     Relay2Status=true;
  132.     currentTimer2=0;
  133.   }
  134.   digitalWrite(Relay1,Relay1Status);
  135.   digitalWrite(Relay2,Relay2Status);
  136.   digitalWrite(Led1,Led1Status);
  137.   digitalWrite(Led2,Led2Status);
  138. }
  139.  
  140. void tick() {
  141.   Serial.print(currentTimer1);
  142.   Serial.print(" - ");
  143.   Serial.println(currentTimer2);
  144.   if (currentTimer1 != 0) {
  145.     currentTimer1--;
  146.     if (currentTimer1 == 0) {
  147.       Relay1Status=false;
  148.     }
  149.   }
  150.  
  151.   if (currentTimer2 != 0) {
  152.     currentTimer2--;
  153.     if (currentTimer2 == 0) {
  154.       Relay2Status=false;
  155.     }
  156.   }
  157. }
  158.  
  159. void buttonCheck1() {
  160.   readingButton1 = digitalRead(Button1);
  161.   if (readingButton1==LOW and reading1==true) {
  162.     if ((millis() - lastDebounceTime1) > debounceDelay) {
  163.       buttonShort1=true;
  164.     }
  165.     if ((millis() - lastDebounceTime1) > longPressDelay) {
  166.       buttonLong1=true;
  167.       buttonShort1=false;
  168.     }
  169.   }
  170.   if (readingButton1==HIGH) {
  171.     lastDebounceTime1=millis();
  172.     reading1=true;
  173.   }
  174. }
  175.  
  176. void buttonCheck2() {
  177.   readingButton2 = digitalRead(Button2);
  178.   if (readingButton2==LOW and reading2==true) {
  179.     if ((millis() - lastDebounceTime2) > debounceDelay) {
  180.       buttonShort2=true;
  181.     }
  182.     if ((millis() - lastDebounceTime2) > longPressDelay) {
  183.       buttonLong2=true;
  184.       buttonShort2=false;
  185.     }
  186.   }
  187.   if (readingButton2==HIGH) {
  188.     lastDebounceTime2=millis();
  189.     reading2=true;
  190.   }
  191. }
  192.  
  193. void buttonCheck3() {
  194.   readingButton3 = digitalRead(Button3);
  195.   if (readingButton3==LOW and reading3==true) {
  196.     if ((millis() - lastDebounceTime3) > debounceDelay) {
  197.       buttonShort3=true;
  198.     }
  199.     if ((millis() - lastDebounceTime3) > longPressDelay) {
  200.       buttonLong3=true;
  201.       buttonShort3=false;
  202.     }
  203.   }
  204.   if (readingButton3==HIGH) {
  205.     lastDebounceTime3=millis();
  206.     reading3=true;
  207.   }
  208. }
  209.  
  210. /*
  211. void tmp() {
  212.   readingButton1 = digitalRead(Button1);
  213.     if (readingButton1==LOW ) {
  214.       if ((millis() - lastDebounceTime1) > debounceDelay) {
  215.         buttonShort1=true;
  216.       }
  217.       if ((millis() - lastDebounceTime1) > longPressDelay) {
  218.         buttonLong1=true;
  219.         buttonShort1=false;
  220.       }
  221.     } else {
  222.       lastDebounceTime1=millis();
  223.       if (buttonShort1==true) {
  224.         buttonShortPress1=true;
  225.         buttonShort1=false;
  226.       }
  227.       if (buttonLong1==true) {
  228.         buttonLongPress1=true;
  229.         buttonLong1=false;
  230.       }
  231.     }
  232.   if (buttonShortPress1==true) {
  233.     buttonShortPress1=false;
  234.     Serial.println("Short Press");
  235.   }
  236.   if (buttonLongPress1==true) {
  237.     reading1=true;
  238.     //lastDebounceTime=millis();
  239.     buttonLongPress1=false;
  240.     Serial.println("Long Press");
  241.   }
  242. }
  243. */
  244.  
  245. // END
Add Comment
Please, Sign In to add comment