/*
****************************
**** LOOP STARTS HERE ****
****************************
*/
void loop(){
digitalClockDisplay();
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
Alarm.delay(1000); // wait one second between clock display
//time();
Serial.println("test");
settemp = EEPROM.read(1); // read the settemp on the eeprom
delay (250); // wait for the lcd to refresh every 250 milliseconds
}
void Repeats()
{
digitalClockDisplay();
Serial.println("Execute Routine");
execute();
}
void OnceOnly(){
execute();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.println();
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void execute() {
//Sonar();
//TimeStamp();
//Thermostat();
// *** Time Stamp ***
DateTime now = RTC.now();
static char DateandTime[20];
sprintf(DateandTime, "%02u/%02u/%02u,%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute());
Serial.println(DateandTime);
//lcd.setCursor(0,0);
//lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(DateandTime);
// *** Sonar ***
delay(100);
int uS = sonar.ping_median(10);
Distance = (uS / US_ROUNDTRIP_CM);
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print("Distance ");
lcd.print(Distance);
lcd.print("cm");
Serial.print("Sonar Measurement = ");
Serial.print(Distance);
Serial.println("cm");
//int Tank_Current = Distance;
float Tank_Current=(uS / US_ROUNDTRIP_CM);
float Tank_Volume=((Tank_Diameter/2) * (Tank_Diameter/2) * 3.142 * (Tank_Empty - Tank_Current));
// *** Thermostat Start ***
int tvalue = analogRead(tempPin); // make tvalue what ever we read on the tempPin
float temp = (tvalue / 9.31); // the math / conversion to temp
// lcd.setCursor (0,0); // set the cursor to 0,0
// lcd.print (temp); // Print the current temp in f
//// lcd.print ('C');
// // Serial.println (temp); // print the temp it the serial monitor
settemp = EEPROM.read(1); // read the settemp on the eeprom
delay (1000); // wait for the lcd to refresh every 250 milliseconds
// if // if we se the switch up pin reading on 1 or 5 volts
// (digitalRead(swtu)== 1 )
// {
// settemp ++; // add one to the settemp, the settemp is the ideal temperature for you
// }
// else{}// other wise do nothing
// if
// (digitalRead (swtd) == 1)// if we detect a 1 on the other switch pin
// {(settemp --);// subtract one fromm the settemp
// }
// else {}// else, do nothing
if (temp < settemp) // if the temperature is lower
{digitalWrite(Relay_2, RELAY_ON); // turn on the heater
}
else // if that doesn't happen, then turn the heater off
{digitalWrite(Relay_2, RELAY_OFF);
}
lcd.setCursor (0,1); // set the cursor to 0,0
lcd.print (" "); // Print set to and your ideal temperature in f
lcd.setCursor(0,1);
lcd.print ("Thermostat Set To "); // Print set to and your ideal temperature in f
lcd.print (settemp);
lcd.print ('C');
//Serial.println(settemp); // Print the settemp in the serial montior
EEPROM.write (1,settemp); /* write the most recent settemp in eeprom data stoage
so that if the power is disconnected, you settemp is saved!*/
// delay (250); // wait 250 milliseconds
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print("Temperature= ");
lcd.print(temp);
lcd.print("C");
Serial.print("Temperature = ");
Serial.print(temp);
Serial.println("C");
// *** Thermostat Finish ***
if
(Distance <= Tank_Full && Distance !=0 )
{
digitalWrite(Relay_1, RELAY_ON);
lcd.clear();
lcd.setCursor(0,3);
lcd.print("Tank Purging");
delay(300000);
// add one to the settemp, the settemp is the ideal temperature for you
digitalWrite(Relay_1, RELAY_OFF);
}
else{}// other wise do nothing
{
File datagas = SD.open("gaslog.txt", FILE_WRITE); // Open file for writing, remember to close it
// if the file is available, write to it:
if (datagas) {
DateTime now = RTC.now();
// log time
datagas.print(now.unixtime()); // seconds since 1/1/1970
datagas.print(",");
datagas.print(now.year(), DEC);
datagas.print("/");
datagas.print(now.month(), DEC);
datagas.print("/");
datagas.print(now.day(), DEC);
datagas.print(",");
datagas.print(now.hour(), DEC);
datagas.print(":");
datagas.print(now.minute(), DEC);
datagas.print(":");
datagas.print(now.second(), DEC);
datagas.print(',');
datagas.print(Tank_Current);
datagas.print(",");
datagas.print(Tank_Volume);
datagas.print(",");
datagas.println(temp);
datagas.close();
Serial.println("Gas Data written to gaslog.txt");
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening gaslog.txt");
}
}
}
/*
********************************
**** FUNCTIONS START HERE ****
********************************
*/
// *** LCD Title Start ***
void LCDtitle() {
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" FARM GAS POWER");
Serial.println("Farm Gas Power");
delay(1000);
lcd.setCursor(0,1);
lcd.print(" Test Digester");
Serial.println("Test Digester");
delay(1000);
lcd.setCursor(0,2);
lcd.print(" Energy in Motion");
Serial.println("Energy in Motion");
delay(1000);
lcd.setCursor(0,3);
lcd.print(" Tel: 01264 850159");
Serial.println("Tel: 01264 850159");
delay(1000);
// // scroll 13 positions (string length) to the left
// // to move it offscreen left:
// for (int positionCounter = 0; positionCounter < 19; positionCounter++) {
// // scroll one position left:
// lcd.scrollDisplayLeft();
// // wait a bit:
// delay(150);
// }
//
// // scroll 31 positions (string length + display length) to the right
// // to move it offscreen right:
// for (int positionCounter = 0; positionCounter < 39; positionCounter++) {
// // scroll one position right:
// lcd.scrollDisplayRight();
// // wait a bit:
// delay(150);
// }
//
// // scroll 16 positions (display length + string length) to the left
// // to move it back to center:
// for (int positionCounter = 0; positionCounter < 20; positionCounter++) {
// // scroll one position left:
// lcd.scrollDisplayLeft();
// // wait a bit:
// delay(150);
// }
}
// *** LCD Title End ***