Ciao a tutti, sto cercando di sviluppare con arduino uno una automazione per il mio acquario ma mi sono imbattuto in un problema che non riesco a risolvere, ho installato il ds 3231 come orologio e vorrei comandare dei relè a tempo per accendere le plafoniere, non riesco a capire come creare una variabile "tempo" cosi da usare IF ed ELSE per dare on o off ai relativi relè, mi potete aiutare, sono nuovo di questo mondo ed ho alcune difficoltà, grazie e buon anno.
Devi creare due variabili, una che indica l'orario di accensione (timeOn) ed una che indica quello di spegnimento (timeOff).
Supponiamo che sia sufficiente fare riferimento alla sola ora senza considerare i minuti e che l'ora di accensione sia sempre inferiore a quella di spegnimento:
byte relePin = 4; // pin collegato alla scheda relè
byte timeON = 8; // ora di accensione (0-23)
byte timeOFF = 18; // ora di spegnimento (0-23)
Nel loop devi confrontare l'ora del RTC con quelle impostate:
if ((RTC.hour >= timeON) && (RTC.hour < timeOFF)) digitalWrite(relePin, LOW) // attiva relè
else digitalWrite(relePin, HIGH) // disattiva relè
Grazie mille poi ci provo.
Scusa se ti disturbo ancora, non riesco a capire come confrontare l'ora del rtc 3231 non trovo la variabile, provo a mettere tutto il stato, tu sicuramente sei di gran lunga più esperto di me e troverai la soluzione, scusa il disturbo.
//Stefano Bertoni project
//I2C pins are A4==SDA, A5==SCL
int led = 13; //led sistema
int ris = 6; // al relè comando riscaldatore riscaldatore
int ledriscaldatore = 11; //led riscaldatore on/off
byte plafoniera1 = 2; //al relè comando plafoniera 1
byte plafoniera2 = 3; //al relè comando plafoniera 2
byte plafoniera3 = 4; //al relè comando plafoniera 3
byte timeON = 17; // ora di accensione (0-23)
byte timeOFF = 22; // ora di spegnimento (0-23)
int DS18B20_Pin = 12;// sonda temperatura in acquario
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define BUTTON A0
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
OneWire ds(DS18B20_Pin);
#define DS3231_I2C_ADDRESS 104
// Enter T1124154091014; the code will read this and set the clock. See the code for full details.
byte seconds, minutes, hours, day, date, month, year;
char weekDay[4];
byte tMSB, tLSB;
float temp3231;
void setup()
{
pinMode(led, OUTPUT); //led funzionamento sistema
pinMode(ris, OUTPUT); //uscita al relè avvio riscaldatore
pinMode(ledriscaldatore, OUTPUT); //led avvio riscaldatore
pinMode(plafoniera1, OUTPUT); //accensione plafoniera 1
pinMode(plafoniera2, OUTPUT); //accensione plafoniera 2
pinMode(plafoniera3, OUTPUT); //accensione plafoniera 3
Serial.begin(9600);
Serial.println();
lcd.begin (20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
Wire.begin();
Serial.begin(9600);
}
void loop()
{
if ((RTC.hour >= timeON) && (RTC.hour < timeOFF)) digitalWrite(relePin, LOW) // attiva relè
else digitalWrite(relePin, HIGH) // disattiva relè
float temperature = getTemp();
Serial.println(temperature);
lcd.home (); // go home
lcd.print("Progetto__ArduH2Oino");
lcd.setCursor ( 0, 1 ); // go to the 2nd line
lcd.print(" by_Stefano_Bertoni");
lcd.setCursor ( 0, 2 ); // go to the third line
lcd.print("Temp.H2O:C ");
lcd.print(getTemp());
lcd.setCursor ( 0, 3 ); // go to the fourth line
lcd.print("Temp.ext:C ");
lcd.print(get3231Temp());
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
float getTemp()
{
//returns the temperature from one DS18B20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr))
{
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7])
{
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28)
{
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) // we need 9 bytes
{
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
if (getTemp() < 30)
{
digitalWrite(ris, HIGH);
digitalWrite(ledriscaldatore, HIGH);
}
else
{
digitalWrite(ris, LOW);
digitalWrite(ledriscaldatore, LOW);
}
watchConsole();
get3231Date();
Serial.print(weekDay);
Serial.print(", ");
Serial.print(date, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" - ");
Serial.print(hours, DEC);
Serial.print(":");
Serial.print(minutes, DEC);
Serial.print(":");
Serial.print(seconds, DEC);
Serial.print(" - Temp: ");
Serial.println(get3231Temp());
delay(1000);
}
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
void watchConsole()
{
if (Serial.available()) // Look for char in serial queue and process if found
{
if (Serial.read() == 84) //If command = "T" Set Date
{
set3231Date();
get3231Date();
Serial.println(" ");
}
}
}
void set3231Date()
{
//T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year)
//T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99)
//Example: 02-Feb-09 @ 19:57:11 for the 3rd day of the week -> T1157193020209
// T1124154091014
seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
minutes = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
hours = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
day = (byte) (Serial.read() - 48);
date = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
year = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0x00);
Wire.write(decToBcd(seconds));
Wire.write(decToBcd(minutes));
Wire.write(decToBcd(hours));
Wire.write(decToBcd(day));
Wire.write(decToBcd(date));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void get3231Date()
{
// send request to receive data starting at register 0
Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address
Wire.write(0x00); // start at register 0
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes
if(Wire.available())
{
seconds = Wire.read(); // get seconds
minutes = Wire.read(); // get minutes
hours = Wire.read(); // get hours
day = Wire.read();
date = Wire.read();
month = Wire.read(); //temp month
year = Wire.read();
seconds = (((seconds & B11110000) >> 4) * 10 + (seconds & B00001111)); // convert BCD to decimal
minutes = (((minutes & B11110000) >> 4) * 10 + (minutes & B00001111)); // convert BCD to decimal
hours = (((hours & B00110000) >> 4) * 10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hour mode)
day = (day & B00000111); // 1-7
date = (((date & B00110000) >> 4) * 10 + (date & B00001111)); // 1-31
month = (((month & B00010000) >> 4) * 10 + (month & B00001111)); //msb7 is century overflow
year = (((year & B11110000) >> 4) * 10 + (year & B00001111));
}
else
{
//oh noes, no data!
}
switch (day)
{
case 1:
strcpy(weekDay, "Dom");
break;
case 2:
strcpy(weekDay, "Lun");
break;
case 3:
strcpy(weekDay, "Mar");
break;
case 4:
strcpy(weekDay, "Mer");
break;
case 5:
strcpy(weekDay, "Gio");
break;
case 6:
strcpy(weekDay, "Ven");
break;
case 7:
strcpy(weekDay, "Sab");
break;
}
}
float get3231Temp()
{
//temp registers (11h-12h) get updated automatically every 64s
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 2);
if(Wire.available())
{
tMSB = Wire.read(); //2's complement int portion
tLSB = Wire.read(); //fraction portion
temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8
delay(1000);
}
else
{
//oh noes, no data!
}
return temp3231;
}