Now I plan to improve it and maybe learn a bit more.
The code was written to emulate (sort of) some communications code running on two serially connected (UART) arduino units which I had been working on with some considerable difficulty given that I did not know which unit was causing which problem.
My objective was to successfully transfer a structure containing both byte and float variables through a single byte interface and re-create the data successfully. This now works as intended using just the one arduino (actually a nano)
I could now move on to using this same approach on the two arduino's (one is a nano the other a mega 2560) but a helpful friend told me my approach is all wrong as I should be using pointers.
So, my question is: Should I continue with this approach or is there a far better method?
Please be gentle, my only previous experience with coding was during the 1980's using assembler.
/*
This test code is designed to explore how an array containing both bytes and floats
can be transferred through a byte sized interface and re-constructed with the same
data format and content.
The idea is to simulate passing byte size variables over a serial link using two
arduino units
pseudo code:
fill the sending structure with dummy data
// Now pass the data via a single byte variable to represent the serial port
for(i = 0; i < totalMessageSize; i++)
{
transferByte = sending strucure[i];
receivingStructure[i] = transferByte;
}
*/
// First define the source data
#define shortMessageSize 6
#define longMessageSize 4
#define totalMessageSize (shortMessageSize + (longMessageSize * 4) + 2)
byte dummyBytes[] = {2,4,6,8,3,3};
float dummyFloats[] = {3.141, 2.453, 56.4, 2234.5};
float tempFloat = 0;
int i;
// Construct the transmit data container
typedef struct
{
byte txShortMessage[shortMessageSize];
byte txShortMessageLRC;
float txLongMessage[longMessageSize];
byte txLongMessageLRC;
} txArray;
typedef union
{
txArray tx;
byte newArray[sizeof(txArray)];
} UTX;
UTX tx;
// Define the transfer location
byte transferByte = 0;
// Construct the receiving data container
typedef struct
{
byte rxShortMessage[shortMessageSize];
byte rxShortMessageLRC;
float rxLongMessage[longMessageSize];
byte rxLongMessageLRC;
} rxArray;
typedef union
{
rxArray rx;
byte newArray[sizeof(rxArray)];
} URX;
URX rx;
void setup()
{
Serial.begin(9600);
// Now populate the source array with data from dummyBytes & dummyFloats
for(i = 0; i < shortMessageSize; i++)
{
tx.tx.txShortMessage[i] = dummyBytes[i];
}
tx.tx.txShortMessageLRC = 55; // Any rubbish will do for testing
for(i = 0; i < longMessageSize; i++)
{
tx.tx.txLongMessage[i] = dummyFloats[i];
}
// Now pass the data across the invisible wire to the receiver one
// byte at a time
for(i = 0; i <= totalMessageSize; i++)
{
transferByte = tx.tx.txShortMessage[i];
Serial.print(transferByte,DEC); // Print the byte being transferred
rx.rx.rxShortMessage[i] = transferByte;
}
}
void loop()
{
for(i = 0; i < longMessageSize; i++)
{
tempFloat = rx.rx.rxLongMessage[i];
printDouble(tempFloat, 4);
Serial.print(" ");
}
Serial.println("");
}
// **************************************************************************
void printDouble( double val, byte precision)
{
// prints val with number of decimal places determined by precision
// precision is a number from 0 to 6 indicating the desired decimal places
// example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
if( precision > 0)
{
Serial.print("."); // print the decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;
if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
Serial.print("0");
Serial.print(frac,DEC) ;
}
}