analogRead function, weird values

I’ve setup my arduino duemilanove to output to 3 digit 7-segment leds. I can print out any 3 digit combination manually. Now I want to use analog input from a temperature sensor to print out the numbers but I’m having problems with the analogRead function.

If I use the analogRead function, the LEDs print out gibberish.

If I comment out the analogRead function and manually set the digits, the LEDs output correctly.

This is my code for reading the temperature sensor:

getLocalTemperature(0);

.
.
.

byte* getLocalTemperature(int localTemperatureSensorPin)
{
    int tempReading = 0;
    float tempRatio = 0;
    float temp2Kelvin = 0;
    float temperature = 0;
    byte temperatureArray[3];
    
    tempReading = analogRead(localTemperatureSensorPin);
    
    tempRatio = (tempReading/1023)*5;
    temp2Kelvin = tempRatio/(0.0001);
    temperature = ((temp2Kelvin - 273) * 1.8) + 32;
    
    temperatureArray[0] = dataArray[5];
    temperatureArray[1] = dataArray[0];
    temperatureArray[2] = dataArray[5];
    
    return temperatureArray;
}

The temperatureArray data will be used for the 3 7-segment LEDs. Right now I have manually entered data for it, but it’s showing gibberish if the analogRead() function is uncommented.

Do I need to set pinMode for the analog input?

The expesssion “tempReading/1023” divides one integer by another probably resulting in an integer division, not a floating point division. Try change tempReading to a floating point and make the constant 1023 a real as in 1023.0

in small micros, commonly one avoids floating point. Too memory intensive.

So multiply the values to scale up, and imply the decimal point. You can print as if pseudo floating point with printf().

x = n * 1000 + frac; // integers, or longs

… later

printf(“%d.%04d\n\r”, x/1000, x % 1000);

nonfiction:
I’ve setup my arduino duemilanove to output to 3 digit 7-segment leds. I can print out any 3 digit combination manually. Now I want to use analog input from a temperature sensor to print out the numbers but I’m having problems with the analogRead function.

If I use the analogRead function, the LEDs print out gibberish.

If I comment out the analogRead function and manually set the digits, the LEDs output correctly.

This is my code for reading the temperature sensor:

getLocalTemperature(0);

.
.
.

byte* getLocalTemperature(int localTemperatureSensorPin)
{
int tempReading = 0;
float tempRatio = 0;
float temp2Kelvin = 0;
float temperature = 0;
byte temperatureArray[3];

tempReading = analogRead(localTemperatureSensorPin);

tempRatio = (tempReading/1023)*5;
temp2Kelvin = tempRatio/(0.0001);
temperature = ((temp2Kelvin - 273) * 1.8) + 32;

temperatureArray[0] = dataArray[5];
temperatureArray[1] = dataArray[0];
temperatureArray[2] = dataArray[5];

return temperatureArray;

}






The temperatureArray data will be used for the 3 7-segment LEDs. Right now I have manually entered data for it, but it's showing gibberish if the analogRead() function is uncommented.

Do I need to set pinMode for the analog input?

Try making “byte temperatureArray[3];” defined in the definitions portion of the program. I’m wondering this as you are returning a pointer to an array that is on the stack (or heap, not sure of name) as it is local, and that gets destroyed when you exit the fuction.

i think nonfiction has the right idea. instead, pass into the function the pointer to where you want the results stored. something like:

void getLocalTemperature(int localTemperatureSensorPin, byte* destArray){
   destArray[n] = x;
}

byte temperatureArray[3]; 

getLocalTemperature(0, temperatureArray);

How are you? I would like to Thanks for the informative post. I really appreciate it. I hope that I can get more benefit from this topic.

Thanks