Salve a tutti, ho bisogno di aiuto. Mi sto costruendo un orologio che abbia sia l'ora UTC che l'ora locale, con un arduino nano, un DS3231 e oled 1306. Ho trovato uno sketch e ho cercato di adattarlo alle mie esigenze, ma sto avendo problemi con l'orario locale. Premetto che sono un novellino. Ora vi posto il mio sketch.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include <Timezone.h> // https://github.com/JChristensen/Timezone
// UTC
TimeChangeRule utcRule = {"UTC", Last, Sun, Mar, 1, 0}; // UTC
Timezone UTC(utcRule);
// Central European Time (Rome)
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time
Timezone CE(CEST, CET);
Timezone myTZ(CEST, CET);
// If TimeChangeRules are already stored in EEPROM, comment out the three
// lines above and uncomment the line below.
//Timezone myTZ(100); //assumes rules stored at EEPROM address 100
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato"};;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2023, 6, 12, 21, 19, 0));
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
display.clearDisplay();
display.display(); // this command will display all the data which is in buffer
display.setTextColor(WHITE, BLACK);
draw_text(0, 25, "Ora Loc.", 1);
}
void loop() {
time_t utc = now();
time_t local = myTZ.toLocal(utc, &tcr);
DateTime now = rtc.now();
/*============Display Date=================*/
display.setTextSize(1);
display.setCursor(0,0);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay = now.day();
thisMonth = now.month();
sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
display.setTextSize(1);
display.setCursor(62,0);
display.print(currentDate);
display.setTextSize(1);
display.setCursor(102,0);
display.print(now.year(), DEC);
/*================ORA UTC================*/
char buffer [16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();
sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
display.setTextSize(2);
display.setCursor(15,9);
display.print(buffer);
/*=============Ora Locale=====================*/
display.setTextSize(1);
display.setCursor(70,25);
display.print(local, tcr -> abbrev);
display.display();
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
display.display();
}
Non riesco ad implementare la libreria Timezone con il mio sketch.