///FINALLY THE LOOP===========================================================
void loop ()
{
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//DS18B20=================================================
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// print out the data
Serial.println(sensors.getTempCByIndex(0));
//relay=================================================
if(sensors.getTempCByIndex(0)<19){
//turn on the radiator, the room is cold
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
Radiator=1; //this is used to datalog wheather the radiator is on or off
}
else if (sensors.getTempCByIndex(0)>21){
//turn off the radiator, the room is warm
digitalWrite(7, LOW);
digitalWrite(6, LOW);
Radiator=0;
}
Serial.print("Radiator is ");
Serial.println(Radiator);
//SD Card===============================
// This is datalogging the above sensors
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
//Save the date and time:
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print(',');
//Save the temp
dataFile.print(sensors.getTempCByIndex(0));
dataFile.print(',');
dataFile.println(Radiator);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
Serial.println();
Serial.println();
delay(30000);
}
//END of LOOP code
/-----( Declare User-written Functions )-----/
//none