Matrix clock RTC

Hi there,
I'm trying to make a nice clock for the master bedroom.
I got 4 rainbowdouino's with RGB matrixes on them.
it works great.

although I have no trouble trying to display the time of my "ladyada's log shield" over serial.

I want to display the time on the four matrixes so that every matrix just shows one number (only hours and minutes)

But i never worked with a RTC before so hoped to learn something here :stuck_out_tongue:

If possible i love to have the seconds to but i dont know how.
32x8 pixels is a lot so it should work

I'm able to output it on my LCD's and the serial monitotr but not on the 4 matrices and it really bugs me.

the real problem is that i don't understand how to write it to the display when the numbers are bigger then 9 because it will then take 2 matrices to show the value

the serial monitoris very simple because its as simple as taken from the official example

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

how do i get this "Serial.print(now.day(), DEC);" and split the digits if there are 2 :S so i can put in this

#include <Wire.h>

unsigned char RainbowCMD[5];
unsigned char State = 0;  
unsigned long timeout;


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master) 
}

void loop()
{
 ShowChar(4,DIGIT1,0,0,15,0);ShowChar(3,DIGIT2,0,0,15,0);ShowChar(2,DIGIT3,0,0,15,0);ShowChar(1,DIGIT4,0,0,15,0); // time NOT working
  delay(1000);
}





//--------------------------------------------------------------------------
//Name:ShowColor
//function: Send a conmand to Rainbowduino for showing a color
//parameter: Address: rainbowduino IIC address
//                 red,green,blue:  the color RGB    
//----------------------------------------------------------------------------
void ShowColor(int Address,unsigned char red , unsigned char green, unsigned char blue)
{
  RainbowCMD[0]='R';
  RainbowCMD[1]=0x03;
  RainbowCMD[2]=red;
  RainbowCMD[3]=((green<<4)|(blue));

   SentCMD(Address);
}


//--------------------------------------------------------------------------
//Name:ShowImage
//function: Send a conmand to Rainbowduino for showing a picture which was pre-set in Rainbowduino Flash
//parameter: Address: rainbowduino IIC address
//                 number:  the pre-set picture position
//                 shift: the picture  shift bit for display
//----------------------------------------------------------------------------
void ShowImage(int Address, unsigned char number,unsigned char shift)
{
    RainbowCMD[0]='R';
    RainbowCMD[1]=0x01;
    RainbowCMD[2]=(shift<<4);
    RainbowCMD[4]=number;
 
     SentCMD(Address);
}



//--------------------------------------------------------------------------
//Name:ShowColor
//function: Send a conmand to Rainbowduino for showing a color
//parameter: Address: rainbowduino IIC address
//                 red,green,blue:  the color RGB  
//                 shift: the picture  shift bit for display
//                 ASCII:the char or Number want to show
//----------------------------------------------------------------------------
void ShowChar(int  Address,unsigned char ASCII,unsigned char red, unsigned char blue ,unsigned char green,unsigned char shift)
{
    RainbowCMD[0]='R';
    RainbowCMD[1]=0x02;
    RainbowCMD[2]=((shift<<4)|(red));
    RainbowCMD[3]=((green<<4)|(blue));
    RainbowCMD[4]=ASCII;
       
     SentCMD(Address);
}




//--------------------------------------------------------------------------
//Name:SentCMD
//function: Send a 5 byet Rainbow conmand out 
//parameter: NC  
//----------------------------------------------------------------------------
void SentCMD(int  Add)
{   unsigned char OK=0;
     unsigned char i,temp;
 
    while(!OK)
  {                          
    switch (State)
    {       

    case 0:                          
      Wire.beginTransmission(Add);
      for (i=0;i<5;i++) Wire.send(RainbowCMD[i]);
      Wire.endTransmission();    
      delay(5);   
      State=1;                      
      break;

    case 1:

      Wire.requestFrom(Add,1);   
      if (Wire.available()>0) 
        temp=Wire.receive();    
      else {
        temp =0xFF;
        timeout++;
      }

      if ((temp==1)||(temp==2)) State=2;
      else if (temp==0) State=0;

      if (timeout>5000)
      {
        timeout=0;
        State=0;
      }

      delay(5);
      break;

    case 2:
      OK=1;
      State=0;
      break;

    default:
      State=0;
      break;

    }
  }
}

i really don't know :S

I don't know where the magic DIGIT1 to DIGIT4 are coming from, but lets assume they're the puzzle to solve. Add this code to the top of your loop() function (as usual untested, the problem isn't interesting enough to start the IDE):

DateTime now = RTC.now();
char DIGIT1 = '0' + now.hour() / 10;
char DIGIT2 = '0' + now.hour() % 10;
char DIGIT3 = '0' + now.minute() / 10;
char DIGIT4 = '0' + now.minute() % 10;

Korman

I will try it when I get home in about 9 hours :stuck_out_tongue:
the digit1 to 4 are just part of my explaination*

It works GREAT ! I really love Open Source :stuck_out_tongue:

thanks Korman.
I'll post my complete code soon :smiley:

finally got the time to post my clock code

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

unsigned char RainbowCMD[5];
unsigned char State = 0;  
unsigned long timeout;
long previousMillis = 0;
long interval = 5 * 1000; // elke 20 seconden verrandert de kleur.
int r_time = 1;
int g_time = 1;
int b_time = 1;

void setup()
{
  Wire.begin();
  RTC.begin();
  // RTC.adjust(DateTime(__DATE__, __TIME__)); // Correct time
  delay(5000);
  ShowColor(4,15,0,0); ShowColor(3,0,15,0); ShowColor(2,0,0,15); ShowColor(1,15,15,15);
  delay(5000);
}

void loop(){
  DateTime now = RTC.now();
  char time1 = '0' + now.hour() / 10; if(time1 == '0'){ ShowChar(4,'0',0, 0, 0,0); }
  char time2 = '0' + now.hour() % 10; ShowChar(3,time2, r_time, g_time, b_time,0);
  char time3 = '0' + now.minute() / 10; ShowChar(2,time3, r_time, g_time, b_time,0);
  char time4 = '0' + now.minute() % 10;  ShowChar(1,time4, r_time, g_time, b_time,0);
  delay(500);
  SetPixelXY(2, 0, 2, r_time, g_time, b_time);
  SetPixelXY(2, 0, 5, r_time, g_time, b_time);
  delay(500);
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if(r_time != 0){
      r_time = 0;
      g_time = 1;
      b_time = 0;
    }else if(g_time != 0){
      r_time = 0;
      g_time = 0;
      b_time = 1;
    } if(b_time != 0){
      r_time = 1;
      g_time = 0;
      b_time = 0;
    }
  } 
}

void ShowColor(int Adr, unsigned char R, unsigned char G, unsigned char B) {
  unsigned char shift;
  RainbowCMD[0]='R';
  RainbowCMD[1]=0x03;
  RainbowCMD[2] = B;
  RainbowCMD[3]=((G<<4)|(R));
  SentCMD(Adr);
}

void SetPixelXY(int Adr, unsigned char X, unsigned char Y, unsigned char R, unsigned char G, unsigned char B) {
  unsigned char TranslateX[8]={6,7,4,5,2,3,0,1};
  X = TranslateX[X];
  RainbowCMD[0] = 'R';
  RainbowCMD[1] = 0x04; 
  RainbowCMD[2] = R;
  RainbowCMD[3] = ((G<<4)|(B));
  RainbowCMD[4] = ((X<<4)|(Y));
  SentCMD(Adr);
}

void ShowChar(int  Address,unsigned char ASCII,unsigned char red, unsigned char green ,unsigned char blue,unsigned char shift)
{
    RainbowCMD[0]='R';
    RainbowCMD[1]=0x02;
    RainbowCMD[2]=((shift<<4)|(red));
    RainbowCMD[3]=((green<<4)|(blue));
    RainbowCMD[4]=ASCII;
    SentCMD(Address);
}

void SentCMD(int  Add)
{   unsigned char OK=0;
     unsigned char i,temp;
 
    while(!OK)
  {                          
    switch (State)
    {       

    case 0:                          
      Wire.beginTransmission(Add);
      for (i=0;i<5;i++) Wire.send(RainbowCMD[i]);
      Wire.endTransmission();    
      delay(5);   
      State=1;                      
      break;

    case 1:

      Wire.requestFrom(Add,1);   
      if (Wire.available()>0) 
        temp=Wire.receive();    
      else {
        temp =0xFF;
        timeout++;
      }

      if ((temp==1)||(temp==2)) State=2;
      else if (temp==0) State=0;

      if (timeout>5000)
      {
        timeout=0;
        State=0;
      }

      delay(5);
      break;

    case 2:
      OK=1;
      State=0;
      break;

    default:
      State=0;
      break;

    }
  }
}

I'm using RTClib files to test ds1307 RTC on data logger shield from Adafruit. Trying to compile ds1307.pde I get error that RTClib.h has many stray '\240'.
Here is the .h file itself
// Released to the public domain! Enjoy!

// Simple general-purpose date/time class (no TZ / DST / leap second handling!)
class DateTime {
public:
DateTime (uint32_t t =0);
DateTime (uint16_t year, uint8_t month, uint8_t day,
uint8_t hour =0, uint8_t min =0, uint8_t sec =0);
DateTime (const char* date, const char* time);
uint16_t year() const { return 2000 + yOff; }
uint8_t month() const { return m; }
uint8_t day() const { return d; }
uint8_t hour() const { return hh; }
uint8_t minute() const { return mm; }
uint8_t second() const { return ss; }
uint8_t dayOfWeek() const;

// 32-bit times as seconds since 1/1/2000
long secondstime() const;
// 32-bit times as seconds since 1/1/1970
uint32_t unixtime(void) const;

protected:
uint8_t yOff, m, d, hh, mm, ss;
};

// RTC based on the DS1307 chip connected via I2C and the Wire library
class RTC_DS1307 {
public:
static uint8_t begin(void);
static void adjust(const DateTime& dt);
uint8_t isrunning(void);
static DateTime now();
};

// RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
class RTC_Millis {
public:
static void begin(const DateTime& dt) { adjust(dt); }
static void adjust(const DateTime& dt);
static DateTime now();

protected:
static long offset;
};

AND the pde file is ds1307 as follows:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
}
}

void loop () {
DateTime now = RTC.now();

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");

// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);

Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}

Any ideas?

Please modify your posting and use the # button for code blocks (so it becomnes readable).

I have used the library with no problems, so it might be the installation. Have you tried to download & install the latest version of the library again?