Advertisement
noam76

CountDown + TFT

Jan 6th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <TFT_ILI9163C.h>
  4. #include<CountUpDownTimer.h>
  5. #define buttonResetPin 2 // interrupt reset all data when reset pressed
  6.  
  7. #define buton_menu_select 3
  8. #define buttonUp 4
  9. #define buttonDown 5
  10.  
  11. // #LED_INDICATOR 7 // Led indicator boiler ON/OFF maybe 220V conected to the contactor
  12. #define relay 6
  13.  
  14. #define __CS  10
  15. #define __DC  9
  16. #define __RST 8
  17.  
  18. #define BLACK   0x0000
  19. #define BLUE    0x001F
  20. #define RED     0xF800
  21. #define GREEN   0x07E0
  22. #define CYAN    0x07FF
  23. #define MAGENTA 0xF81F
  24. #define YELLOW  0xFFE0
  25. #define WHITE   0xFFFF
  26.  
  27. TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);
  28.  
  29. CountUpDownTimer T(DOWN);
  30.  
  31. int Hours=0;
  32. int Min=0;
  33. const unsigned int add_min = 5; // add 5 min
  34.  
  35. boolean buttonPress = false ,bt_state = false ,longPress = false ;
  36. long timerStart;
  37. const long delay_long_press = 500;
  38. const long delay_short_press = 270;
  39. long lastDebounceTime = 0;
  40.  
  41. unsigned int select_time_place = 1; // select hours or minutes to adjust
  42.  
  43. // interrupt function run only when the reset is activate
  44. void reset()
  45. {
  46.  buttonPress = false;
  47.  longPress = false;
  48.  Hours = 0;
  49.  Min = 0;
  50.  bt_state = LOW;
  51. }
  52.  
  53. void setup()
  54. {
  55.  pinMode(buttonResetPin, INPUT_PULLUP);
  56.  pinMode(buton_menu_select, INPUT_PULLUP);
  57.  pinMode(buttonUp, INPUT_PULLUP);
  58.  pinMode(buttonDown, INPUT_PULLUP);
  59.  pinMode(relay, OUTPUT);
  60.  // pinMode(LED_INDICATOR, OUTPUT);
  61.  digitalWrite(relay,LOW);
  62.  tft.begin();
  63.  tft.clearScreen();
  64.  tft.setRotation(3);
  65.  tft.setTextSize(2);
  66.  tft.setTextColor(GREEN);
  67.  tft.setCursor(32,10);
  68.  tft.print("Boiler");
  69.  tft.setCursor(35,28);
  70.  tft.print("Timer");
  71.  T.SetTimer(0,0,0); //start at X minute (USE FOR: DOWN ONLY)
  72.  
  73.  attachInterrupt(0, reset, FALLING ); // FALLING  trigger buton_menu_select when high to low
  74. }
  75.  
  76. void loop()
  77. {
  78.  T.Timer(); // run the timer
  79.  check_button();
  80.  tft_display(Hours,Min);
  81.  run_timer();
  82. }
  83.  
  84. void check_button()
  85. {
  86.  if(digitalRead(buton_menu_select) == LOW)
  87.  {
  88.   if (buttonPress == false)
  89.   {
  90.     buttonPress = true ;
  91.     timerStart = millis();
  92.   }
  93.   if((millis() - timerStart > delay_long_press) && (longPress == false)) // check for long press
  94.   {
  95.     longPress = true ;
  96.     bt_state = !bt_state;
  97.   }
  98.  }
  99.  else
  100.  {
  101.   if (buttonPress == true)
  102.   {
  103.    if (longPress == true)
  104.    {
  105.     longPress = false ;
  106.    }
  107.    
  108.    
  109.    else if(bt_state == true && buttonPress == true)
  110.    {
  111.     select_time_place++;
  112.     if(select_time_place > 2)
  113.     {
  114.      select_time_place = 1 ;
  115.     }
  116.    }
  117.    buttonPress = false ;
  118.   }
  119.  }
  120.  if(bt_state == true)
  121.  {
  122.    adjust_timer(select_time_place);
  123.  }
  124. }
  125.  
  126. void adjust_timer(int menu_place)
  127. {
  128.  switch(menu_place)
  129.  {
  130.   case 1:
  131.    if ((millis() - lastDebounceTime) > delay_short_press) // debounce for one press
  132.    {
  133.    if (digitalRead(buttonUp) == LOW)
  134.    {
  135.     Hours++;
  136.     if (Hours >= 4) {Hours=0;} // limit to 3 hours configuration
  137.    }
  138.    if (digitalRead(buttonDown) == LOW)
  139.    {
  140.     // Hours--;
  141.     // if (Hours == 0) {Hours=3;} // block under 0
  142.     // if (4 <= Hours && Hours == 0) {Hours=3;}
  143.     if (Hours > 0) {Hours--; /*Hours=0;*/} // block under 0
  144.    }
  145.    lastDebounceTime = millis();
  146.   }
  147.    break;
  148.    
  149.   case 2:
  150.    if ((millis() - lastDebounceTime) > delay_short_press) // debounce for one press
  151.    {
  152.    if (digitalRead(buttonUp) == LOW)
  153.    {
  154.     Min = Min + add_min;
  155.     if (Min >= 60) {Min=0;}
  156.    }
  157.    if (digitalRead(buttonDown) == LOW)
  158.    {
  159.     Min = Min - add_min;
  160.     if (Min <= 0) {Min=59;}
  161.    }
  162.    lastDebounceTime = millis();
  163.   }
  164.   break;
  165.  }
  166. }
  167.  
  168. void tft_display(int _Hours, int _Min)
  169. {
  170.  if(bt_state == true && select_time_place == 1)
  171.  {
  172.   tft.setTextSize(2);
  173.   tft.setTextColor(WHITE,BLACK);
  174.   tft.setCursor(23,60);
  175.   tft.print(Hours);
  176.   tft.print(":");
  177.   tft.print(Min);
  178.  }
  179.  if(bt_state == true && select_time_place == 2)
  180.  {
  181.   tft.setTextSize(2);
  182.   tft.setTextColor(WHITE,BLACK);
  183.   tft.setCursor(23,60);
  184.   tft.print(Hours);
  185.   tft.print(":");
  186.   tft.print(Min);
  187.  }
  188. }
  189.  
  190. void run_timer()
  191. {
  192.  if((0 < Hours && Hours >= 3) && (0 < Min && Min >= 59))
  193.  {
  194.   T.SetTimer(Hours,Min,0);
  195.   T.StartTimer();
  196.  }
  197.  if (T.TimeHasChanged()) // this prevents the time from being constantly shown.
  198.  {
  199.   tft.setTextSize(2);
  200.   tft.setTextColor(RED);
  201.   tft.setCursor(23,60); //
  202.   tft.fillRect(19,58,85,20,BLACK);
  203.   tft.print(T.ShowHours());
  204.   tft.print(":");
  205.   tft.print(T.ShowMinutes());
  206.   tft.print(":");
  207.   tft.print(T.ShowSeconds());
  208.  }
  209.  if(T.ShowHours() > 0 || T.ShowMinutes() > 0 || T.ShowSeconds() > 0)
  210.  {
  211.   tft.setTextSize(2);
  212.   tft.setTextColor(WHITE,BLACK);
  213.   tft.setCursor(13,85);
  214.   tft.print("relay on ");
  215.   // digitalWrite(LED_INDICATOR,HIGH);
  216.  }
  217.  else
  218.  {
  219.   // tft.fillRect(13,85,106,16,BLACK);
  220.   tft.setTextSize(2);
  221.   tft.setTextColor(WHITE,BLACK);
  222.   tft.setCursor(13,85);
  223.   tft.print("relay off");
  224.   // digitalWrite(LED_INDICATOR,LOW);
  225.  }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement