Pages: [1]   Go Down
Author Topic: RTCLib parsing  (Read 794 times)
0 Members and 1 Guest are viewing this topic.
Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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:
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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

So he can get the last one?
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
char last = buffer [n - 1];
Logged

France
Online Online
God Member
*****
Karma: 34
Posts: 984
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An easier method to write all your 7 segments displays at once (assuming you want hours, minutes and seconds):
Code:
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
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  n =sprintf (buffer, "%d", a);
You could instead do this:
Code:
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:
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.
Logged

France
Online Online
God Member
*****
Karma: 34
Posts: 984
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Jump to: