So the conversion function "long" does not seem to accept arrays and I wonder how to use it?
You don't, because that is not what it is for.
The long that you want to construct is done by shifting one of the bytes in the array 24 places to the next. Then, the next byte is shifted 16 bits to the left, and added. Then, the next byte is shifted 8 bits to the left, and added. Then, the final byte is added.
long val = 0;
val += d[0] << 24;
val += d[1] << 16;
val += d[2] << 8;
val += d[3];
Depending on how the bytes were sent, the array may need to be used in the other order (3, 2, 1, 0).
long adc_value;
byte d[4];
/* Put the low byte in d[0].
*/
d[0]= shiftIn() // THE LOW BYTE
:
d[3] = shiftIn() // THE HIGH BYTE
adc_value = *((long *)d);
I do get interesting results and am still looking for a solution. It looks like the shift operator is only 16 bit. The cast to a long swaps nibbles on some of the bytes, which is strange. Thanks everyone for your help so far.
these operations need to be cast to a long. Your left operand is of type byte, and your right operand is a const that the compiler defaults to type int, so the operation is performed as a 16 bit operation. change it to:
If you take care to get the order of the bytes correct (not sure about the AVR endianness), then you can cast the array as a pointer to long and dereference it:
adc_value=long(d[]); // <-- sketch does not compile
adc_value = * (long *) d; // try this instead
An equivalent way to do this is using a union
union foo {
byte as_array[4];
long as_long;
}
d;
d.as_array[0]=0x87;
d.as_array[1]=0x65;
d.as_array[2]=0x43;
d.as_array[3]=0x21;
Serial.println(d.as_long,HEX);
If you get weird values, just put in one byte to work out if it goes into element 0 or element 3 of the array.