Buongiorno, volevo condividere con voi un problema riscontrato con un progetto definitivo. Ho usato un Arduino UNO R4 WiFi, un RTC DS1307, un display TFT SPI e un relè a 5V.
Si tratta di un’irrigazione automatica e lì per lì funziona tutto, il problema sorge dopo qualche giorno, in cui l’orologio mi va “in palla” e inizia a segnare 00:00 oppure 00:22 è si ferma lì.
Se viene resettato, il tutto riparte senza problemi all’orario corrente, quindi l’ora viene mantenuta.
Allego il codice:
//Ultimo aggiornamento 01/08/2025
#include "icons.h"
//LIBRERIE
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_GenericDevice.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <RTClib.h>
#include <Wire.h>
//OGGETTI
RTC_DS1307 rtc;
#define PIN_POMPA 4
//VARIABILI
int hh, mm;
int terra;
int th_terra = 680;
bool TIMER;
bool POMPA;
bool stato_pompa = false;
//TIMERS
unsigned long t1, dt1;
unsigned long t2, dt2;
unsigned long t3, dt3;
unsigned long t4, dt4;
unsigned long ton_pompa;
//DISPLAY
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_MOSI 11
#define TFT_SCLK 13
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup() {
Serial.begin(9600);
delay(1500);
Serial.print("START");
if (!rtc.begin()) {
Serial.println("Verifica connessioni");
while(true);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(2, INPUT_PULLUP);
pinMode(PIN_POMPA, OUTPUT);
digitalWrite(PIN_POMPA, LOW);
inizializza_tft();
leggiOra();
leggiSensori();
}
void loop() {
dt1 = millis() -t1;
if (dt1 >= 1000) {
t1 = millis();
leggiSensori();
leggiOra();
}
dt2 = millis() - t2;
if (dt2 >= 8500) {
t2 = millis();
stampaValori();
Serial.print("timer =");
Serial.println(TIMER);
Serial.print("pompa =");
Serial.println(POMPA);
}
if (POMPA) {
dt4 = millis() - t4;
if (dt4 >= ton_pompa) {
t4 = millis();
stato_pompa = !stato_pompa;
}
if (stato_pompa) {
ton_pompa = 2000;
digitalWrite(PIN_POMPA, HIGH);
} else {
ton_pompa = 10000;
digitalWrite(PIN_POMPA, LOW);
}
}
}
void leggiOra() {
DateTime now = rtc.now();
hh = now.hour();
mm = now.minute();
String time = String(hh) + ":" + String(mm);
char TIME[5];
sprintf(TIME, "%02d:%02d",hh, mm);
Serial.println(TIME);
if ((hh <= 7) || (hh >= 18)) {
TIMER = true;
} else {
TIMER = false;
}
}
void leggiSensori() {
for (int i = 0; i < 10; i++) {
terra+= analogRead(A0);
}
terra = terra/10;
if (TIMER) {
if (terra >= th_terra) {
POMPA = true;
}else {
POMPA = false;
digitalWrite(PIN_POMPA, LOW);
}
}
}
void inizializza_tft() {
tft.init(240, 320);
tft.fillScreen(ST77XX_WHITE);
tft.setRotation(3);
tft.fillRect(0, 0, 120, 220, ST77XX_BLACK);
tft.drawBitmap(0, 0,image_data_icons, 120, 240, ST77XX_WHITE);
}
void stampaValori(){
tft.setTextSize(6);
tft.setTextColor(ST77XX_BLACK);
String time = String(hh) + ":" + String(mm);
char TIME[5];
sprintf(TIME, "%02d:%02d",hh, mm);
tft.fillRect(125, 0, 200, 240, ST77XX_WHITE);
tft.setCursor(130,150);
tft.print(TIME);
tft.setCursor(150, 40);
tft.setTextSize(6);
tft.print(terra);
}