Code not running from RTC

Hi

I have a KTA223 with an RTC I2C installed, supplied by Ocean Controls.

I am having trouble with my code reading time.

Any suggestions??

Thanks in advance

#include <Wire.h>
#include <Math.h>



#define RTC_ADDRESS 0x68

int current_day;
int doorA_opened;
int doorA_closed;
int doorB_opened;
int doorB_closed;
int sunrise; // in minutes from midnight
int sunset;  // in minutes from midnight

int close_A = 2;// Relay 1 is conncected to pin 2
int close_B = 3;// Relay 2 is conncected to pin 3
int open_A  = 4;// Relay 3 is conncected to pin 4
int open_B  = 5;// Relay 4 is conncected to pin 5

int second;
int minutex;
int hour;
int dayOfWeek;
int dayOfMonth;
int month;
int year;


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

  pinMode(close_A, OUTPUT);
  pinMode(close_B, OUTPUT);
  pinMode(open_A, OUTPUT);
  pinMode(open_B, OUTPUT);
  
  digitalWrite(open_A, LOW);
  digitalWrite(open_B, LOW);
  digitalWrite(close_A, LOW);
  digitalWrite(close_B, LOW);
  
  doorA_opened = 1;
  doorA_closed = 0;
  doorB_opened = 1;
  doorB_closed = 0;
  
  Wire.begin();
  ReadTime();
  int current_day = dayOfMonth;
  getRiseSet();
  
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void OpenDoorA()
{
  digitalWrite(open_A, HIGH);
  delay(25000);
  digitalWrite(open_A, LOW);
  doorA_opened = 1;
}

void CloseDoorA()
{
  digitalWrite(close_A, HIGH);
  delay(25000);
  digitalWrite(close_A, LOW);
  doorA_closed = 1;
}

void OpenDoorB()
{
  digitalWrite(open_B, HIGH);
  delay(25000);
  digitalWrite(open_B, LOW);
  doorB_opened = 1;
}

void CloseDoorB()
{
  digitalWrite(close_B, HIGH);
  delay(25000);
  digitalWrite(close_B, LOW);
  doorB_closed = 1;
}

void getRiseSet()
{
  sunrise = int(350.5 + (71.5*cos(((((month-1)*30.5)+dayOfMonth)+201)/58.1)));
  sunset = int(1083 + (68*sin(((((month-1)*30.5)+dayOfMonth)+90)/58.1)));
}

void NewDay()
{
  current_day = dayOfMonth;
  getRiseSet();
  doorA_opened = 0;
  doorA_closed = 0;
  doorB_opened = 0;
  doorB_closed = 0;
}

void ReadTime()
{
  Wire.beginTransmission(RTC_ADDRESS);   // Open I2C line in write mode
  Wire.send(0x00);                              // Set the register pointer to (0x00)
  Wire.endTransmission();                       // End Write Transmission 

 Wire.requestFrom(RTC_ADDRESS, 7);      // Open the I2C line in send mode

  second     = bcdToDec(Wire.receive() & 0x7f); // Read seven bytes of data
  minutex    = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());
}


void  loop()
{  
  delay(300000);
  ReadTime();
  if (current_day!=dayOfMonth)
  {
    NewDay();
  }
  
  int current_time = (hour*60)+minutex;
  
  if (current_time > (sunrise+30))
  {
    if (doorA_opened == 0)
    {
      OpenDoorA();
    }
  }
  

  
  if (current_time >= 720)
  {
    if (doorB_opened == 0)
    {
      OpenDoorB();
    }
  }
  
  if (current_time > (sunset+45))
  {
    if (doorB_closed == 0)
    {
      CloseDoorB();
    }
  }
  
  
  if (current_time > (sunset+55))
  {
    if (doorA_closed == 0)
    {
      CloseDoorA();
    }
  }
}

Moderator edit: Code put into code box. AWOL

feebs: I am having trouble with my code reading time.

Any suggestions??

First, edit your post, select the code part, and then hit the "#" button to put it inside [ code ] tags. That way it is more readable.

Now, when you say you are "having trouble" what do you mean? The time is wrong? The code doesn't compile? It does something other than what you expect? What do you expect and what does it do?

Thanks for your input Nick.

  1. Clock is set correctly
  2. Code compiles
  3. If I remove any reference to time & just have doors open & close on a delay setting, doors open & close as programmed.
  4. With the RiseSet & get time functions (as shown below) my KTA223 doesn’t respond as I desire ie open & close doors. It just “sits there idle”.
  5. The clock fitted is “SparkFun designed module for the DS1307 Real Time Clock”

Any suggestions would be appreciated.

Thanks

#include <Wire.h>
#include <Math.h>



#define RTC_ADDRESS 0x68

int current_day;
int doorA_opened;
int doorA_closed;
int doorB_opened;
int doorB_closed;
int sunrise; // in minutes from midnight
int sunset;  // in minutes from midnight

int close_A = 2;// Relay 1 is conncected to pin 2
int close_B = 3;// Relay 2 is conncected to pin 3
int open_A  = 4;// Relay 3 is conncected to pin 4
int open_B  = 5;// Relay 4 is conncected to pin 5

int second;
int minutex;
int hour;
int dayOfWeek;
int dayOfMonth;
int month;
int year;


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

  pinMode(close_A, OUTPUT);
  pinMode(close_B, OUTPUT);
  pinMode(open_A, OUTPUT);
  pinMode(open_B, OUTPUT);
  
  digitalWrite(open_A, LOW);
  digitalWrite(open_B, LOW);
  digitalWrite(close_A, LOW);
  digitalWrite(close_B, LOW);
  
  doorA_opened = 1;
  doorA_closed = 0;
  doorB_opened = 1;
  doorB_closed = 0;
  
  Wire.begin();
  ReadTime();
  int current_day = dayOfMonth;
  getRiseSet();
  
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

void OpenDoorA()
{
  digitalWrite(open_A, HIGH);
  delay(25000);
  digitalWrite(open_A, LOW);
  doorA_opened = 1;
}

void CloseDoorA()
{
  digitalWrite(close_A, HIGH);
  delay(25000);
  digitalWrite(close_A, LOW);
  doorA_closed = 1;
}

void OpenDoorB()
{
  digitalWrite(open_B, HIGH);
  delay(25000);
  digitalWrite(open_B, LOW);
  doorB_opened = 1;
}

void CloseDoorB()
{
  digitalWrite(close_B, HIGH);
  delay(25000);
  digitalWrite(close_B, LOW);
  doorB_closed = 1;
}

void getRiseSet()
{
  sunrise = int(350.5 + (71.5*cos(((((month-1)*30.5)+dayOfMonth)+201)/58.1)));
  sunset = int(1083 + (68*sin(((((month-1)*30.5)+dayOfMonth)+90)/58.1)));
}

void NewDay()
{
  current_day = dayOfMonth;
  getRiseSet();
  doorA_opened = 0;
  doorA_closed = 0;
  doorB_opened = 0;
  doorB_closed = 0;
}

void ReadTime()
{
  Wire.beginTransmission(RTC_ADDRESS);   // Open I2C line in write mode
  Wire.send(0x00);                              // Set the register pointer to (0x00)
  Wire.endTransmission();                       // End Write Transmission 

 Wire.requestFrom(RTC_ADDRESS, 7);      // Open the I2C line in send mode

  second     = bcdToDec(Wire.receive() & 0x7f); // Read seven bytes of data
  minutex    = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());
}


void  loop()
{  
  delay(300000);
  ReadTime();
  if (current_day!=dayOfMonth)
  {
    NewDay();
  }
  
  int current_time = (hour*60)+minutex;
  
  if (current_time > (sunrise+30))
  {
    if (doorA_opened == 0)
    {
      OpenDoorA();
    }
  }
  

  
  if (current_time >= 720)
  {
    if (doorB_opened == 0)
    {
      OpenDoorB();
    }
  }
  
  if (current_time > (sunset+45))
  {
    if (doorB_closed == 0)
    {
      CloseDoorB();
    }
  }
  
  
  if (current_time > (sunset+55))
  {
    if (doorA_closed == 0)
    {
      CloseDoorA();
    }
  }
}

I'd start by examining the output from your debug prints.

This isn’t right:

void setup()
{
...

  Wire.begin();
  ReadTime();
  int current_day = dayOfMonth;
  getRiseSet();

}

Your variable current_day is a local one, not the global one you have further up.

I’m confused by this:

int doorA_opened;
int doorA_closed;

Are you conceiving of a situation where door A is neither open nor closed?

Wouldn’t it be simpler and less bug-prone to have:

bool doorA_open = false;

So false is closed, and true is open? Otherwise every time you set doorA_opened to true you have to set doorA_closed to false.