Go Down

Topic: math with float not working (Read 492 times) previous topic - next topic

This code was working, but now all I get is 0.00 for temperature  :~ and I am confused about what happened

For these two routines, output is:
x2: 296.78, ReadTemperatureCal: 0.00°C
x3: 0.00  <<---- why is this zero? should be 23.63°C

Code: [Select]
float ReadTemperatureCal(void) {
  // reads calibration factor from EEPROM; float max. -10.0 - +10.0 degree offset range
  float Cal;
   Cal= EEPROMreadFloat(CAL_EEADDR); // read cal factor
   if ((Cal > 10.0) || (Cal < -10.0)) { // if garbage in fresh EEPROM
     Cal=0.0; //set to zero
     EEPROMwriteFloat(CAL_EEADDR, Cal); // save it;
     Serial.println("Reset Cal Factor to 0.0");
   Serial.print("ReadTemperatureCal: ");  Serial.print(Cal,2); Serial.print(0xB0,BYTE); Serial.println("C");
   return Cal;
double Thermistor(int RawADC) {

  double Temp;
  // The coefficients below for 10K @ 25C thermistor from Radio Shack 271-110   
  Temp = log(((30720000/RawADC) - 30000)); //30k pulldown
  // S-H coefficients
  Temp = 1 / (0.00090145879873 + (0.00024921185913 * Temp) + (0.00000020123928 * Temp * Temp * Temp));
    Serial.print("x2: ");   Serial.print(Temp,2); Serial.print(", ");
  Temp = Temp - 273.15 - ReadTemperatureCal();         // Convert Kelvin to Celcius
    Serial.print("x3: ");   Serial.println(Temp,2);
  return Temp;


I did a quick test, and the maths works as expected.

So "It suddenly stopped working" - the classic counter question is : What did you do ?

My best guess is that you are overflowing RAM, this usually results in weird behaviour. How large is the overall program? The size of code (~number of lines) isnt what I am asking about, but how many arrays are you using? Many strings used in print will break the ~1.5K you have. Including a library may have a large RAM-memory usage. Search for the "MemoryFree" routine and use that.

Thanks for verifying the code  :smiley-surprise:  I was running a Mega328 and tried it on a Mega2560, as a quick check if more RAM would help, but no change. It has to be a side-effect, stack mismatch. I found the EEPROM read routine seems to be the culprit, having a Union declared inside (?)
Code: [Select]
float EEPROMreadFloat(int address) {
  union floatStore {
    byte floatByte[4];
    float floatVal;
  } floatOut;
  for (int i = 0; i < 4; i++)
    floatOut.floatByte[i] = EEPROM.read(address + i);
  return floatOut.floatVal;

I moved the union declaration up to the top of the program it started working. I hope that's it. I checked all other routines for in/out variable passing and couldn't see anything off. I assume doubles and floats are the same (size).

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131