SHARE
TWEET

kupa2

a guest Jan 19th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define BUTTON 2
  2. #define LED1 7
  3. #define LED2 8
  4. #define WAITING_TIME 1000
  5. #define MAX_TIME 100
  6.  
  7. bool bBTNstate;
  8. bool bPrevBTNstate;
  9. bool bFlag;
  10. bool bFlagReset;
  11. unsigned long ulPrevTime;
  12. unsigned long ulCurrentTime;
  13. unsigned long ulResetTime;
  14. byte status;
  15.  
  16. void setup() {
  17.   pinMode(BUTTON, INPUT_PULLUP);
  18.   pinMode(LED1, OUTPUT);
  19.   pinMode(LED2, OUTPUT);
  20.   bBTNstate = true;
  21.   bFlag = false;
  22.   bFlagReset = false;
  23.   bPrevBTNstate = bBTNstate;
  24.   status = 0;
  25.   ulCurrentTime = 0;
  26.   ulPrevTime = 0;
  27. }
  28.  
  29. void loop() {
  30.   ulCurrentTime = millis();
  31.   bBTNstate = digitalRead(BUTTON);
  32.  
  33.   if (bBTNstate && !bPrevBTNstate) // wykrycie kliknięcia
  34.   {
  35.     status++;
  36.     ulPrevTime = ulCurrentTime;
  37.     bFlag = true;
  38.   }
  39.  
  40.   if(bFlag && (ulCurrentTime - ulPrevTime) > MAX_TIME)
  41.   {
  42.     switch (status)
  43.     {
  44.     case 2: // dwa kliknięcia
  45.       digitalWrite(LED1, HIGH);
  46.       ulResetTime = ulCurrentTime;
  47.       bFlag = false;
  48.       bFlagReset = true;
  49.       break;
  50.  
  51.     case 3: //trzy kliknięcia
  52.       digitalWrite(LED2, HIGH);
  53.       ulResetTime = ulCurrentTime;
  54.       bFlag = false;
  55.       bFlagReset = true;
  56.       break;
  57.  
  58.     default: // reset licznika kliknięć
  59.       status = 0;
  60.       bFlag = false;
  61.       break;
  62.     }
  63.   }
  64.  
  65.   if(bFlagReset && (ulCurrentTime - ulResetTime) > WAITING_TIME) // reset po 1s
  66.   {
  67.     status = 0;
  68.     bFlagReset = false;
  69.     digitalWrite(LED1, LOW);
  70.     digitalWrite(LED2, LOW);
  71.   }
  72.  
  73.   bPrevBTNstate = bBTNstate;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top