x Datman: li ho messi x dare il tempo di scrivere/leggere al programma (ero alla disperazione!!) con l’idea di eliminarli a poco a poco eliminato l’errore,
Ecco l’ultima versione:
/*
Versione da provare
*/
#include <EEPROM.h>
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#include <TimeLib.h>
// SD
#include <SD.h>
#include <SPI.h>
//HC-12
#include <VirtualWire.h>
#include <SoftwareSerial.h>
// Creazione oggetto SD
File file;
// Crea il collegamento HC-12
SoftwareSerial HC12(3, 2); // RX, TX
#include <PCF8574_HD44780_I2C.h>
// Address 0x27, 20 chars, 4 line display
PCF8574_HD44780_I2C lcd(0x27, 20, 4);
// Definizioni variabili
String X ;
int t_max;
int t_min;
int t;
int h;
int t_max_old;
int t_min_old;
int oldmyday;
char c_newday[3];
char c_month[3];
char c_year[5];
char c_day[3];
char c_hour[3];
char c_minute[3];
char c_second[3];
char DataOraString[20];
char OraString[5];
int ctr_btt;
int ctr_day;
#define countof(a) (sizeof(a) / sizeof(a[0]))
void setup(void)
{
HC12.begin(9600);
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
lcd.init(); // LCD Initialization
lcd.backlight(); // Backlight ON
Serial.println (F("** Basato su RX_Prova_19 - Arduino UNO **"));
// leggo un valore sulla EEPROM
t_max_old = (EEPROM.read(1) - 50);
delay(500);
t_min_old = (EEPROM.read(2)- 50);
delay(500);
// Imposta la comunicazione SD
if (!SD.begin(10)) {
Serial.println("Errore SD");
}
lcd.clear();
t_max = (EEPROM.read(1) - 50);
delay (200);
t_min = (EEPROM.read(2) - 50);
delay (200);
// preparo la maschera per i valori di temp e umidità su LCD
lcd.setCursor(1, 1); // Set the cursor to col 0, row 1
lcd.print (F("Temp:"));
lcd.setCursor(8, 1); // Set the cursor to col 8, row 1
lcd.print (char(0xDF));
lcd.setCursor(9, 1);
lcd.print (F("C Umid:"));
lcd.setCursor(19, 1); // Set the cursor to col 19, row 1
lcd.print (F("%"));
}
void loop(void)
{
// Controlla la presenza della SD
int SD_OK;
if (!SD.begin(10)) {
Serial.println("SD non presente");
SD_OK = 0;
} else {
SD_OK = 1;
}
// imposta la comunicazione HC-12
if (HC12.available() ) {
X = HC12.readString();//read string
Serial.println(X);
int value = X.toInt();
ctr_btt = (value / 10000);
t = (((value - (ctr_btt * 10000)) / 100) - 49);
h = (((value - (ctr_btt * 10000)) - ((t + 49) * 100)) / 1);
// Controlla temperatura max/min
if (t >= t_max) {
t_max = t;
EEPROM.write(1, (t_max + 50));
delay (200);
}
if (t_min >= t) {
t_min = t;
Serial.println (F("cambio EEPROM"));
EEPROM.write(2, (t_min + 50));
}
// scrivo la temperatura
if (t < 10) {
lcd.setCursor(7, 1); // Set the cursor to col 0, row 7
} else {
lcd.setCursor(6, 1); // Set the cursor to col 0, row 6
}
lcd.print (t);
// scrivo l'umidità
lcd.setCursor(17, 1); // Set the cursor to col 0, row 17
lcd.print (h);
// scrivo la temperatura max e min odierna o l'avviso di cambio batteria
lcd.setCursor(2, 2); // Set the cursor to col 2, row 2
if (ctr_btt < 2) {
lcd.print (F("Oggi - max.: "));
delay (50);
lcd.setCursor(16, 2); // Set the cursor to col 16, row 2
lcd.print (t_max);
delay (50);
lcd.setCursor(18, 2); // Set the cursor to col 18, row 2
lcd.print (char(0xDF));
delay (50);
lcd.setCursor(19, 2); // Set the cursor to col 19, row 2
lcd.print (F("C"));
delay (50);
lcd.setCursor(9, 3); // Set the cursor to col 9, row 3
lcd.print("min.: " );
delay (50);
lcd.setCursor(16, 3); // Set the cursor to col 16, row 3
lcd.print (t_min);
delay (50);
lcd.setCursor(18, 3); // Set the cursor to col 18, row 3
lcd.print (char(0xDF));
delay (50);
lcd.setCursor(19, 3); // Set the cursor to col 19, row 3
lcd.print (F("C"));
delay (50);
Serial.print("4 controllo t_min: ");
Serial.println(t_min);
} else {
lcd.print(F(" ATTENZIONE: Livello") );
lcd.setCursor(0, 3);
lcd.print(F("batteria < a 5 volt " ));
}
// trasformo l'ora in char per controllare l0ultima trasmissione
sprintf_P(OraString, PSTR("%02d:%02d"), (hour()), (minute()));
lcd.setCursor(0, 3); // Set the cursor to col 0, row 3
lcd.print(OraString);
// controllo se devo registrare su SD
if (SD_OK == 1) {
ctrwrite();
}
}
// trasformo la data/ora in char
sprintf_P(DataOraString, PSTR("%02d/%02d/%4d %02d:%02d:%02d"), (day()), (month()), (year()), (hour()), (minute()), (second()));
// Scrivo la data/ora su LCD
lcd.setCursor(0, 0);
lcd.print(DataOraString);
delay (1000);
}
void ctrwrite() {
oldmyday = EEPROM.read(0);
if (day() != oldmyday) {
writeSD();
EEPROM.write(0, day());
delay (10);
}
}
void writeSD()
{
Serial.println("Apro SD");
// controllo se il file esiste
if (SD.exists("Valori.txt"))
{
file = SD.open("Valori.txt", FILE_WRITE);
} else {
// lo creo e inserisco la testata
file = SD.open("Valori.txt", FILE_WRITE);
file.write("Data");
file.write("/");
file.write("temp. max");
file.write("/");
file.write("temp. min");
file.write("\n" ); // vado a capo per dati
}
delay (100);
int newday = (day() - 1);
// trasformo i vali della data in char
char DataString[11];
sprintf_P(DataString, PSTR("%02d.%02d.%4d"), (newday), (month()), (year()));
// scrivo la data
file.write(DataString);
file.write("/");
delay (100);
// scrivo la temperatura max registrata
char newt_max[3];
snprintf_P( newt_max,
countof(newt_max),
PSTR("%02u"),
(t_max));
file.write(newt_max);
file.write("/");
delay (100);
// scrivo la temperatura min registrata
char newt_min[3];
snprintf_P( newt_min,
countof(newt_min),
PSTR("%02u"),
(t_min));
file.write(newt_min);
file.write("/");
delay (100);
// vado a capo per altri dati
file.write("\n" );
delay (100);
file.close();
Serial.println("chiudo SD");
// aggiungo 50 °C per evitare negativi e inserisco i valori sulla EEPROM
EEPROM.write(1, (t_max + 50));
delay (100);
EEPROM.write(2, (t_min + 50));
delay (100);
// ripato con le temperatura max/min
t_max = t;
t_min = t;
}
è ancora da “pulire”. Penso inoltre di eliminare scritture/letture ridondanti su EEPROM e di suddividere ulteriormente il loop.
x Maurotec/Standardoil → aggiunto le parentesi, comunque anche prima compilava senza dare errori!
Saluti
Enrico