Pages: [1] 2   Go Down
Author Topic: String and Array Confusion.  (Read 1648 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All

I am working on a POV clock that is giving me a hard time in the coding department. I am at a point where I have been able to get data from a RTC breakout board and store the values. I also have been able to assign those values to a predefined character set for the POV. I am able to display the Hours values, the minutes values, and the seconds values. HOWEVER, I can only display them individually and not all at once. I think I am not creating either a string or an array correctly.

Here is a part of my sketch that deals with this issue:
Code:
//Declaring variables.   
  int Hrs = now.hour();
  int Mins = now.minute();
  int Sec = now.second();

  int Time =(Hrs, Min, Sec);
 
  //This is just for debugging. Just to see what is being sent to the LED's
  Serial.println(Time);
  delay(1000);
 

Ok, at this point when I put all three values in "int Time" all I get is my seconds value printing out on the serial monitor and on my POV clock.

How can I get it to show all individual values. Keep in mind, I am only using the Serial.print command here JUST to see whats being sent to my POV display.

Thanks
Phillip B.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you trying to concatenate the Hrs, Min, Sec together in an interger? I'm a confused about int Time =(Hrs, Min, Sec); I've never seen this in a code before. Suppose Hrs is 9, Min is 23 and Sec is 56. Do you want to put them together like this: 92356? Please tell me exactly want you want to accomplish with the code you put up, and what Time =(Hrs, Min, Sec); is supposed to do.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you trying to concatenate the Hrs, Min, Sec together in an interger? I'm a confused about int Time =(Hrs, Min, Sec); I've never seen this in a code before. Suppose Hrs is 9, Min is 23 and Sec is 56. Do you want to put them together like this: 92356? Please tell me exactly want you want to accomplish with the code you put up, and what Time =(Hrs, Min, Sec); is supposed to do.

I am trying to make one variable with all 3 parts of the time, Hrs, Mins, Secs. I still haven't figured out how to get the colons in there yet. So if the Hrs = 9, the Mins = 23, and the Sec = 56. I want the Time to = 9:23:56. How do I get that to happen. Also how can I get the predefined colons to show in that same variable too. My POV character set has a colon defined in it.   
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ints can only stored a single discrete integer value that is 16bits in size.
What you want is a string displaying the time in a specific format. sprintf() will allow you to generate a specifically formatted string containing the values you want.  Google it for details on it's usage.  fyi, it returns a char array (ie c style string).
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks

I understand how the sprintf() command will work. Is it possible to store all 3 of the time values and the colons as one variable so I can send it out to the POV display? I am only using the serial.print line just to see whats being sent to the POV.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you want something like:
Code:
char Time[10];

sprintf( Time, "%02d:%02d:%02d", Hrs, Mins, Sec );

Serial.println( Time );

The printf formatting code '%02d' means print an integer, zero-padded to 2 digits. E.g. '9' will be printed as '09'
Logged

83 - var
Offline Offline
God Member
*****
Karma: 9
Posts: 864
ARDUINO Powa !
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

test that !


Code:

  int Hrs = now.hour();
  int Mins = now.minute();
  int Sec = now.second();

  int Time[3] =(Hrs, Min, Sec); // your tab of int !
String string_time="";
  
  //This is just for debugging. Just to see what is being sent to the LED's

string_time +=  String(hour);    // (or =+ i dont say :s sry)
string_time +=  String(" : ");
string_time +=  String(minute);
string_time +=  String(" : ");
string_time +=  String(seconde);

  Serial.println(string_time);
  delay(1000);




Two solution for you, the int hour and the string our to save or print !

Skizo !
Logged

Un tien vaux mieux que deux tu l'auras !

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

test that !


Code:

  int Hrs = now.hour();
  int Mins = now.minute();
  int Sec = now.second();

  int Time[3] =(Hrs, Min, Sec); // your tab of int !
String string_time="";
  
  //This is just for debugging. Just to see what is being sent to the LED's

string_time +=  String(hour);    // (or =+ i dont say :s sry)
string_time +=  String(" : ");
string_time +=  String(minute);
string_time +=  String(" : ");
string_time +=  String(seconde);

  Serial.println(string_time);
  delay(1000);




Two solution for you, the int hour and the string our to save or print !

Skizo !



Hey Skizo

Thanks for the help but it did not work. I am still getting just the seconds value printing out.

Any other suggestions?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help but I still could not get what I needed.

here is the entire code that I am using, so maybe it will be easier to understand. I had help with the code so there are still parts of it I am a bit confused about.

Code:

« Last Edit: April 26, 2012, 08:19:39 pm by bafill » Logged

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

Code:
int Time =(Hrs, Mins, Sec);

This is not some sort of concatenation operation. That will just evaluate to Secs.

What was wrong with the sprintf idea?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int Time =(Hrs, Mins, Sec);

This is not some sort of concatenation operation. That will just evaluate to Secs.

What was wrong with the sprintf idea?

Hi Nick

I'm glad you chimed in on this. I could not get the sprintf() to work. I kept getting 2 different kinds of errors doing it. One was an invalid conversion from 'int' to 'const char*', and the other was initializing argument 2 of 'int sprintf(char*, const char*, ...)'


Heres that bit of code:

Code:

void loop()
  {

  //Calling time from RTC.   
  DateTime now = RTC.now();

  //Declaring variables.   
  int Hrs = now.hour();
  int Mins = now.minute();
  int Sec = now.second();
   
  sprintf (displayThis, 'Hrs', 'Mins', 'Sec');
  delay (1000);
  }  // end of loop

Logged

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

Why are you quoting your variables? That's a strange way to do it. More like this:

Code:
  char displayThis [20];
 
  sprintf (displayThis, "%02i:%02i:%02i", Hrs, Mins, Sec);

Now you will have to display each byte from displayThis one after the other so the digits appear in sequence.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Nick

If I do a Serial.println(displayThis); to check what displayThis has in it. It is correct and perfect. Thanks. But excuse my rookie attempts at writing code, but what are the "%2i:" I have only see %d or %02d when writing some C code. Also, will that also show the colons on my POV display if I am able to see them in the Serial monitor?

Thanks again,
Phillip B.
Logged

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

You should be able to find printf on the Net somewhere. Basically %i is for ints, whereas %d is for "decimal" (I think normally short ints). Probably %d would have worked - not that you had that.

Quote
Also, will that also show the colons on my POV display if I am able to see them in the Serial monitor?

If you font has them it should (and the font appears to have them). Just take them out if you don't want them.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Nick

I think we got it figured out.

But I have ANOTHER question. I have hooked it up to my Adruino UNO driven POV and tried it. But it didn't look correct. So I tried displaying the original hello word version you gave me but I changed it to show just the number 8. It seems to display more than just an 8 but many other characters. I can't figure out why.
Logged

Pages: [1] 2   Go Up
Jump to: