MD_Parola and Chronos libraries for a led matrix countdown display and clock

Hi,

I am trying to hack together 2 libraries to create a Countdown display and clock. I am using MD_Parola and Chronos and can't seem to put a uint32_t into a char array.

Code below, issue is around line 179, I have tried using sprintf but I don't know what I am doing. Been playing around casually for years but never really delved in and learned how to do this stuff, now I am trying but am completely lost.

Ignore my ignorance if it is a very simple fix but I can't see the wood for the trees.

// Program to demonstrate the MD_Parola library
//
// Display the time in one zone and other information scrolling through in
// another zone. 
// - Time is shown in a user defined fixed width font 
// - Scrolling text uses the default font
// - Temperature display uses user defined characters
// - Optional use of DS1307 module for time and DHT11 sensor for temp and humidity
//   - DS1307 library (MD_DS1307) found at http://arduinocode.codeplex.com
//   - DHT11 library (DHT11_lib) found at http://arduino.cc/playground/Main/DHT11Lib
//

// Use the DHT11 temp and humidity sensor
#define	USE_DHT11	0

// Use the DS1307 clock module
#define	USE_DS1307	1

// Header file includes
#include <Time.h>
#include <Chronos.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#if USE_LIBRARY_SPI
#include <SPI.h>
#endif
#include "Font_Data.h"

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may 
// need to be adapted
#define	MAX_DEVICES	8

#define	CLK_PIN		13
#define	DATA_PIN	11
#define	CS_PIN		10

#define SECONDSINDAY 86400
#define SECONDSINHOUR 3600
#define SECONDSINMINUTE 60

#if	USE_DHT11
#include <dht11.h>

#define	DHT11_PIN	2

dht11	DHT11;
#endif

#if	USE_DS1307
#include <MD_DS1307.h>
#include <Wire.h>
#endif

// Hardware SPI connection
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

#define	SPEED_TIME	75
#define	PAUSE_TIME	0

#define	MAX_MESG	20

// Turn on debug statements to the serial output
#define  DEBUG  0

// Global variables
char	szTime[9];		// mm:ss\0
char	szMesg[MAX_MESG+1] = "";

uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 };	// Deg C
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 };	// Deg F

char *mon2str(uint8_t mon, char *psz, uint8_t len)

// Get a label from PROGMEM into a char array
{
  static const __FlashStringHelper*	str[] = 
  {
    F("Jan"), F("Feb"), F("Mar"), F("Apr"), 
    F("May"), F("Jun"), F("Jul"), F("Aug"), 
    F("Sep"), F("Oct"), F("Nov"), F("Dec")
  };

  strncpy_P(psz, (const char PROGMEM *)str[mon-1], len);
  psz[len] = '\0';

  return(psz);
}

char *dow2str(uint8_t code, char *psz, uint8_t len)
{
  static const __FlashStringHelper*	str[] = 
  {
    F("Sunday"), F("Monday"), F("Tuesday"), 
    F("Wednesday"), F("Thursday"), F("Friday"), 
    F("Saturday")
  };
  
  strncpy_P(psz, (const char PROGMEM *)str[code-1], len);
  psz[len] = '\0';

  return(psz);
}

void getTime(char *psz, bool f = true)
// Code for reading clock time
{
#if	USE_DS1307
  RTC.readTime();
  sprintf(psz, "%02d%c%02d", RTC.h, (f ? ':' : ' '), RTC.m);
#else
  uint16_t	h, m, s;
	
  s = millis()/1000;
  m = s/60;
  h = m/60;
  m %= 60;
  s %= 60;
  sprintf(psz, "%02d%c%02d", h, (f ? ':' : ' '), m);
#endif
}

void getDate(char *psz)
// Code for reading clock date
{
#if	USE_DS1307
  char	szBuf[10];
	
  RTC.readTime();
  sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
#else
  strcpy(szMesg, "29 Feb 2016");
#endif
}

void setup(void)
{
  P.begin(2);
  P.setInvert(false);
  
  P.setZone(0, 0, MAX_DEVICES-5);
  P.setZone(1, MAX_DEVICES-4, MAX_DEVICES-1);
  P.setFont(1, numeric7Seg);

  P.displayZoneText(1, szTime, CENTER, SPEED_TIME, PAUSE_TIME, PRINT, NO_EFFECT);
  P.displayZoneText(0, szMesg, CENTER, SPEED_TIME, 0, SCROLL_LEFT, SCROLL_LEFT);
  

#if USE_DS1307
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
#endif

  getTime(szTime);
}

void loop(void)
{
  static uint32_t	lastTime = 0;		// millis() memory
  static uint8_t	display = 0;		// current display mode
  static bool	flasher = false;	// seconds passing flasher
  
  P.displayAnimate();
  
  if (P.getZoneStatus(0))
  {
    switch (display)
    {
      case 0:	// Days until
      
        Chronos::DateTime targetDateTime(2016,4,25,20,00,00);
        RTC.readTime();
        Chronos::DateTime curDateTime(RTC.yyyy, RTC.mm, RTC.dd, RTC.h, RTC.m, RTC.s);
        Chronos::Span::Absolute timeDiff = targetDateTime - curDateTime;
        P.setTextEffect(0,SCROLL_LEFT,SCROLL_LEFT);
        //strcpy(szMesg, timeDiff.days());
        sprintf(szMesg, "%04" PRIu32, timeDiff.days());
        
        display++;
        break;
        
      case 1:	// day of week
        P.setTextEffect(0, SCROLL_LEFT, SCROLL_LEFT);
        display++;
        dow2str(RTC.dow, szMesg, MAX_MESG);			
        break;

      default:	// Calendar
        P.setTextEffect(0, SCROLL_LEFT, SCROLL_LEFT);
        display = 0;
        getDate(szMesg);
        break;
    }
		
    P.displayReset(0); 
  }
  
  // Finally, adjust the time string if we have to
  if (millis() - lastTime >= 1000)
  {
    lastTime = millis();
    getTime(szTime, flasher);
    flasher = !flasher;
	  
    P.displayReset(1);
  }
}

A simple way to put a uint32_t into a string would be:

char s[10];
uint32_t num = 20;
 
sprintf( s, "%u", num )

Ashavent previously worked with your mentioned libraries, didn't check out your code but if the problem is only conversion then this should work.

Cheers,

Now I get this error message

Parola_Zone_TimeMsg_countdown.ino: In function ‘void loop()’:
Parola_Zone_TimeMsg_countdown.ino:191:12: error: jump to case label [-fpermissive]
Parola_Zone_TimeMsg_countdown.ino:183:33: error:   crosses initialization of ‘Chronos::Span::Absolute timeDiff’
Parola_Zone_TimeMsg_countdown.ino:182:27: error:   crosses initialization of ‘Chronos::DateTime curDateTime’
Parola_Zone_TimeMsg_countdown.ino:180:27: error:   crosses initialization of ‘Chronos::DateTime targetDateTime’
Parola_Zone_TimeMsg_countdown.ino:197:7: error: jump to case label [-fpermissive]
Parola_Zone_TimeMsg_countdown.ino:183:33: error:   crosses initialization of ‘Chronos::Span::Absolute timeDiff’
Parola_Zone_TimeMsg_countdown.ino:182:27: error:   crosses initialization of ‘Chronos::DateTime curDateTime’
Parola_Zone_TimeMsg_countdown.ino:180:27: error:   crosses initialization of ‘Chronos::DateTime targetDateTime’

Alright I sorted it.

Turns out that error message comes up because of something to do with scope of the variables in the switch case. I put the Chronos into its own function and satisfied the code.

// Program to demonstrate the MD_Parola library
//
// Display the time in one zone and other information scrolling through in
// another zone. 
// - Time is shown in a user defined fixed width font 
// - Scrolling text uses the default font
// - Temperature display uses user defined characters
// - Optional use of DS1307 module for time and DHT11 sensor for temp and humidity
//   - DS1307 library (MD_DS1307) found at http://arduinocode.codeplex.com
//   - DHT11 library (DHT11_lib) found at http://arduino.cc/playground/Main/DHT11Lib
//

// Use the DHT11 temp and humidity sensor
#define	USE_DHT11	0

// Use the DS1307 clock module
#define	USE_DS1307	1

// Header file includes
#include <Time.h>
#include <Chronos.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#if USE_LIBRARY_SPI
#include <SPI.h>
#endif
#include "Font_Data.h"

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may 
// need to be adapted
#define	MAX_DEVICES	8

#define	CLK_PIN		13
#define	DATA_PIN	11
#define	CS_PIN		10

#define SECONDSINDAY 86400
#define SECONDSINHOUR 3600
#define SECONDSINMINUTE 60

#if	USE_DHT11
#include <dht11.h>

#define	DHT11_PIN	2

dht11	DHT11;
#endif

#if	USE_DS1307
#include <MD_DS1307.h>
#include <Wire.h>
#endif

// Hardware SPI connection
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

#define	SPEED_TIME	75
#define	PAUSE_TIME	0

#define	MAX_MESG	20

// Turn on debug statements to the serial output
#define  DEBUG  0

// Global variables
char	szTime[9];		// mm:ss\0
char	szMesg[MAX_MESG+1] = "";

uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 };	// Deg C
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 };	// Deg F

char *mon2str(uint8_t mon, char *psz, uint8_t len)

// Get a label from PROGMEM into a char array
{
  static const __FlashStringHelper*	str[] = 
  {
    F("Jan"), F("Feb"), F("Mar"), F("Apr"), 
    F("May"), F("Jun"), F("Jul"), F("Aug"), 
    F("Sep"), F("Oct"), F("Nov"), F("Dec")
  };

  strncpy_P(psz, (const char PROGMEM *)str[mon-1], len);
  psz[len] = '\0';

  return(psz);
}

char *dow2str(uint8_t code, char *psz, uint8_t len)
{
  static const __FlashStringHelper*	str[] = 
  {
    F("Sunday"), F("Monday"), F("Tuesday"), 
    F("Wednesday"), F("Thursday"), F("Friday"), 
    F("Saturday")
  };
  
  strncpy_P(psz, (const char PROGMEM *)str[code-1], len);
  psz[len] = '\0';

  return(psz);
}

void getTime(char *psz, bool f = true)
// Code for reading clock time
{
#if	USE_DS1307
  RTC.readTime();
  sprintf(psz, "%02d%c%02d", RTC.h, (f ? ':' : ' '), RTC.m);
#else
  uint16_t	h, m, s;
	
  s = millis()/1000;
  m = s/60;
  h = m/60;
  m %= 60;
  s %= 60;
  sprintf(psz, "%02d%c%02d", h, (f ? ':' : ' '), m);
#endif
}

void getDate(char *psz)
// Code for reading clock date
{
#if	USE_DS1307
  char	szBuf[10];
	
  RTC.readTime();
  sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
#else
  strcpy(szMesg, "29 Feb 2016");
#endif
}

void getDaysDiff()
{
  char hoursTill[3];
  Chronos::DateTime targetDateTime(2016,4,25,20,00,00);
  RTC.readTime();
  Chronos::DateTime curDateTime(RTC.yyyy, RTC.mm, RTC.dd, RTC.h, RTC.m, RTC.s);
  Chronos::Span::Absolute timeDiff = targetDateTime - curDateTime;
  sprintf(szMesg, "%u", timeDiff.days());
  sprintf(hoursTill, "%u", timeDiff.hours());
  strcat(szMesg, " days and ");
  strcat(szMesg, hoursTill);
  if (timeDiff.hours() > 1)
  {
    strcat(szMesg," hours until GoT");
  }
  if (timeDiff.hours() == 1)
  {
    strcat(szMesg, " hour until GoT");
  }
  if (timeDiff.hours() == 0)
  {
    strcat(szMesg, " until GoT");
  }
}

void setup(void)
{
  P.begin(2);
  P.setInvert(false);
  
  P.setZone(0, 0, MAX_DEVICES-5);
  P.setZone(1, MAX_DEVICES-4, MAX_DEVICES-1);
  P.setFont(1, numeric7Seg);

  P.displayZoneText(1, szTime, CENTER, SPEED_TIME, PAUSE_TIME, PRINT, NO_EFFECT);
  P.displayZoneText(0, szMesg, CENTER, SPEED_TIME, 0, SCROLL_LEFT, SCROLL_LEFT);
  

#if USE_DS1307
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
#endif

  getTime(szTime);
}

void loop(void)
{
  static uint32_t	lastTime = 0;		// millis() memory
  static uint8_t	display = 0;		// current display mode
  static bool	flasher = false;	// seconds passing flasher
  
  P.displayAnimate();
  
  if (P.getZoneStatus(0))
  {
    switch (display)
    {
      case 0:	// Days until
        P.setTextEffect(0,SCROLL_LEFT,SCROLL_LEFT);
        getDaysDiff();
        display++;
        
        break;
        
      case 1:	// day of week
        P.setTextEffect(0, SCROLL_LEFT, SCROLL_LEFT);
        display++;
        dow2str(RTC.dow, szMesg, MAX_MESG);			
        break;

      default:	// Calendar
        P.setTextEffect(0, SCROLL_LEFT, SCROLL_LEFT);
        display = 0;
        getDate(szMesg);
        break;
    }
		
    P.displayReset(0); 
  }
  
  // Finally, adjust the time string if we have to
  if (millis() - lastTime >= 1000)
  {
    lastTime = millis();
    getTime(szTime, flasher);
    flasher = !flasher;
	  
    P.displayReset(1);
  }
}

Great it worked out...