Advertisement
noam76

Cowntdown Timer1

Dec 25th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <TFT_ILI9163C.h>
  3. #include<CountUpDownTimer.h>
  4.  
  5. #define buttonHoursPin 3
  6. #define buttonMinPin 4
  7. #define buttonStartPin 5
  8. #define relay 14
  9.  
  10. #define __CS  10
  11. #define __DC  9
  12. #define __RST 8
  13.  
  14. #define BLACK   0x0000
  15. #define BLUE    0x001F
  16. #define RED     0xF800
  17. #define GREEN   0x07E0
  18. #define CYAN    0x07FF
  19. #define MAGENTA 0xF81F
  20. #define YELLOW  0xFFE0
  21. #define WHITE   0xFFFF
  22.  
  23. TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);
  24.  
  25. CountUpDownTimer T(DOWN);
  26.  
  27. // debounce button timer
  28. long lastDebounceTime = 0;  // the last time the output pin was toggled
  29. const unsigned long debounceDelay = 250;    // the debounce time; increase if the output flickers
  30. const unsigned long interval = 1000;
  31. unsigned long previousInterval = 0;
  32.  
  33. unsigned int Hours = 0;
  34. unsigned int Min = 0;
  35. const unsigned int add_min = 5; // 5 min
  36.  
  37. void setup()
  38. {
  39.   Serial.begin(9600);
  40.   pinMode(buttonHoursPin, INPUT_PULLUP);
  41.   pinMode(buttonMinPin, INPUT_PULLUP);
  42.   pinMode(buttonStartPin, INPUT_PULLUP);
  43.   pinMode(relay, OUTPUT);
  44.   digitalWrite(relay,LOW);
  45.   tft.begin();
  46.   tft.setRotation(3);
  47.   tft.setTextSize(3);
  48.   tft.setTextColor(GREEN);
  49.   T.SetTimer(0,0,0); //start at X minute (USE FOR: DOWN ONLY)
  50. }
  51.  
  52. void loop()
  53. {
  54.  T.Timer(); // run the timer
  55.  if ( (millis() - lastDebounceTime) > debounceDelay)
  56.  {
  57.   if (digitalRead(buttonHoursPin) == LOW)
  58.   {
  59.    Hours++;
  60.    if (Hours >= 4) Hours=0;
  61.    Serial.println(Hours);
  62.    lastDebounceTime = millis();
  63.   }
  64.   if (digitalRead(buttonMinPin) == LOW)
  65.   {
  66.    Min = Min+add_min;
  67.    if (Min >= 60) Min=0;
  68.    Serial.println(Min);
  69.    lastDebounceTime = millis();
  70.   }
  71.   if (digitalRead(buttonStartPin) == LOW)
  72.   {
  73.    T.SetTimer(Hours,Min,20);
  74.    T.StartTimer();
  75.    lastDebounceTime = millis();
  76.   }
  77.  }
  78.   if (T.TimeHasChanged()) // this prevents the time from being constantly shown.
  79.   {
  80.     tft.setCursor(10,15);
  81.     tft.clearScreen();
  82.     tft.print(T.ShowHours());
  83.     tft.print(":");
  84.     tft.print(T.ShowMinutes());
  85.     tft.print(":");
  86.     tft.print(T.ShowSeconds());
  87.     // This DOES NOT format the time to 0:0x when seconds is less than 10.
  88.     // if you need to format the time to standard format, use the sprintf() function.
  89.   }
  90.  
  91.   unsigned long currentInterval = millis();
  92.   if (currentInterval - previousInterval >= interval)
  93.   {
  94.     previousInterval = currentInterval;
  95.     if(T.ShowHours() > 0 && T.ShowMinutes() > 0 && T.ShowSeconds() > 0)
  96.     {
  97.      Serial.print("relay on :");
  98.     }
  99.     else
  100.      {
  101.       Serial.print("relay off :");
  102.      }
  103.     /* if (T.TimeHasChanged())
  104.     {
  105.      Serial.print("time change :");
  106.      Serial.println(T.TimeHasChanged());
  107.     }
  108.     else
  109.     {
  110.      Serial.println("time not change");
  111.     } */
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement