SPI Slave to Master sending dummy bite

I have been trying to send four bytes of data between two Arduino DUE with SPI which I have sought of to manage to get working

I have been able to send and receive individual bytes and put them into an array the issue I'm having is the order that the bytes are in the array as once I have received all four bytes it puts it back into a long int

so for example when I upload the code the bytes are received from the slave are in this order 113,69,10,1 but another time I upload the code or reset the Arduino the bytes are received in this order 69,10,1,113

What I was thinking to get around this is send a char or some character For example "\n" which is displayed as a number 10. then work out where that is in the array and reference the other numbers from that spot

But for example if one of the bytes is number 10 As the four bytes of data will change depending on what long int is being sent, how do I distinguish between the number 10 and the char 10 as in 10,113,69,10,1

I might have misunderstood some think all they could be a completely better way of receiving four bytes from the slave

Your help would be much appreciated jim

But for example if one of the bytes is number 10 As the four bytes of data will change depending on what long int is being sent, how do I distinguish between the number 10 and the char 10

You can't. The byte 10 is exactly the same bit pattern as the char whose numeric value is 10.

It really isn't that much work to convert the long to a string, send the string, and convert the string back to a signed or unsigned long.

hi Paul
Thank you for your speedy reply

i will do some research and try to work out how to do that

jimlathe: hi Paul Thank you for your speedy reply

i will do some research and try to work out how to do that

ltoa() and atol().

You could try an initialization sequence to synchronize an SPI master with an SPI slave.

E.g. before sending real values byte by byte, the slave sends a sequence of 4 bytes repeatedly (255,254,253,252) until the SPI master can receive these 4 bytes in this order, and the SPI master sends 255 repeatedly as a dummy byte until it receives the correct four bytes, then sends another dummy byte, e.g. 55 to indicate to the SPI slave it can send now the real bytes one by one.

Note that you can send data 16 bits by 16 bits too.