Advertisement
Szerelo

Timer

Feb 19th, 2022
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. // Timer
  2.  
  3. #define delayTime 5
  4.  
  5. #define buttonPin A0
  6. #define signalLedPin 9
  7. #define pilotLedPin 10
  8. #define relayPin 11
  9.  
  10. unsigned long debounceDelay=50;
  11. unsigned long longPressDelay=3000; // Long press time
  12. unsigned long lastDebounceTime1=0;
  13. unsigned long currentMillis=0, previousMillis=0, previousMillisTimer=0;
  14.  
  15. bool readingButton1;
  16. bool buttonShort1=false;
  17. bool buttonLong1=false;
  18. bool buttonShortPress1=false;
  19. bool buttonLongPress1=false;
  20. bool reading1=true;
  21.  
  22. bool pilotLedStatus=false;
  23. bool timerStatus=false;
  24.  
  25. void setup() {
  26.   Serial.begin(115200);
  27.   Serial.println("Start ...");
  28.   pinMode(buttonPin,INPUT_PULLUP);
  29.   pinMode(signalLedPin,OUTPUT);
  30.   pinMode(pilotLedPin,OUTPUT);
  31.   pinMode(relayPin,OUTPUT);
  32. }
  33.  
  34. void loop() {
  35.   currentMillis = millis();
  36.   if (currentMillis - previousMillis >= 500 and timerStatus==true) {
  37.     previousMillis = currentMillis;
  38.     flash();
  39.   }
  40.   if (currentMillis - previousMillisTimer >= delayTime*1000 and timerStatus==true) {
  41.     previousMillisTimer = currentMillis;
  42.     timerStatus=false;
  43.     digitalWrite(pilotLedPin,LOW);
  44.     digitalWrite(signalLedPin,HIGH);
  45.     digitalWrite(relayPin,HIGH);
  46.   }
  47.  
  48.   buttonCheck1();
  49.   if (buttonShort1==true and readingButton1==HIGH){
  50.     reading1=false;
  51.     buttonShort1=false;
  52.     Serial.println("Button Short Press");
  53.     digitalWrite(signalLedPin,LOW);
  54.     digitalWrite(relayPin,LOW);
  55.     if (timerStatus==false){
  56.       timerStatus=true;
  57.       previousMillisTimer=millis();;
  58.     } else {
  59.       timerStatus=false;
  60.       digitalWrite(pilotLedPin,LOW);
  61.     }
  62.   }
  63.   if (buttonLong1==true){
  64.     reading1=false;
  65.     buttonLong1=false;
  66.     Serial.println("Button Long Press");
  67.     timerStatus=false;
  68.     digitalWrite(pilotLedPin,LOW);
  69.     digitalWrite(signalLedPin,LOW);
  70.     digitalWrite(relayPin,LOW);
  71.   }
  72.  
  73. }
  74.  
  75. void flash(){
  76.   if (pilotLedStatus==false){
  77.     pilotLedStatus=true;
  78.     digitalWrite(pilotLedPin,HIGH);
  79.   } else {
  80.     pilotLedStatus=false;
  81.     digitalWrite(pilotLedPin,LOW);
  82.   }
  83. }
  84.  
  85. void buttonCheck1() {
  86.   readingButton1 = digitalRead(buttonPin);
  87.   if (readingButton1==LOW and reading1==true) {
  88.     if ((millis() - lastDebounceTime1) > debounceDelay) {
  89.       buttonShort1=true;
  90.     }
  91.     if ((millis() - lastDebounceTime1) > longPressDelay) {
  92.       buttonLong1=true;
  93.       buttonShort1=false;
  94.     }
  95.   }
  96.   if (readingButton1==HIGH) {
  97.     lastDebounceTime1=millis();
  98.     reading1=true;
  99.   }
  100. }
  101.  
  102.  
  103. // END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement