I need to send data from a microcontroller through a serial port and would like to minimize the data sent, because the data is a 10bit reading from an ADC. So I thought, I would use an union. This is what I got:
union fortybit
{
struct
{
unsigned int a : 10;
unsigned int b : 10;
unsigned int c : 10;
unsigned int d : 10;
} tenbits;
unsigned char bytes[5];
};
Now I'm sending the data like this:
fortybit fb;
for (int i = 0; i < 640; i++)
{
fb.tenbits.a = 512;
fb.tenbits.b = 512;
fb.tenbits.c = 512;
fb.tenbits.d = 512;
Serial.write(fb.bytes, 5);
}
Which results in some strange readings, which are periodic and some of the values are right, but most of them are completely wrong.
What could I be doing wrong? What is the right way to do this?