Go Down

Topic: String and Array Confusion. (Read 1 time) previous topic - next topic

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.

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.

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