springf error help needed

I try to use the CountUpDownTimer.h and four digit seven segment display with sevseg.h library to display the count down in below time format. hh:mm (just hour and minutes).

Due to the CountUpDownTimer is not display in the time format. I try to use springf command to achieve the hh:mm format. When I add this I get error when compile.

char buffer[5];
sprintf (buffer, "%02d:%02d", T.ShowHours(), T.ShowMinutes());
Serial.print(buffer);

The error message is " no known conversion for argument 1 from 'char [5]' to 'byte {aka unsigned char}' "

I attached my code.

The hardware connection to the seven segment have no errors as I can get proper value display if just show the T.ShowHours.

Any help is very much appreciated. I have get stuck for whole days.

simple-countdown-sevseg.txt (1.5 KB)

What data type does ShowHours() return ? It may be an array of chars, perhaps terminated by a zero making it into a string.

What happens if you call the functions and put the values returned in variables before using them in sprintf() ?

Have you tried using the string specifier in sprintf() instead of %d ?

I attached my code.

I POSTED your code

#include<CountUpDownTimer.h>
#include "SevSeg.h"

CountUpDownTimer T(DOWN, HIGH); // Default precision is HIGH, but you can change it to also be LOW
SevSeg sevseg; //Instantiate a seven segment controller object




void setup()
{
  Serial.begin(115200);
  T.SetTimer(0,2,20);     //start at 2 minute, 20 sec (USE FOR: DOWN ONLY)
  T.SetStopTime(0,0,30); // stop at 10 seconds (USE FOR: UP/DOWN)
  T.StartTimer();
//Setup for Seven Segment Display
  byte numDigits = 4;
  byte digitPins[] = {A0, A1, A2, A3};
  byte segmentPins[] = {2, 3, 4, 8, 9, 10, 11, 12};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE; // See README.md for options
  bool updateWithDelays = false; // Default. Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
  sevseg.setBrightness(90);
    
}

void loop()
{
  T.Timer(); // run the timer
  
  if (T.TimeHasChanged() ) // this prevents the time from being constantly shown.
  {
    Serial.print(T.ShowHours());
    Serial.print(":");
    Serial.print(T.ShowMinutes());
    Serial.print(":");
    Serial.println(T.ShowSeconds());
    
   char buffer[10];
   sprintf (buffer, "%02d:%02d", T.ShowHours(), T.ShowMinutes());
   Serial.print(buffer);
   
   sevseg.setNumber(buffer, 0);
 
     
  }
 sevseg.refreshDisplay();
}

You don't post the complete error message with line numbers and then you post a section of code where the error is not at. Additionally, the code in your post is not even exactly the code in the attached file. Very annoying.

   char buffer[10];
   sprintf (buffer, "%02d:%02d", T.ShowHours(), T.ShowMinutes());
   Serial.print(buffer);
   
   sevseg.setNumber(buffer, 0);

A buffer of 5 as shown in your original post would have been too small for the 5 characters and terminating null character. The 10 in your actual code is more than adequate.

setNumber() only takes actual numbers, not character strings. You can choose between the following prototypes:

  void setNumber(long numToShow, char decPlaces=-1, bool hex=0);
  void setNumber(unsigned long numToShow, char decPlaces=-1, bool hex=0);
  void setNumber(int numToShow, char decPlaces=-1, bool hex=0);
  void setNumber(unsigned int numToShow, char decPlaces=-1, bool hex=0);
  void setNumber(char numToShow, char decPlaces=-1, bool hex=0);
  void setNumber(byte numToShow, char decPlaces=-1, bool hex=0);
void setNumber(float numToShow, char decPlaces=-1, bool hex=0);

I am sorry, the complete error code is below :-

C:\Users\User\Documents\Arduino\libraries\SevSeg-master/SevSeg.h:62:8: note: no known conversion for argument 1 from 'char [10]' to 'byte {aka unsigned char}'
Error compiling.

The char buffer[5] change to char buffer[10], the error is the same.

The ShowHours() return a Clock/3600. The Clock is an unsigned int. I have tried to display ShowMinutes(), it can be display in the seven segment. The problem is the format is not time format HH:MM

I bet that a few minutes reading the library documentation would solve your problem.

sevseg.setNumber(buffer, 0);

The problem is here, see my previous post.

There is also this prototype:

void setChars(char str[]);

arduarn,

Sorry that I am really new in programming. I try to put the prototype code before the void loop()

void setChars(char str[]);
void setNumber(unsigned int numToShow, char decPlaces=-1, bool hex=0);
void setNumber(char numToShow, char decPlaces=-1, bool hex=0);

It doesnt work either. Pls enlighten me.

Change this line:

sevseg.setNumber(buffer, 0);

to this line:

sevseg.setChars(buffer);

and it should probably fix it.

Sorry that I am really new in programming.

Ok, apologies if I confused you with the prototype stuff. On reflection, it is not obvious to a beginner.

Thank you Arduarn,

It is working now. I reattach my complete code below :-

#include<CountUpDownTimer.h>
#include "SevSeg.h"

CountUpDownTimer T(DOWN, HIGH); // Default precision is HIGH, but you can change it to also be LOW
SevSeg sevseg; //Instantiate a seven segment controller object

void setup()
{
Serial.begin(115200);
T.SetTimer(4,50,20); //start at 2 minute, 20 sec (USE FOR: DOWN ONLY)
T.SetStopTime(0,0,30); // stop at 10 seconds (USE FOR: UP/DOWN)
T.StartTimer();
//Setup for Seven Segment Display
byte numDigits = 4;
byte digitPins[] = {A0, A1, A2, A3};
byte segmentPins[] = {2, 3, 4, 8, 9, 10, 11, 12};
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // See README.md for options
bool updateWithDelays = false; // Default. Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros

sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros);
sevseg.setBrightness(90);

}

//void setChars(char str[]);
//void setNumber(unsigned int numToShow, char decPlaces=-1, bool hex=0);

void loop()
{
T.Timer(); // run the timer

if (T.TimeHasChanged() ) // this prevents the time from being constantly shown.
{
Serial.print(T.ShowHours());
Serial.print(":");
Serial.print(T.ShowMinutes());
Serial.print(":");
Serial.println(T.ShowSeconds());

char bufferHour[8];
sprintf (bufferHour, "%02d", T.ShowHours());
Serial.println(bufferHour);

char bufferMinutes[8];
sprintf (bufferMinutes, "%02d", T.ShowSeconds());
Serial.println(bufferMinutes);

char timestr[100];
strcpy (timestr,bufferHour);
strcat (timestr,bufferMinutes);

sevseg.setChars(timestr);

}
sevseg.refreshDisplay();
}

My next problem now is the sevseg.h library do not have a way to display the time double dots.
My double dots is light up with dp(pin11 +) and com2 (-) and dp(pin 11 +) and com3 (-). How to modify this into the sevseg library ?

Please use the code tags button, the </> button in the top left of the post editor, to wrap code in your post. It formats it properly and stops the code from being mangled by the forum software.

I don't think that the SevenSeg library supports anything other than seven segment characters. You can either:

  • Connect the colon up directly to another digital pin or pins to control it (or to supply if you want it permanently lit), but you may need to add a resistor to avoid damage to your display depending on what hardware you have.
  • Or, and I think this may work, add the colon as a pseudo extra digit to your SevenSeg configuration and print a number that lights it up, an 8 should do the trick.
  • Or find a library that supports the colon.

All at your own risk of course.

i used the line
sevseg.setNumber(100*ShowMinutes()+ShowSeconds(),2);
to make it display hours and minutes