String and Array Confusion

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
bafil
 
Posts: 9
Joined: Sun Apr 15, 2012 11:16 am

String and Array Confusion

Post by bafil »

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 all


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

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: String and Array Confusion

Post by pburgess »

Greetings from one Phillip B. to another. :D

Problem is with these lines:

Code: Select all

  int Time =(Hrs, Min, Sec);
  Serial.println(Time);
The first line doesn't concatenate the values, nor are they being passed to a function. The syntax is still valid C though -- the comma operator normally lets you handle multiple assignments on one line (most frequently seen in the setup of 'for' loops)...but in this case, reduces to the equivalent of:

Code: Select all

  int Time = Sec;
To get these into a string (which is probably what you want), there are a couple of approaches...easiest is probably sprintf(), a la:

Code: Select all

  char Time[9];
  sprintf(Time, "%02:%02:%02", Hrs, Min, Sec);
The POV display code will then need to walk through the string, converting each ASCII character to your corresponding bitmap.

bafil
 
Posts: 9
Joined: Sun Apr 15, 2012 11:16 am

Re: String and Array Confusion

Post by bafil »

Hey Phillip B.

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

I posted 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 all



Last edited by bafil on Mon Apr 30, 2012 10:28 am, edited 1 time in total.

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: String and Array Confusion

Post by pburgess »

You have both a local and a global variable named 'Time'. Inside the loop() function, don't declare the variable Time at all (so disregard that line in the code I previously posted)...remove the line, leave the global declaration at top, then sprintf() into the string as I showed. Should work at that point.

bafil
 
Posts: 9
Joined: Sun Apr 15, 2012 11:16 am

Re: String and Array Confusion

Post by bafil »

Thanks for your help Phillip B.

I went through my code some more and did the changes you said and I finally got it to work.

Thanks again for your help

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”