Hello,
I’ve been consistently getting Nan’s on a straightforward approach to accessing floats stored a char array. I’m using the Raspberry Pi 3 device with apt-get update / upgrade performed recently, and also updated (or installed) the latest particle-agent with a recent update from this forum.
This is the sketch:
#define N_FLOATS 10
const int bufLen=40;
char buf[40]; // floats are 4 bytes on Arduino
float *myFloat;
unsigned long cnt=0;
void setup() {
Serial.begin(250000);
memset(buf,0,bufLen); // zero-out buf array
myFloat = (float*) &buf[0]; // assign float pointer to beginning of char array
}
void loop() {
cnt++;
Serial.print("cnt=");
Serial.print(cnt);
Serial.print(", buf=[");
for (int i=0; i<N_FLOATS; i++){
myFloat[i] = (float) i + (float) cnt/10.0;
Serial.print( myFloat[i] );
Serial.print(", ");
}
Serial.println("]");
delay(1000);
}
This code works exactly as-is on the Arduino Uno R3. Copy-pasting directly from the Arduino IDE into particle web-IDE generates this output:
cnt=1, buf=[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, ]
cnt=2, buf=[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, ]
cnt=3, buf=[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, ]
cnt=4, buf=[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, ]
.
.
.
The exact same sketch on Arduino generates the expected result:
cnt=1, buf=[0.10, 1.10, 2.10, 3.10, 4.10, 5.10, 6.10, 7.10, 8.10, 9.10, ]
cnt=2, buf=[0.20, 1.20, 2.20, 3.20, 4.20, 5.20, 6.20, 7.20, 8.20, 9.20, ]
cnt=3, buf=[0.30, 1.30, 2.30, 3.30, 4.30, 5.30, 6.30, 7.30, 8.30, 9.30, ]
cnt=4, buf=[0.40, 1.40, 2.40, 3.40, 4.40, 5.40, 6.40, 7.40, 8.40, 9.40, ]
.
.
.
This looks like a bug somewhere in the raspberrry pi particle binary. Perhaps it’s a compiler option.
Not sure. But this is standard C and should work.
Why no workee???
Marc