# String and Array Confusion.

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:

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

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.

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.

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.

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

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.

Maybe you want something like:

``````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'

test that !

``````  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 !

skizoh:
test that !

``````  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?

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.

``````
``````
`````` 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?

`````` 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? [/quote]

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:

``````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
``````

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

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

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.

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.

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.

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.

Well show the code.

[quote author=Nick Gammon link=topic=103033.msg773458#msg773458 date=1335404557] Well show the code. [/quote]

Hehehe, that would help wouldn't it.

``````
``````

Here:

``````// move onto next letter if required
if (pixel >= 5)
{
pos++;
if (displayThis [pos] == 0)  // back to start
pos = 0;
}  // if done all 5 pixels
``````

Once we get up to the 5th pixel we move onto the next letter, but someone (was that me?) forgot to go back to pixel 0. So add an extra line like this:

``````// move onto next letter if required
if (pixel >= 5)
{
pixel = 0;
pos++;
if (displayThis [pos] == 0)  // back to start
pos = 0;
}  // if done all 5 pixels
``````

Its always something small like that.

Thanks so much for the help. I will keep you updated on my progress.

Grrrr this is getting crazy

Ok so we had some success with this code for displaying whatever is entered as plane text like hello world. But I am almost back to square 1. The time is not displaying on my POV. Maybe if I let you know what components I am using. The LED setup is pretty straight forward, I am using 8 red LED’s with 220 ohm resistors feeding off of the UNO’s 0-7 digital outputs (PORTD). I have an Adafruit RS1307 breakout board RTC on analog pins A2-A5, A2 and A3 are providing 5 volts and ground for the RTC board.

I was thinking it had to do with the sprintf() towards the end of the loop that shows, “sprintf (displayThis, “%02i:%02i:%02i”, Hrs, Mins, Sec);”
if I change that to just “sprintf(displayThis, “hello world”);” it works. But when I change it back to the original one to display the time it does not.

And here is my code again:

``````
``````