converting RTC to string

Hi, i am doing a clock project with two adafruit 8x8 bicolor matrix and a RTC.
i am not really into strings, and when i try to put “now.hour();” in a temporary string it gives me an error (invalid conversion from ‘unit8_t’ to ‘const char*’)

basically what i want to do is take the actual time and put it into a string that i can print on the matrix

this is the code, can someone help?

Thank you
Fabrizio

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


Adafruit_BicolorMatrix matrixa = Adafruit_BicolorMatrix();
Adafruit_BicolorMatrix matrixb = Adafruit_BicolorMatrix();
String stringclock,stringtemp;
RTC_DS1307 RTC;
const int DS1307 = 0x68;

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");
  
  DateTime now = RTC.now();
    //this gives me the error
    stringtemp=now.hour();
    //adding the hour to the string
    stringclock= stringclock+stringtemp;
    //i know that i have to add puntuation and leading zeroes... i'll do it later when this is fixed
    stringtemp=now.minute();
    stringclock= stringclock+stringtemp;
    //
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(3000);
  
  
  
  matrixa.begin(0x76);  // pass in the address
   matrixb.begin(0x77);  // pass in the address
   matrixa.setRotation(1);
   matrixb.setRotation(1);
   matrixa.clear();
   matrixb.clear();
   matrixa.writeDisplay();
   matrixb.writeDisplay();
}

void loop() {


  matrixa.setRotation(1);
        matrixa.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
        matrixa.setTextSize(1);
        matrixa.setTextColor(LED_RED);
        
        matrixb.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
        matrixb.setTextSize(1);
        matrixb.setTextColor(LED_RED);
  for (int x=7; x>=-80; x--) {
          matrixa.clear();
          matrixa.setCursor(x,0);
          matrixa.print(stringclock);
          matrixa.writeDisplay();
          
          matrixb.clear();
          matrixb.setCursor(x+8,0);
          matrixb.print(stringclock);
          matrixb.writeDisplay();
          
          delay(75);
  }
}

Hello,

I suggest that you don't use the String class. now.hour() will most likely return a byte, and you can store it in a char array by using a function such as itoa, or sprintf. The latter is much more powerful, but also requires much more memory.

Ok, but i get strange results..

time here is 23:57 and i get 165165 as the output of the concatenation of hour and time

 DateTime now = RTC.now();
    itoa(now.hour(),time,10);
    stringclock= stringclock+time;
    itoa(now.minute(),time,10);
    stringclock= stringclock+time;

Try:

char time[6];
sprintf( time, "%02hhu:%02hhu", now.hour(), now.minute() )
...
matrixa.print( time );

it prints 165:165 on the matrix :|

I think i have some problem with the library, i have tried to incorporate another project with the etc and i can't make it to work in this project, it must be a some simple mistake i must find...

No idea, I don't have a matrix :/

I see you're using the DS1307. Keep in mind how the Time Keeper Register works. Each value is a BCD number, so if you try to display the 8 bits as a single byte, it probably won't make much sense. So if you need seconds, you read register 0 and look at the low nibble. You can find BCD routines on this Forum.

I cleaned up the code and moved RTC stuff out of setup(), now it works as expected!
I also added a third 8x8 matrix to scroll the hour.
here is a short video of it https://dl.dropboxusercontent.com/u/8328992/arduino/video/8x8.mp4

Thank you!

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

// create three instances of the matrix
Adafruit_BicolorMatrix matrixa = Adafruit_BicolorMatrix();
Adafruit_BicolorMatrix matrixb = Adafruit_BicolorMatrix();
Adafruit_BicolorMatrix matrixc = Adafruit_BicolorMatrix();

RTC_DS1307 rtc;
const int DS1307 = 0x68; //address of the RTC
char time[30]; //

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");

  matrixa.begin(0x76);  // address of the first matrix (first installed from the left)
  matrixb.begin(0x77);  // address of the second
  matrixc.begin(0x75);  // address of the third
  matrixa.setRotation(1);
  matrixb.setRotation(1);
  matrixc.setRotation(1);
  matrixa.clear();
  matrixb.clear();
  matrixc.clear();
  matrixa.writeDisplay();
  matrixb.writeDisplay();
  matrixc.writeDisplay();
  matrixa.setBrightness(1);
  matrixb.setBrightness(1);
  matrixc.setBrightness(1);
  matrixa.setTextWrap(false);
  matrixb.setTextWrap(false);
  matrixc.setTextWrap(false);
  matrixa.setTextSize(1);
  matrixb.setTextSize(1);
  matrixc.setTextSize(1);
  matrixa.setTextColor(LED_RED);
  matrixb.setTextColor(LED_RED);
  matrixc.setTextColor(LED_RED);
}

void loop() {

  getTime();
  
  // we start the loop at 7 so the text is out of the first matrix, and we stop when we have scrolled to -80 pixels
  //this now has to be adjusted manually based on the number of chars to be displayed
  for (int x=7; x>=-80; x--) {
    matrixa.clear();
    matrixa.setCursor(x,0);
    matrixa.print(time);
    matrixa.writeDisplay();

    matrixb.clear();
    matrixb.setCursor(x+8,0); //offset 8 pixels for the second matrix
    matrixb.print(time);
    matrixb.writeDisplay();

    matrixc.clear();
    matrixc.setCursor(x+16,0); //offset 16 pixels for the second matrix
    matrixc.print(time);
    matrixc.writeDisplay();

    delay(70); //speed of the rolling text
  }
}

void getTime(void)
{
  //get the time and put it into "time" chars
  DateTime now = rtc.now();

  sprintf( time, "%02hhu:%02hhu:%02hhu", now.hour(), now.minute(), now.second() );

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

  delay(1000);
}