[SOLVED] Need help with writing and using a "struct"

I need help to get the syntax right in “struct sleepSt” below.

I’m sure the setup() function has errors as well, but can’t debug it until I get “struct sleepSt” syntax correct.

You all are WONDERFUL helpers. I’d be in big trouble if I didn’t have this forum to take me by the hand and lead the way.

#include <SPI.h>
#include <SD.h>

struct sleepSt
  char hr[2];
  char a;
  char mn[2];
  char b[4];
  char level;
  char c[4];

sleepSt sleepRec =
  {'0','0'}, // hour
  {'0','0'}, // minute
  {',',' ',' ',' '},
  {' '},     // level
  {' ',' ',' ','\n'}

File sleepF;
byte* sleepRecAr = (byte*)&sleepRec;

void setup()
  sleepF = SD.open("SLEEP101",FILE_WRITE);
  sleepF.println("TIME: ,LEVEL:");
  sleepRec.hr = "12";
  sleepRec.mn = "45";
  sleepRec.level = "3";
  for (byte i=0; i<5; i++)
  sleepRec.hr = "00";
  sleepRec.mn = "00";
  sleepRec.level = "0";
  sleepF = SD.open("SLEEP101",FILE_WRITE);
  sleepRec = sleepF.read();

void loop() {}

This sketch should save a file that looks like the below in a text editor:

12:45 ,  3      
12:45 ,  3      
12:45 ,  3      
12:45 ,  3      
12:45 ,  3
file sleepF;

Shouldn't that be "File"?

file sleepF;Shouldn't that be "File"?

Oops. Yes. Just now corrected it. Thanks.

Entries like:

sleepRec.hr = "12";

are trying to copy directly into a char array and I don't think that will work. It would think it would require something like:

strcpy(&sleepRec.hr, "12");

However, I don't think that will work either, because you later try to print them with the Serial object as though they are C strings, but your structure definition left no room for the null termination character for each member of the structure.

Also, shouldn't :

sleepSt sleepRec;

be defined as:

struct sleepSt sleepRec;

I don't have access to my computer right now, so I can't test the above.

Also, shouldn't :

sleepSt sleepRec;

be defined as:

struct sleepSt sleepRec;

Yes. sleepRec is not a type. struct sleepSt is.

Typically, one would do something like:

typedef struct sleepSt SleepSt;

so one could create an instance by

SleepSt sleepRec;

Thanks guys!

One more neccesity was removing the brackets from around single values, changing
and changing
{' '}, to
' ',