noam76

Long Press Buton

Jan 2nd, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #define button 3
  2. #define buttonUp 4
  3. #define buttonDown 5
  4.  
  5. #define relay 8
  6.  
  7. int Hours=0;
  8. int Min=0;
  9.  
  10. boolean buttonPress = false ,bt_state = false ,longPress = false ;
  11. long timerStart;
  12. const long delay_long_press = 500;
  13. const long delay_short_press = 270;
  14. long lastDebounceTime = 0;
  15.  
  16. int x =0;
  17. void setup()
  18. {
  19.  // Serial.begin(9600);
  20.  pinMode(button,INPUT);
  21.  pinMode(buttonUp,INPUT);
  22.  pinMode(buttonDown,INPUT);
  23.  pinMode(relay,OUTPUT);
  24. }
  25.  
  26. void loop()
  27. {
  28.  if(digitalRead(button) == HIGH)
  29.  {
  30.   if (buttonPress == false)
  31.   {
  32.     buttonPress = true ;
  33.     timerStart = millis();
  34.   }
  35.   if((millis() - timerStart > delay_long_press) && (longPress == false)) // check for long press
  36.   {
  37.     longPress = true ;
  38.     bt_state = !bt_state;
  39.   }
  40.  }
  41.  else
  42.  {
  43.   if (buttonPress == true)
  44.   {
  45.    if (longPress == true)
  46.    {
  47.     longPress = false ;
  48.    }
  49.    buttonPress = false ;
  50.   }
  51.  }
  52.  
  53.  if(bt_state == true)
  54.  {
  55.   if ((millis() - lastDebounceTime) > delay_short_press) // debounce for one press
  56.   {
  57.    if (digitalRead(button) == HIGH && buttonPress == true)
  58.    {
  59.     x++;
  60.     if(x > 2)
  61.     {
  62.      x = 0 ;
  63.     }
  64.    }
  65.    adjust_time(x);
  66.    lastDebounceTime = millis();
  67.   }
  68.  }
  69. }
  70.  
  71. void adjust_time(int menu_place)
  72. {
  73.  switch(menu_place)
  74.  {
  75.   case 1:
  76.    if (digitalRead(buttonUp) == HIGH)
  77.    {
  78.     Hours++;
  79.    }
  80.    if (digitalRead(buttonDown) == HIGH)
  81.    {
  82.     Hours--;
  83.    }
  84.    break;
  85.    
  86.   case 2:
  87.    if (digitalRead(buttonUp) == HIGH)
  88.    {
  89.     Min++;
  90.    }
  91.    if (digitalRead(buttonDown) == HIGH)
  92.    {
  93.     Min--;
  94.    }
  95.    break;
  96.  }
  97. }
Add Comment
Please, Sign In to add comment