Go Down

Topic: converting 2 variables into character array variable (Read 978 times) previous topic - next topic


I have written some code that takes the amount on minutes called durationInMins.
durationInMins is converted to 2 byte variables named hours, and mins.

I don't know how to convert the byte variables hours, mins to a char array.
I'm trying to fill the character array with the data as follows "hh:mm" format.
Here is what I have so far I have it running on my Uno board.
Thank you

Code: [Select]

#include <math.h>
void setup()

void loop()
 char duration[] = {"  :  "};
 int durationInMins = 490;
 byte hours = 0;
 byte mins = 0;
 double i, r;
 r = durationInMins * .01666;
 Serial.print("Durationin mins: ");
 r = modf(r, &i);
 r = r * 60;
 mins = byte(round(r));
 hours = byte(i);
 Serial.print("Hours: ");
 Serial.print("Mins: ");
   I now want to convert
   variable hours to a character array;
   and variable mins to a character array
   then put them in char array duration
   char duaration[] = {"hh:mm"};



durationInMins/60 gets you the number of hours.

durationInMins%60 gets you the number on mins (see ref section).

Then use print an a print(0) in hours or mins is less than 10 to give the leading zero.



Code: [Select]

hours = durationInMins / 60;
mins = durationInMins % 60;

sprintf(duration, "%02d:%02d", (int)hours, (int)mins);
I only provide help via the forum - please do not contact me for private consultancy.


Ill give it a try much simpler that what I had lol thanks guys


By the way if you want thing to happen just once put the code in setup(), don't use a forever loop as you do.



Peter and Mark
Thanks I have a hard time coming up with simple solutions to my problems... I hope this gets better with time. Mark I understand that code in setup runs once but is there a reason why putting that for loop in Loop() is a bad idea? I want to learn do's and don't of Arduino. I've use the for loop in WinAvr.

Can you recommend books, or web sites to help learn the best approach solving the task needed?
I knew of modus arithmetic but wasn't sure how to use it.


is there a reason why putting that for loop in Loop() is a bad idea?

Not exactly, but your example is rather artificial - most arduino sketches will run through loop more than once - they're often used as controllers for some external system. In a learning exercise such as yours it really doesn't matter, but generally, if something will run only once at the beginning, particularly if other people are going to see the code, it should live in setup.


I appreciate your feedback, I will follow the convention as I move forward.
Thank you everyone who responded to the topic
I always learn something new with every topic I post in the forum. You all are respected!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131