Buongiorno a tutti,
Sto cercando di controllare 2 rele con un RTC, voglio che il primo di accenda ad una certa ora (il secondo vediamo più avanti voglio impostare un timer che dopo x minuti dall'avvio del primo si deve accendere il secondo per x tempo).
Per ora, mi funzionano i rele, il sensore funziona, lo schermo funziona e l'RTC funziona.
MA, se mi stampa l'ora in Serial, non riesco a stampare sullo schermo, ne a utilizzare "hour" "minute" "second" "dayOfMonth" ecc altrove che li.
Ad esempio, nella riga evidenziata di giallo, mi sembra non aver sbagliato il codice ma evidentemente lo è perché mi dice che " 'hour' was not declared in this scope"
Perché?!
Grazie a chi vorrà aiutarmi.
(il mio sketch è fatto da più sketch trovati in internet e adattati al mio caso, non sono bravo abbastanza da scrivere da 0)
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
//inizio RTC *****************************
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
unsigned long previousMillisRTC = 0; // will store last time RTC was updated
const long interval = 1000; // interval at which to check RTC (milliseconds)
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(
byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
switch(dayOfWeek){
case 1:
Serial.print("Domenica ");
break;
case 2:
Serial.print("Lunedi ");
break;
case 3:
Serial.print("Martedi ");
break;
case 4:
Serial.print("Mercoledi ");
break;
case 5:
Serial.print("Giovedi ");
break;
case 6:
Serial.print("Venerdi ");
break;
case 7:
Serial.print("Sabato ");
break;
}
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.println(year, DEC);
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10)
{
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10)
{
Serial.print("0");
}
Serial.print(second, DEC);
Serial.println(" ");
}
//fine RTC *****************************
//Pompa
int pompa = 7; // Pump connected to digital pin 7
//unsigned long previousMillisPompa = 0; // will store last time POMPA was updated
//const long intervalPompa = 5000; // interval at which to check POMPA (milliseconds)
//Elettrovalvola
int valvola = 4; // Pump connected to digital pin 4
//millis per sensore orizzontale
unsigned long previousMillisSensore = 0; // will store last time Sensore was updated
//const long interval = 1000; // Uguale per RTC e Sensore
int lightPin = 0; // Analog Input LED Sensore
int threshold = 500; // 0 chiuso - 1023 aperto
//lcd2004
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
unsigned long previousMillisLCD = 0; // will store last time Sensore was updated
void setup()
{
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.setCursor(3,0); //Start at character 4 on line 0
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(40,12,16,3,25,04,17);
//Pompa
pinMode(pompa, OUTPUT); // sets the digital pin as output
//Elettrovalvola
pinMode(valvola, OUTPUT); // sets the digital pin as output
//LED Sensore orizzontale
pinMode(13, OUTPUT);
}
void loop()
{
//RTC
unsigned long currentMillisRTC = millis();
if (currentMillisRTC - previousMillisRTC >= interval) {
// save the last time you blinked the LED
previousMillisRTC = currentMillisRTC;
displayTime(); // display the real-time clock data on the Serial Monitor,
}
{
//lcd2004
unsigned long currentMillisLCD = millis();
if (currentMillisLCD - previousMillisLCD >= interval) {
// save the last time you controlled
previousMillisLCD = currentMillisLCD;
lcd.setCursor(3,0); //Start at character 4 on line 0
lcd.print("HELLO, WORLD!");
}
}
{
//Elettrovalvola
[color=yellow]while(hour == 7 && minute >= 00 && minute <= 02){[/color] <<------
digitalWrite(valvola, HIGH); // sets the elettrovalvola on
}
}
unsigned long currentMillisSensore = millis();
if (currentMillisSensore - previousMillisSensore >= interval) {
// save the last time you blinked the LED
previousMillisSensore = currentMillisSensore;
//Serial.println(analogRead(lightPin));
if(analogRead(lightPin) > threshold ) {
digitalWrite(13, HIGH); //LED pin 13
digitalWrite(pompa, HIGH); // sets the pump on
Serial.println("high");
lcd.setCursor(0,2);
lcd.print("Pompa aperta");
} else {
digitalWrite(13, LOW); //LED pin 13
digitalWrite(pompa, LOW); // sets the pump off
Serial.println("low");
lcd.setCursor(0,2);
lcd.print("Pompa chiusa");
}
}
}