Go Down

Topic: String and Array Confusion. (Read 2006 times) previous topic - next topic

bafill

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

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

dkl65

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.
Visit http://www.glenof.webs.com/!

bafill


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. 

jraskell

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

bafill

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.

blackm0k

Maybe you want something like:
Code: [Select]

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'

skizoh

test that !


Code: [Select]


 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 !
Un tien vaux mieux que deux tu l'auras !

bafill


test that !


Code: [Select]


 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?

bafill

#8
Apr 26, 2012, 12:34 am Last Edit: Apr 27, 2012, 03:19 am by bafill Reason: 1
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: [Select]



Nick Gammon

Code: [Select]
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?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bafill


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


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


Nick Gammon

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

Code: [Select]
  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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bafill

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.

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bafill

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.

Go Up