Simple sketch hangs when calling math log() function?

I have an artemis redboard with a normal thermistor connected to pin A0. I have no problems reading values from the ADC with analogRead(), but when I try to convert that ADC value to a temperature my sketch ultimately hangs when calling the log() function. The sketch is pretty simple (below). I’ve tried various things in the adcToTemp() method, such as using floats vs. double, etc., but the sketch will always hang when calling log(). You can also see where I added a call to log() in the sketch’s setup method - that one works fine. I’m not sure what I’m missing here?

#define thermistorAdcPin A0

void setup()
{
  Serial.begin(115200);
  delay(1000);

  float test = log(22579.617188);
  Serial.print("\nInitial float test: "); Serial.println(test);
}

double adcToTemp(int RawADC)
{
  long   Resistance;
  double Temp;

  Serial.print("\nConvert RAW: "); Serial.println(RawADC);

  Resistance = ((10240000 / RawADC) - 10000);
  Serial.print("Convert Resistance: "); Serial.println(Resistance);

  Temp = log(Resistance); // <--- this line causes the hang
  Serial.print("Convert Temp: "); Serial.println(Temp);
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;
  Temp = (Temp * 9.0) / 5.0 + 32.0;

  return Temp;
}

void loop()
{
  int adcTempVal = analogRead(thermistorAdcPin);

  Serial.print("Temp ADC Pin:   "); Serial.println(adcTempVal);
  Serial.print("Temp F:         "); Serial.println(adcToTemp(adcTempVal));
}

The output is:

16:09:20.659 -> ⸮⸮
16:09:21.699 -> Initial float test: 10.02
16:09:21.699 -> Temp ADC Pin:   780
16:09:21.699 -> Temp F:         
16:09:21.699 -> Convert RAW: 780
16:09:21.699 -> Convert Resistance: 3128

Thanks for the help!

Chris

I don’t know anything about the Artemis. But do you not need to include some kind of math-library to be able to use the log function?

Thanks for the reply.

I’ve tried both with and without explicitly including math.h with no change in behavior. And if that was the problem wouldn’t I see the sketch hang in the setup() method where I seem to have no problem with calling log()?

cdollar:
Thanks for the reply.

I’ve tried both with and without explicitly including math.h with no change in behavior. And if that was the problem wouldn’t I see the sketch hang in the setup() method where I seem to have no problem with calling log()?

You’ve got a point there. Must be something else then. I did notice in Setup() you calculate the log function as:

float test = log(22579.617188);

22579.617188 is a floating point explicit value. So that is calculated without issues.

But in AdcToTemp you calculate it on a variable of type long, and store it as double. There may ba a type casting issue. try:

double Temp = log ( (double) Resistance);

This first converts the Resistance variable to type double, then execute the log function on it, and stores it as double.

You are exactly right. After casting to a double my sketch seems to be working as expected. Thanks!!