Relay8 & RTC possible conflict

Got comments, questions, or tips about any of the shields listed on ShieldList.org? Post them here!
Post Reply
hillside
Posts: 3
Joined: Wed Sep 17, 2014 11:23 am

Relay8 & RTC possible conflict

Post by hillside » Wed Sep 17, 2014 12:18 pm

Hi,
I am trying to get a relay8 shield and the RTC module working together on an EtherTen to operate as an 8 channel time clock. I can get demo code to work with the Relay8 and RTC but not together. I have posted below the code I have so far. When run the time and date displayed from the RTC are Date: 20000101 Time: 0 and do not advance and I haven't tried to manipulate the relay8 outputs.

I am assuming there is a conflict between the SoftI2C, DS3232RTC and Wire libraries as both of these devices operate on the I2C bus.

Any help offered would be greatly appreciated.

Thanks
Mark

Code: Select all

#include <SoftI2C.h> // initialise required libraries
#include <DS3232RTC.h>
#include "Wire.h"
#include <elapsedMillis.h>

SoftI2C i2c(A4, A5); // assign pins to SDA and SCL

DS3232RTC rtc(i2c);

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed

elapsedMillis timer0;
#define LOG_INTERVAL 60000    // the interval in mS
#define ERR_INTERVAL 100

#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()

#define CHIP_SELECT 10    // for the data logging shield, we use digital pin 10 for the SD cs line

// the digital pins that connect to the LEDs
#define ERRORled 5    // Red
#define SAMPLEled 4    // Green

void setup(void) {
    Serial.begin(9600);
    Serial.println("TimeClock1.ino");

#if WAIT_TO_START
    Serial.println("Type any character to start");
    while (! Serial.available());
#endif //WAIT_TO_START

    pinMode(ERRORled, OUTPUT);
    pinMode(SAMPLEled, OUTPUT);

    Wire.begin(); // Wake up I2C bus

    // Set I/O bank A to outputs
    Wire.beginTransmission(I2C_ADDR);
    Wire.write(0x00); // IODIRA register
    Wire.write(0x00); // Set all of bank A to outputs
    Wire.endTransmission();    

    timer0 = (LOG_INTERVAL); // clear the timer at the end of startup
}

void loop(void) {
    long dateNum;
    int timeNum;
    int mapVal;

    if (timer0 > LOG_INTERVAL) {
        timer0 -= LOG_INTERVAL; //reset the timer

        digitalWrite(SAMPLEled, HIGH);

        dateNum = GetDateNum();
        timeNum = GetTimeNum();

        Serial.print("Date: ");
        Serial.println(dateNum, DEC);
        Serial.print("Time: ");
        Serial.println(timeNum, DEC);

        if (timeNum >= 1700 && timeNum <= 2359) {
            Serial.print("For timeNum = ");
            Serial.print(timeNum, DEC);
        }

        digitalWrite(SAMPLEled, LOW);
    }
}

void error(char *str) {
    int ledState;

    Serial.println("");
    Serial.print("error: ");
    Serial.println(str);

    timer0 = 0;
    digitalWrite(ERRORled, HIGH);
    while(1) {
        if (timer0 > ERR_INTERVAL) {
            timer0 -= ERR_INTERVAL; //reset the timer
            ledState = digitalRead(ERRORled);    // read the current state and write the opposite
            digitalWrite(ERRORled, ! ledState);
        }
    };
}

int GetTimeNum(void) {
    RTCTime time;
    int timeNum = 0;

    // Read the current time.
    rtc.readTime(&time);

    timeNum = time.hour * 100;
    timeNum += time.minute;

    return timeNum;
}

long GetDateNum(void) {
    RTCDate date;
    long dateNum = 0L;

    rtc.readDate(&date);

    dateNum = date.year;
    dateNum = dateNum * 10000L;
    dateNum += date.month * 100L;
    dateNum += date.day;

    return dateNum;
}

//... End of File ..................................................................................
//... File: TimeClock1.ino
//..................................................................................................

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Relay8 & RTC possible conflict

Post by andrew » Wed Sep 17, 2014 9:23 pm

When you run the demo code to use either the Relay8 or the RTC - are they still both electricallly connected to the EtherTen?

hillside
Posts: 3
Joined: Wed Sep 17, 2014 11:23 am

Re: Relay8 & RTC possible conflict

Post by hillside » Wed Sep 17, 2014 9:44 pm

Hi John
Thanks for the reply. Yes, both are electrically connected when running either of the demo code. I have mounted the RTC module on a protoshield which is stacked above the relay8.

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Relay8 & RTC possible conflict

Post by angusgr » Fri Sep 19, 2014 12:38 am

Hi Mark,

I think the easiest solution might be to remove Wire and try using SoftI2C for everything. You should be able to change this block:

Code: Select all

    Wire.begin(); // Wake up I2C bus

    // Set I/O bank A to outputs
    Wire.beginTransmission(I2C_ADDR);
    Wire.write(0x00); // IODIRA register
    Wire.write(0x00); // Set all of bank A to outputs
    Wire.endTransmission();   
To this:

Code: Select all

    // Set I/O bank A to outputs
    i2c.startWrite(I2C_ADDR);
    i2c.write(0x00); // IODIRA register
    i2c.write(0x00); // Set all of bank A to outputs
    i2c.endWrite();   
... and so on for any other interactions you make with the Relay8.

Please let us know if that helps.


Angus

hillside
Posts: 3
Joined: Wed Sep 17, 2014 11:23 am

Re: Relay8 & RTC possible conflict

Post by hillside » Sat Sep 20, 2014 7:29 am

Thanks Angus, that worked, I can now read and use the date and time from the RTC to alter the relay8 outputs. I was going to try using the direct I2C commands with the RTC but this looks to be an easier method.

Mark

Post Reply