Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <TFT_ILI9163C.h>
- #include<CountUpDownTimer.h>
- #define buttonResetPin 2 // interrupt reset all data when reset pressed
- #define buton_menu_select 3
- #define buttonUp 4
- #define buttonDown 5
- // #LED_INDICATOR 7 // Led indicator boiler ON/OFF maybe 220V conected to the contactor
- #define relay 6
- #define __CS 10
- #define __DC 9
- #define __RST 8
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC, __RST);
- CountUpDownTimer T(DOWN);
- int Hours=0;
- int Min=0;
- const unsigned int add_min = 5; // add 5 min
- boolean buttonPress = false ,bt_state = false ,longPress = false ;
- long timerStart;
- const long delay_long_press = 500;
- const long delay_short_press = 270;
- long lastDebounceTime = 0;
- unsigned int select_time_place = 1; // select hours or minutes to adjust
- // interrupt function run only when the reset is activate
- void reset()
- {
- buttonPress = false;
- longPress = false;
- Hours = 0;
- Min = 0;
- bt_state = LOW;
- }
- void setup()
- {
- pinMode(buttonResetPin, INPUT_PULLUP);
- pinMode(buton_menu_select, INPUT_PULLUP);
- pinMode(buttonUp, INPUT_PULLUP);
- pinMode(buttonDown, INPUT_PULLUP);
- pinMode(relay, OUTPUT);
- // pinMode(LED_INDICATOR, OUTPUT);
- digitalWrite(relay,LOW);
- tft.begin();
- tft.clearScreen();
- tft.setRotation(3);
- tft.setTextSize(2);
- tft.setTextColor(GREEN);
- tft.setCursor(32,10);
- tft.print("Boiler");
- tft.setCursor(35,28);
- tft.print("Timer");
- T.SetTimer(0,0,0); //start at X minute (USE FOR: DOWN ONLY)
- attachInterrupt(0, reset, FALLING ); // FALLING trigger buton_menu_select when high to low
- }
- void loop()
- {
- T.Timer(); // run the timer
- check_button();
- tft_display(Hours,Min);
- run_timer();
- }
- void check_button()
- {
- if(digitalRead(buton_menu_select) == LOW)
- {
- if (buttonPress == false)
- {
- buttonPress = true ;
- timerStart = millis();
- }
- if((millis() - timerStart > delay_long_press) && (longPress == false)) // check for long press
- {
- longPress = true ;
- bt_state = !bt_state;
- }
- }
- else
- {
- if (buttonPress == true)
- {
- if (longPress == true)
- {
- longPress = false ;
- }
- else if(bt_state == true && buttonPress == true)
- {
- select_time_place++;
- if(select_time_place > 2)
- {
- select_time_place = 1 ;
- }
- }
- buttonPress = false ;
- }
- }
- if(bt_state == true)
- {
- adjust_timer(select_time_place);
- }
- }
- void adjust_timer(int menu_place)
- {
- switch(menu_place)
- {
- case 1:
- if ((millis() - lastDebounceTime) > delay_short_press) // debounce for one press
- {
- if (digitalRead(buttonUp) == LOW)
- {
- Hours++;
- if (Hours >= 4) {Hours=0;} // limit to 3 hours configuration
- }
- if (digitalRead(buttonDown) == LOW)
- {
- // Hours--;
- // if (Hours == 0) {Hours=3;} // block under 0
- // if (4 <= Hours && Hours == 0) {Hours=3;}
- if (Hours > 0) {Hours--; /*Hours=0;*/} // block under 0
- }
- lastDebounceTime = millis();
- }
- break;
- case 2:
- if ((millis() - lastDebounceTime) > delay_short_press) // debounce for one press
- {
- if (digitalRead(buttonUp) == LOW)
- {
- Min = Min + add_min;
- if (Min >= 60) {Min=0;}
- }
- if (digitalRead(buttonDown) == LOW)
- {
- Min = Min - add_min;
- if (Min <= 0) {Min=59;}
- }
- lastDebounceTime = millis();
- }
- break;
- }
- }
- void tft_display(int _Hours, int _Min)
- {
- if(bt_state == true && select_time_place == 1)
- {
- tft.setTextSize(2);
- tft.setTextColor(WHITE,BLACK);
- tft.setCursor(23,60);
- tft.print(Hours);
- tft.print(":");
- tft.print(Min);
- }
- if(bt_state == true && select_time_place == 2)
- {
- tft.setTextSize(2);
- tft.setTextColor(WHITE,BLACK);
- tft.setCursor(23,60);
- tft.print(Hours);
- tft.print(":");
- tft.print(Min);
- }
- }
- void run_timer()
- {
- if((0 < Hours && Hours >= 3) && (0 < Min && Min >= 59))
- {
- T.SetTimer(Hours,Min,0);
- T.StartTimer();
- }
- if (T.TimeHasChanged()) // this prevents the time from being constantly shown.
- {
- tft.setTextSize(2);
- tft.setTextColor(RED);
- tft.setCursor(23,60); //
- tft.fillRect(19,58,85,20,BLACK);
- tft.print(T.ShowHours());
- tft.print(":");
- tft.print(T.ShowMinutes());
- tft.print(":");
- tft.print(T.ShowSeconds());
- }
- if(T.ShowHours() > 0 || T.ShowMinutes() > 0 || T.ShowSeconds() > 0)
- {
- tft.setTextSize(2);
- tft.setTextColor(WHITE,BLACK);
- tft.setCursor(13,85);
- tft.print("relay on ");
- // digitalWrite(LED_INDICATOR,HIGH);
- }
- else
- {
- // tft.fillRect(13,85,106,16,BLACK);
- tft.setTextSize(2);
- tft.setTextColor(WHITE,BLACK);
- tft.setCursor(13,85);
- tft.print("relay off");
- // digitalWrite(LED_INDICATOR,LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement