Go Down

Topic: Byte to Char (Read 4528 times) previous topic - next topic

hunter

May 21, 2012, 09:26 am Last Edit: May 21, 2012, 09:32 am by Coding Badly Reason: 1
Hey Guys,
I'm attempting to take bytes that contain time and date and make them into a string for the purpose of naming a file on an openlog.  My clock produces: month, dayOfMonth, hour, and minute in bytes.    I tried char() but had no joy.  Lots of ascii spaces.

Suppose it was October 28th at 14:12.  I would want my file name to be "10281412."   Problem is, I have no idea how to make:

byte minute 12;

turn into:

char minute[2] = "12";

I've seen in some examples people using springf().  I liked the one instance where I saw it, but I can't figure out how it works exactly.  

Any guidance much appreciated!

- Hunter



I included some (marginal) code below to help you understand what I'm trying to do.


Code: [Select]
#include "string.h"

// bytes each of the components
byte minute;
byte hour;
byte dayOfMonth;
byte month;

// strings for each of the four components
char Cmonth[2];
char CdayOfmonth[2];
char Chour[2];
char Cminute[2];

char TimeDate[8];  // will contain all of the four components in a string

void setup()
{
     Serial.begin(9600);
}

void loop()
  {

 // Data as it arrives off the clock  
 minute = 12;
 hour = 14;
 dayOfMonth = 28;
 month = 10;

   //**** need something here that will turn bytes into integers
 
// I have no idea if this is legal, or how I'd go about putting all of my CHAR's into one long string.
 TimeDate[8] = {month, dayOfMonth, hour, minute};
 
 Serial.print(TimeDate);
 
   //**** I want to see: "10281412";  That's month dayOfMonth hour minute.
   
  }



Moderator edit: [code] [/code] tags added.

michael_x

does

Code: [Select]
byte minute =12;
byte hour=14;
byte dayOfMonth=28;
byte month=10;
char TimeDate[9];
sprintf (TimeDate, "%02u%02u%02u%02u",month,dayOfMonth,hour,minute);

work ?

"%02u" means unsigned 2digits with leading zero.
- You need space for a trailing 0 as end of string indicator, so TimeDate has to be at least 9 chars wide .
- You don't need string.h
- If it's just to create a file name, you can do that in one shot:
   sprintf (TimeDate, "%02u%02u%02u%02u.log",month,dayOfMonth,hour,minute); should produce "10281412.log"

hunter

Michael,
That pretty much worked.  Thanks!

Your explanation of sprintf combined with what I saw in an openlog example were the magic trick.  The line that ultimately worked was:

sprintf(buff, "new L%02d%02d%02d.txt\r", month, dayOfMonth, fileNumber);

I didn't mention any of it in the first post, but I needed "new" to initiate the new file.  I replaced hour and minute with fileNumber to keep the name under the size 8.3 limit.   Then I added \r, and I have no idea what it means or why it's necessary, but it works.

Thanks a ton for your good explanation and quick response.

- Hunter

PaulS

Quote
I didn't mention any of it in the first post, but I needed "new" to initiate the new file.  I replaced hour and minute with fileNumber to keep the name under the size 8.3 limit.   Then I added \r, and I have no idea what it means or why it's necessary, but it works.

What, exactly, are you doing with the resulting buffer? The SD library does not expect that "new " in there, nor will it operate correctly with it there.

The \r is a new line, often used to indicate the end of a command, which implies that the array you are populating defines some kind of command.

hunter

I'm sending it straight to the OpenLog.    I got it from this example:
https://github.com/sparkfun/OpenLog/blob/master/OpenLog_CommandTest/OpenLog_CommandTest.ino
Lines 76-90.

Here's my implementation:

Code: [Select]
  sprintf(buff, "new L%02d%02d%02d.txt\r", month, dayOfMonth, fileNumber);
  Serial.print(buff); //\r in string + regular print works with older v2.5 Openlogs


  delay(500);
 
  sprintf(buff, "append L%02d%02d%02d.txt\r", month, dayOfMonth, fileNumber);
  Serial.print(buff);

  delay(500);


I wish I could get Serial.read to work so I could see that the OpenLog is alive before I start sending data (as Nate does in the example).  It stopped working last night (hence no while loops).  I don't know what failed, I suspect hardware.

- Hunter

Go Up