DS1307 RTC Help needed!!

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;
  
}

johnp90:
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!!

What is the problem with the code? It looks like you typed alot.

Chuck.

the problem is with the arduino not digital writing output 6 High when the tm.Hour is 13. is there a method perhaps to control an output based on a value being printed to my serial monitor?

Please edit your post and surround the code with code tags ("</>" button).

the problem is with the arduino not digital writing output 6 High when the tm.Hour is 13.

if (switchState1 == HIGH || tm.Hour == 13){ //relay1
  digitalWrite (6, HIGH);
 }

I don't see a problem here. Put serial.print(tm.Hour) just before this statement to make sure.

johnp90:
the problem is with the arduino not digital writing output 6 High when the tm.Hour is 13. is there a method perhaps to control an output based on a value being printed to my serial monitor?

your RTCC could be in AM/PM mode, try this code sequence to test it. this sketch will change the config
from AM/PM to 24hr every reset. It will also start the clock if the clock is halted.

#include <Streaming.h>
#include <Wire.h>

void checkRtcc( bool chg){
Wire.beginTransmission(0x68); // rtcc address
Wire.write((uint8_t)0);
Wire.endTransmission(true);
Wire.requestFrom(0x68,8);
uint8_t b[8], i=0;
while(i <8){
  b[i++]=Wire.read();
  }
 
Serial << (b[0]&0x80 ? "Clock Stopped " : "Clock Running ");
Serial << "Current Time ";
Serial << (b[2]&0x40 ? ( b[2]&32 ? "PM, 12 Hour " : "AM, 12 Hour") : "24 Hour ");
Serial << (b[2]&0x40 ? _HEX(b[2]&31) : _HEX(b[2]&63));
Serial << ":" << _HEX(b[1]) << ":" << _HEX(b[0]&127) << endl;
if(b[0]&0x80){ //start clock
  Serial << " Starting clock" << endl;
  Wire.beginTransmission(0x68);
  Wire.write((uint8_t)0);
  Wire.write((uint8_t)0);
  Wire.endTransmission(true);
  }
if(chg){ // change for 12 to 24 to 12
  uint8_t hr;
  if(b[2]&0x40){ //12 hour
    if(b[2]&0x20){ // pm
    hr=12+(b[2]&0x0f)+(b[2]&0x10 ? 10 : 0);
      if(hr==24) hr = 12; //noon
      
      b[2]=((hr/10)<<4)+(hr%10);
      }
    else {
      if((b[2]&0x1F)==0x12) b[2]=0; // 12am is just after midnight 0 o'clock
      b[2]=b[2]&0x1f;
      }
    }
  else { // 24 hr, switch to 12
    hr = ((b[2]&0x30)>>4)*10+(b[2]&0x0f);
    if(hr>=12){
      hr = hr -12;
      if(hr==0) hr =12; // 12 am/pm 
      b[2]=((hr/10)<<4)+(hr%10) | 0x20;
      }
    else{
      if(hr==0) b[2] = 0x12; // 12 Am
      else b[2] = ((hr/10)<<4)+(hr%10);
      }
    b[2] = b[2] | 0x40; // 12 hour mode
    }  
  
  Wire.beginTransmission(0x68);
  Wire.write((uint8_t)2);
  Wire.write((uint8_t)b[2]);
  Wire.endTransmission(true);
  }
}

void setup(){
Wire.begin();
Serial.begin(9600);

checkRtcc(true);
}

void loop(){
delay(1000);
checkRtcc(false);
}

Chuck.

switchState1 = digitalRead (10);
if (switchState1 == HIGH || tm.Hour == 13){ //relay1
  digitalWrite (6, HIGH);
 }

How is the switch wired and how is it set? Is it actually high at 13 hours? As jremington says confirm that both the switch and the time are correct with serial print statements.

Thanks for all the input guys! I was counteracting the statement to make output 6 high right below as switchstate 1 was low during testing which in turn made output 6 low. My mistake thanks for the help!