8 years, 4 months ago.

subscript out of range Xbee+LM35

i have a little problem that i cannot solve i connected the mbed with LM35 (temprature sensor) and Xbee Pro. i tried to send the temprature data through xbee pro. this is the code,

LM35_Mbed_Xbee

#include "mbed.h"
#include "xbee.h"

//Print temperature from LM35 analog temperature sensor

//set p19 to analog input to read LM35 sensor's voltage output
AnalogIn LM35(p19);
xbee xbee1(p9,p10,p11); //Initalise xbee_lib
Serial pc(USBTX, USBRX); //Initalise PC serial comms

//also setting unused analog input pins to digital outputs reduces A/D noise a bit
//see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
/*DigitalOut P16(p16);
DigitalOut P17(p17);
DigitalOut P18(p18);
DigitalOut P19(p19);
DigitalOut P20(p20);*/

int main()
{
    float tempC[202];
    //char b[202];
        
    while(1) {
        //conversion to degrees C - from sensor output voltage per LM61 data sheet
        tempC[202] = ((LM35*3.3)-0.6)*100.0;
        pc.printf("%5.2f C\n", tempC);
        wait(1);
        xbee1.SendData(tempC);
        }
}

the code compiled succesfully but i got some warns like "Subscript out of range" in file "/main.cpp", Line: 26, Col: 0 and "C3488E: write to variable 'tempC' with offset out of bounds" in file "/main.cpp", Line: 36, Col: 0 do you have any idea to solve this problem?

thank you

1 Answer

8 years, 4 months ago.

tempC is an array of 202 floats (so 808 bytes). You assign a value to 202, but with an array of length 202 the indices run from 0 to 201. So you can't assign anything beyond 201 (at least not without having a good chance on a crash and/or really weird behavior).