trying to create aquarium controller for switching on lights. Tiny RTC works good, display time and temp, temperature controlled relay works. when I run this code the relay on the timer (pin 9) is always on.
#include <OneWireTempSensor.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>
int DS18S20_Pin = 7; //temp
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 RTC; //timer
OneWire ds(DS18S20_Pin); //temp
void setup() {
Wire.begin();
RTC.begin();
lcd.begin(20, 4);
lcd.print("Time :");
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Alarm.alarmRepeat(8,30,0, MorningAlarm); //create morning alarm
Alarm.alarmRepeat(15,0,0, EveAlarm); // create evening alarm
}
void loop() {
float temperature = getTemp();
float tempF = (temperature * 9.0)/ 5.0 + 32.0;
DateTime now = RTC.now();
lcd.setCursor(7, 0);
if ( now.hour() < 10) {
lcd.print("0");
}
lcd.print(now.hour(), DEC);
lcd.print(':');
if ( now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(':');
if ( now.second() < 10) {
lcd.print("0");
}
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Temp : ");
lcd.print(tempF);
lcd.print(" *F ");
lcd.setCursor(0, 2);
lcd.print("Temp : ");
lcd.print(temperature);
lcd.print(" *C ");
if ( tempF < 78) { //This line turns on pin 8 @78F
digitalWrite(8, HIGH);
}
if ( tempF > 77) { //This line turns off pin 8 @77F
digitalWrite(8, LOW);
}
}
void MorningAlarm(){
digitalWrite(9, HIGH); // should turn on pin 9
}
void EveAlarm(){
digitalWrite(9, LOW); // should turn off pin 9
}
float getTemp(){
//returns the temperature from one DS18S20 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;
}
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 = 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;
}