Pages: [1]   Go Down
Author Topic: converting 2 variables into character array variable  (Read 609 times)
0 Members and 1 Guest are viewing this topic.
Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
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
Don

Code:
#include <math.h>
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  char duration[] = {"  :  "};
  int durationInMins = 490;
  byte hours = 0;
  byte mins = 0;
  double i, r;
  r = durationInMins * .01666;
  Serial.print("Durationin mins: ");
  Serial.println(durationInMins);
  r = modf(r, &i);
  r = r * 60;
  mins = byte(round(r));
  hours = byte(i);
  Serial.print("Hours: ");
  Serial.println(hours);
  Serial.print("Mins: ");
  Serial.println(mins);
  Serial.println();
  
  /*
    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"};
  */
 Serial.println(duration);
 Serial.println();
  
 for(;;){}  

}


 
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Mark

Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

sprintf(duration, "%02d:%02d", (int)hours, (int)mins);
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Mark
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Don   
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wildbill
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!
Don
Logged

Pages: [1]   Go Up
Jump to: