mpmcws

LCD 16x2 PCF8574AT + RTC Tiny I2C

Sep 8th, 2018
224
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //LCD 16x2 PCF8574AT + RTC Tiny I2C V 1.3 set 2018 / Versione IDE 1.6.0
  2. //Cerani Marco Tolentino Cotesta
  3. #include <Wire.h>
  4. #include <RTClib.h>
  5. RTC_DS1307 RTC;
  6. #include <LiquidCrystal_I2C.h>
  7. #define DS1307_I2C_ADDRESS 0x68
  8. LiquidCrystal_I2C lcd(0x3F,16,2);
  9. char daysOfTheWeek[7][10] = {"Do", "Lu", "Ma", "Me", "Gio", "Ve", "Sa"};
  10. char bufferRX[50];
  11. void setup()
  12. {
  13.   Serial.begin(9600);
  14.   Serial.println("Per aggiornare data e ora inviare il seguente comando: ");
  15.   Serial.println("\"setDateTime anno, mese, giorno, ora, minuti, secondi\"");
  16.   Serial.println("17:30 del 8 sett 2018");
  17.   Serial.println("Esempio: setDateTime, 2018, 9, 8, 17, 30, 00");
  18.   lcd.init();
  19.   lcd.backlight();
  20. }
  21. void loop()
  22. {
  23.   static unsigned long updateTime;  
  24.   if(millis() - updateTime > 1000){
  25.     updateTime = millis();
  26.     DateTime now = RTC.now();
  27.     lcd.clear();
  28.     lcd.setCursor(0, 0);
  29.     LCDPrintNum(now.hour());
  30.     lcd.print(":");
  31.     LCDPrintNum(now.minute());
  32.     lcd.print(":");
  33.     LCDPrintNum(now.second());
  34.     lcd.print(" ");  
  35.     lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  36.     lcd.setCursor(0, 1);
  37.     LCDPrintNum(now.day());
  38.     lcd.print("/");
  39.     LCDPrintNum(now.month());
  40.     lcd.print("/");
  41.     lcd.print(now.year(), DEC);
  42.   }
  43.   readSerial();  
  44. }
  45. void LCDPrintNum(int num){
  46.   if(num <10)
  47.     lcd.print("0");
  48.   lcd.print(num);
  49. }
  50. void readSerial(){
  51.   while (Serial.available() > 0) {  
  52.     Serial.readBytesUntil('\n', bufferRX, sizeof(bufferRX) - 1);    
  53.     if(strstr(bufferRX, "setDateTime,")){          
  54.       int tdArray[6];  
  55.       char * pch = strtok (bufferRX, " ,");
  56.       unsigned int i = 0;
  57.       while (pch != NULL){          
  58.         if(i > 0)
  59.           tdArray[i-1] = atoi(pch);
  60.         pch = strtok (NULL, " ,");
  61.         i++;
  62.       }
  63.       DateTime newTime = DateTime(tdArray[0], tdArray[1], tdArray[2], tdArray[3], tdArray[4], tdArray[5]);
  64.       RTC.adjust(newTime);      
  65.       Serial.println("Impostato nuovo orario.");      
  66.     }
  67.   }
  68. }
RAW Paste Data