Go Down

Topic: RTCLib parsing (Read 958 times) previous topic - next topic

Pauly

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.
Code: [Select]
// 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:
Code: [Select]
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

Nick Gammon

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.
http://www.gammon.com.au/electronics

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 ?

Code: [Select]
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 -
Quote
Buffer = 15 length = 2

PaulS

Quote
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.

Code: [Select]
  n =sprintf (buffer, "%d", a);
Why do you care how many characters sprintf wrote in the buffer? You never use that value.

Quote
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:
Code: [Select]
char last = buffer[strlen(buffer)-1];

Nick Gammon


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


So he can get the last one?
http://www.gammon.com.au/electronics

Pauly

Quote

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.

Nick Gammon

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

Code: [Select]

char last = buffer [n - 1];
http://www.gammon.com.au/electronics

guix

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

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

danb35


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 ?
Code: [Select]

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


You could instead do this:
Code: [Select]

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:
Code: [Select]

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.

guix


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 :)

Go Up