I've been googling and trying to figure out how to convert (i.e. serialPrint) the value of an array to decimal.
In a nutshell here is a simplified sample of the code:
int point1;
int point2;
int point3;
int point4;
int point5;
int point6;
int point7;
int point8;
union {
byte configs [8] = {1,0,0,0,0,0,0,1}; // this will be replaced by what the slave has sent via i2c from another arduino.
} slaveDataUnion;
void setup()
{
Serial.begin(9600);
}
void loop() {
point8 = slaveDataUnion.configs [0];
point7 = slaveDataUnion.configs [1];
point6 = slaveDataUnion.configs [2];
point5 = slaveDataUnion.configs [3];
point4 = slaveDataUnion.configs [4];
point3 = slaveDataUnion.configs [5];
point2 = slaveDataUnion.configs [6];
point1 = slaveDataUnion.configs [7];
Serial.print(point1);
Serial.print(point2);
Serial.print(point3);
Serial.print(point4);
Serial.print(point5);
Serial.print(point6);
Serial.print(point7);
Serial.println(point8);
// Now need to print the decimal value.... :o
}
so far so good..... BUT....... I also need to see a decimal value of what the binary value above is.
Forgive me if it is a stupid question but my head hurts already and after days of fiddling with it I might just not see the forest 'cause of the trees!
Does this help. I added a bit to get the decimal value of the byte. Is this what you meant?
int point1;
int point2;
int point3;
int point4;
int point5;
int point6;
int point7;
int point8;
union
{
byte configs [8] = {1, 0, 0, 0, 0, 0, 1, 1}; // this will be replaced by what the slave has sent via i2c from another arduino.
} slaveDataUnion;
byte decimalValue = 0; //****** added
void setup()
{
Serial.begin(9600);
}
void loop()
{
// lets slow it down so we can see the prints
static unsigned long timer = 0;
unsigned long interval = 500;
if (millis() - timer >= interval)
{
timer = millis();
point8 = slaveDataUnion.configs [0];
point7 = slaveDataUnion.configs [1];
point6 = slaveDataUnion.configs [2];
point5 = slaveDataUnion.configs [3];
point4 = slaveDataUnion.configs [4];
point3 = slaveDataUnion.configs [5];
point2 = slaveDataUnion.configs [6];
point1 = slaveDataUnion.configs [7];
Serial.print(point1);
Serial.print(point2);
Serial.print(point3);
Serial.print(point4);
Serial.print(point5);
Serial.print(point6);
Serial.print(point7);
Serial.println(point8);
// Now need to print the decimal value.... :o
// I guessed that the MSB is slaveDataUnion.configs [0]
for (int n = 0; n < 8; n++)
{
bitWrite(decimalValue, n, slaveDataUnion.configs [7 - n]);
// if my guess is wrong
// bitWrite(decimalValue, n, slaveDataUnion.configs [n]);
}
Serial.print("Decimal value ");
Serial.println(decimalValue, DEC);
}
}
byte decimalValue = 0;
for (int bit = 0; bit < 8; bit++) decimalValue = 2 * decimalValue + slaveDataUnion.configs[bit]; // if bit 0 is MSB
If you are receiving the points separately - you could also benefit from a hack (out of language spec but works with GCC in your Arduino context) and use bitfields in your union.
Again, that's a hack as the language state that you should not read slaveDataUnion.byteValue if you have been using the slaveDataUnion.points structure to initialize the union. But it does work
Whandall:
Theoretically they are only guaranteed to work for one interpretation at a time,
factually the different interpretations work in parallel IIRC.
That means that we can't do the following though 3F9D70A4 is the correct binary32 formatted value for 1.23.
union
{
float x = 1.23;
byte myByte[4] = {0xA4, 0x70, 0x9D, 0x3F}; //LSB first
}myData;
We have to do either one of the following two:
union
{
float x = 1.23;
byte myByte[4];
}myData;
OR
union
{
float x;
byte myByte[4] = {0xA4, 0x70, 0x9D, 0x3F}; //LSB first
}myData;
Why do you think that has to be pointed out?
I see no connection to anything I have claimed.
It seems you can not assign to two interpretations of the same memory via initialization,
which absolutely makes sense to me, I would have never even tried that.
Whandall:
Theoretically they are only guaranteed to work for one interpretation at a time,
factually the different interpretations work in parallel IIRC.
yes - I usually add __attribute__((packed), gives a bit of comfort the compiler won't play too much with data alignment and data structure padding and go for a condensed memory usage.