Hey Guys,
New to the forums here! I am attempting to manipulate a relay to switch on LED lighting based on the time that my DS1307 is providing to the arduino. Here is the code I currently have with where I am trying to manipulate highlighted.Any help you are able to provide is much appreciated!!
//developed by John Pleskina & Kyle Patterson, Using code derived from other users which include:
//data from brainybits ds1307 RTC code, and Digital DS18s20 temp sensor code from bildr.com
#include <OneWire.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;
int switchState4 = 0;
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
pinMode(10, INPUT); //left left switch
pinMode(11, INPUT); //left switch
pinMode(12, INPUT); //right switch
pinMode(13, INPUT); //right right switch
pinMode(6, OUTPUT); //Light
pinMode(7, OUTPUT); //Filter
pinMode(8, OUTPUT); //Pump
pinMode(9, OUTPUT); //Heater
pinMode(4, OUTPUT); //Fan
pinMode(3, OUTPUT); //Alarm
}
void loop(void) {
float temperature = getTemp();
Serial.println("AquariumTemperature:");
Serial.println(temperature);
Serial.println("Degrees Celsius");
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
}
delay(250); //just here to slow down the output so it is easier to read
switchState1 = digitalRead (10);
switchState2 = digitalRead (11);
switchState3 = digitalRead (12);
switchState4 = digitalRead (13);
if (temperature > 24.00){
digitalWrite (4,HIGH);
}
if (temperature <= 24.00){
digitalWrite (4,LOW);
}
[color=yellow]if (switchState1 == HIGH || tm.Hour == 13){ //relay1
digitalWrite (6, HIGH);
[/color] }
if (switchState1 == LOW) {
digitalWrite (6, LOW);
}
if (switchState2 == HIGH){ //relay 2
digitalWrite (7, HIGH);
}
if (switchState2 == LOW) {
digitalWrite (7, LOW);
}
if (switchState3 == HIGH){ //relay 3
digitalWrite (8, HIGH);
}
if (switchState3 == LOW) {
digitalWrite (8, LOW);
}
if (switchState4 == HIGH || temperature < 22.00){ //relay 4
digitalWrite (9, HIGH);
}
if (switchState4 == LOW && temperature >= 22.00) {
digitalWrite (9, LOW);
}
if (temperature >28){
analogWrite (3, 50);
delay(50);
analogWrite (3, 255);
}
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
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;
}
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;
}