Advertisement
Szerelo

Repeat Button

Mar 1st, 2022
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. // Repeat Button
  2.  
  3. #define buttonStart A3
  4.  
  5. unsigned long debounceDelay=50;     // the debounce time; increase if the output flickers
  6. unsigned long longPressDelay=1000;  // A gombokhoz tartozó hosszú megnyomás
  7. unsigned long repeatDelay=400;      // A gombokhoz tartozó ismétlődés ideje
  8. signed long repeatDelayDynamic=400;
  9. signed long dynamic=10;
  10. unsigned long lastDebounceTime1=0;
  11.  
  12. unsigned long counter=0;
  13.  
  14.  
  15. bool readingButton1, readingButton2, readingButton3, readingButton4, readingButton5;
  16. bool buttonShort1=false, buttonShort2=false, buttonShort3=false, buttonShort4=false, buttonShort5=false;
  17. bool buttonLong1=false, buttonLong2=false, buttonLong3=false, buttonLong4=false, buttonLong5=false;
  18. bool repeat1=false;
  19.  
  20. void setup(){  
  21.   pinMode(buttonStart, INPUT_PULLUP);
  22.   Serial.begin(115200);
  23.   Serial.println("Start ...");
  24.  
  25. }
  26.  
  27. void loop(){
  28.   buttonCheck1();
  29.  
  30.   if (buttonShort1==true and readingButton1==HIGH){
  31.     buttonShort1=false;
  32.     //Serial.println("Start Short Press");
  33.     counter++;
  34.     Serial.println(counter);
  35.   }
  36.   if (buttonLong1==true){
  37.     buttonLong1=false;
  38. //    Serial.println("Start Long Press");
  39.     counter++;
  40.     Serial.println(counter);
  41.   }
  42. }
  43.  
  44. void buttonCheck1() {
  45.   readingButton1 = digitalRead(buttonStart);
  46.   if (readingButton1==LOW){
  47.     if (repeat1==false){
  48.       if ((millis() - lastDebounceTime1) > debounceDelay) {
  49.         buttonShort1=true;
  50.       }
  51.       if ((millis() - lastDebounceTime1) > longPressDelay) {
  52.         //buttonLong1=true;
  53.         buttonShort1=false;
  54.         repeat1=true;
  55.       }
  56.     } //else {
  57.       if (millis()-lastDebounceTime1>repeatDelayDynamic) {
  58.         lastDebounceTime1=millis();
  59.         buttonLong1=true;
  60.         buttonShort1=false;
  61.         repeatDelayDynamic=repeatDelayDynamic-dynamic;
  62.         if (repeatDelayDynamic<10) repeatDelayDynamic=10;
  63.       }
  64.   }
  65.   if (readingButton1==HIGH) {
  66.     lastDebounceTime1=millis();
  67.     repeat1==false;
  68.     repeatDelayDynamic=repeatDelay;
  69.   }
  70. }
  71.  
  72. // END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement