Hey guys.
I've got lots of data of different variable types,
I'd like to send that data from one Arduino to another using the SPI protocol.
(for fellow noobs: Nick Gammon has a great write-up on different protocols: http://www.gammon.com.au/forum/?id=10918 )
I figured the best way to keep track of my data would be to create a struct.
And I figured since the SPI protocol sends one byte at a time, the best way to send my data would be to first convert the struct into a byte array (of the same size as my struct).
Here is what I've got thus far:
// Struct -to - Byte Array TEST
// define global data variables:
byte exampleByte = 255;
int exampleInt = 32767;
long exampleLong = 2147483647;
// create a structure to store the different data values:
struct myDataStruct
{
byte exampleByteVessel;
int exampleIntVessel;
long exampleLongVessel;
// since I plan on using these variables to later update the
// global variables on the slave arduino, I am calling these
// variables "vessels". I'll have to update these "vessels" w/
// the coresponding global data values.
};
// name the structure:
typedef struct myDataStruct dataVessels;
// I'm not sure why I have to name it, because it sure seems like
// a name was already assigned to it when I created the struct (?)
// declare a byte array:
byte byteArray[sizeof(dataVessels)];
// I plan to use this byte array to send over SPI to the slave
// ardino. Now if only I could figure out how to populate this
// array with all the bytes within the struct (?)
void setup()
{
// populate the variables within the struct:
myDataVessels.exampleByteVessel = exampleByte;
myDataVessels.exampleIntVessel = exampleInt;
myDataVessels.exampleLongVessel = exampleLong;
// now the vessels are loaded.
// populate the byte array by...????
//...
//...
//...
// send the byte array over SPI:
// haven't gotten this far yet, because I'm stuck on trying
// to figure out how to convery my struct to a byte array.
}
void loop()
{
}
I've noticed two different approaches to solving this problem:
one involved a "union" : union and struct to make byte array - Programming Questions - Arduino Forum
and the other involved casting a char* : c++ - Convert struct into bytes - Stack Overflow
I'm not familiar with either of these techniques, nor do I know what the "best practice" is, and I don't fully understand how to implement them.
If someone could please baby-step me through the struct-to-byte array conversion process I would be very grateful.
Thanks.
-Josh!