RTCLib parsing

Hi,
I'm trying to get the minutes from an RTC using RTCLib to use with a 7 segment display. Everything else is already working, I just need the minutes I've got the 10 spot working using a long handed version, but need the ones spot.

 // Minutes
 if(now.minute()>=10)
 {
  matrix.writeDigitNum(3, 1);
 }
  if(now.minute()>=20)
 {
  matrix.writeDigitNum(3, 2);
 }
  if(now.minute()>=30)
 {
  matrix.writeDigitNum(3, 3);
 }
  if(now.minute()>=40)
 {
  matrix.writeDigitNum(3, 4);
 }
  if(now.minute()>=50)
 {
  matrix.writeDigitNum(3, 5);
 }
  if(now.minute()<=9)
 {
  matrix.writeDigitNum(3, 0);
 }

For the ones spot, this method would be far too long.
What I've got for the ones spot so far is:

 for(int x;x<=59;x++)
 {
   if(now.minute() ==x)
   {
     matrix.writeDigitNum(4, x);
   }
 }

Obviously this only works in some cases. Is there an easy way to parse out the one spot minutes. For example, how do I get the 2 out of 11:52?
Thanks

You could look up the modulus operator. Given the minutes, taking minutes modulus 10 gives the remainder, that is the low-order digit.

Or, you could sprintf into a string and get the last character. In fact that would be an easy way of getting the entire time displayed.

Thanks Nick. So I've used sprintf, but am unclear how to get the last character.
This is what I have so far. How do I get the last character out ?

int parseMinutes()
{
 DateTime now = RTC.now();
  char buffer [10];
  int n; 
  int a =now.minute();
  n =sprintf (buffer, "%d", a);
  Serial.print("Buffer = ");
  Serial.print(buffer);  
  int len = strlen(buffer);
  Serial.print(" length = ");
  Serial.println(len); 
}

Results for say 9:15 are -

Buffer = 15 length = 2

but am unclear how to get the last character.
This is what I have so far. How do I get the last character out ?

I don't understand the question.

  n =sprintf (buffer, "%d", a);

Why do you care how many characters sprintf wrote in the buffer? You never use that value.

Results for say 9:15 are -
Quote
Buffer = 15 length = 2

Looks correct to me.

If your question is how to use the last character in the array:

char last = buffer[strlen(buffer)-1];

PaulS:
Why do you care how many characters sprintf wrote in the buffer?

So he can get the last one?

If your question is how to use the last character in the array:
Code:

char last = buffer[strlen(buffer)-1];

Thanks PaulS - that was what I was looking for.

Given that "n" was the number of characters in the buffer, and it is zero-relative:

char last = buffer [n - 1];

An easier method to write all your 7 segments displays at once (assuming you want hours, minutes and seconds):

char time_str[7];
sprintf( time_str, "%02d%02d%02d", now.hour(), now.minute(), now.second() );

for (uint8_t i = 0; i < 6; i++ )
  matrix.writeDigitNum( i+1, time_str[i] - '0' ); //not sure if the first 7-seg is id 1 but whatever, modify if needed

Pauly:
Thanks Nick. So I've used sprintf, but am unclear how to get the last character.
This is what I have so far. How do I get the last character out ?

  n =sprintf (buffer, "%d", a);

You could instead do this:

sprintf(buffer,"%.2d",a);

...then you'd know that buffer contained two digits (prefixed with 0 if a < 10). buffer[0] is the first digit, and buffer[1] is the second. And, as mentioned, you can do that with the the rest of the time elements as well, even putting them all into the same buffer:

sprintf(buffer,"%.2d%.2d%.2d",now.hour(), now.minute(), now.second());

I see that guix's example uses %02d while mine uses %.2d; I'm honestly not sure what the difference is between these two.

danb35:
I see that guix's example uses %02d while mine uses %.2d; I'm honestly not sure what the difference is between these two.

Same thing in this case :slight_smile: