Advertisement
Szerelo

Arduino Easy Timer 16*2 LCD

Jun 21st, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. // Timer program v.0.9 beta, Author: Szerzetes, Licenc: free
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h> // https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
  4. #include <Timer.h>
  5. #include <EEPROM.h>
  6.  
  7. Timer t;
  8.  
  9. #define KeySetting 10
  10. #define KeyPlus 11
  11. #define KeyMinus 12
  12. #define Relay 9       // The number of outputs that the timer switches
  13. //                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  14. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  15.  
  16. int Time,TimeKey;
  17. byte TimeLow,TimeHigh;
  18. void setup()
  19. {
  20.   pinMode(KeySetting,INPUT);
  21.   pinMode(KeyPlus,INPUT);
  22.   pinMode(KeyMinus,INPUT);
  23.   pinMode(Relay,OUTPUT);
  24.   digitalWrite(KeySetting,HIGH);
  25.   digitalWrite(KeyPlus,HIGH);
  26.   digitalWrite(KeyMinus,HIGH);
  27. // *********************************** Write here what happens when the timer is switched on *********************************
  28.   digitalWrite(Relay,HIGH);
  29. // *********************************** Relay output is set ***********************************
  30. lcd.begin(16,2);
  31.   TimeLow=EEPROM.read(0);
  32.   TimeHigh=EEPROM.read(1);
  33.   if (TimeHigh==0 and TimeLow==0) TimeLow=1;  // If EEPROM is empty, the time value will be 1 secundum
  34.   Time=TimeHigh*256+TimeLow;
  35. int tickEvent = t.every(1000, tikkel, 100 );
  36.   TimeKey=500;
  37. }
  38.  
  39. void loop() {
  40.   if (digitalRead(KeySetting)==1) {
  41.     t.update();
  42.   }
  43.   else {
  44.     lcd.clear();
  45.     lcd.print("Time setting: ");
  46.     TimeLow=EEPROM.read(0);
  47.     TimeHigh=EEPROM.read(1);
  48.     Time=TimeHigh*256+TimeLow;
  49.     while (digitalRead(KeySetting)==0) {
  50.       if (digitalRead(KeyPlus)==0 ) {
  51.         Time++;
  52.         if (Time>=600) Time=600;
  53.         delay(TimeKey);
  54.         if (TimeKey>=50) TimeKey-=30;
  55.         goto PushEnd;
  56.       } // endif
  57.       if (digitalRead(KeyMinus)==0 ) {
  58.         Time--;
  59.         if (Time<=0) Time=1;
  60.         delay(TimeKey);
  61.         if (TimeKey>=50) TimeKey-=30;
  62.         goto PushEnd;
  63.       }
  64.       TimeKey=500;
  65. PushEnd:    
  66.       lcd.setCursor(1,1);
  67.       TimePrintLCD();
  68.     }
  69.     lcd.setCursor(10,1);
  70.     lcd.print("Save");
  71.     TimeHigh=Time/256;
  72.     TimeLow=Time-256*TimeHigh;
  73.     EEPROM.write(0,TimeLow);
  74.     EEPROM.write(1,TimeHigh);
  75.     delay(2000);
  76.     lcd.clear();
  77.   }
  78. }
  79.  
  80. void tikkel() {
  81.   if (Time==0) {
  82.     lcd.setCursor(0,0);
  83.     lcd.print("Time over. ");
  84. // ************************************ Write here what happens when the timer is switched off. ***************************************    
  85.   digitalWrite(Relay,LOW);
  86. // *********************************** Relay output is reset ***********************************
  87.   }
  88.   else {
  89.     Time--;
  90.     lcd.setCursor(0,0);
  91.     lcd.print("Time: ");
  92.     TimePrintLCD();
  93.   }
  94. }
  95.  
  96. void TimePrintLCD() {
  97.   byte TMinute, TSecundum;
  98.   String tmp;
  99.   TMinute=Time/60;
  100.   TSecundum=Time-TMinute*60;
  101.   tmp=""; if (TMinute<=9) tmp= "0";
  102.   lcd.print(tmp);
  103.   lcd.print(TMinute);
  104.   lcd.print(":");
  105.   tmp=""; if (TSecundum<=9) tmp= "0";
  106.   lcd.print(tmp);
  107.   lcd.print(TSecundum);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement