10 years, 10 months ago.  This question has been closed. Reason: has been answered

LM35+Xbee PRO+MBED = subscript out of range

hi guys, i am working with temprature sensor(LM35), an xbee pro, and an mbed right now. i want to make a very simple prototype of nanosatellite. the system will measure the prototype temprature and transmit the temprature data using Xbee Pro. as the ground segment, i just using Xbee PRO+Xbee adapter+XTCU software. i tried to make the program based on the cookbook, modified it, and here it is, the program. but the problem is, when i compiled it, there are two warning statement.

  1. subscript out of range
  2. write to variable 'tempC' with offset out of bounds

main.cpp

#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;
        printf("%5.2f C \n",tempC[202]);
        memcpy(b, &tempC, 202);
        wait(1);
        xbee1.SendData(b);
        }
}

i have no idea to resolve these problems. is there any idea to resolve these problems?

2 Answers

10 years, 10 months ago.

It is handy to mention where the error is found, then we know where to look.

Anyway you have two buffers of 202 bytes. You use only tempC[202], but if a buffer is 202 bytes long it is defined from zero to 201 bytes, 202 is outside its scope.

Problem two is you copy tempC into b. However tempC is a float, and a float is 4 bytes long, while b is a char, and a char is a single byte. So that doesn't fit.

Accepted Answer
10 years, 10 months ago.

thank you very much, it works well!!!