Why is My Function Not Working?

Hi everyone, I am new to Arduino and don't understand why my code is giving me an error. I have a simple algorithm to read a thermistor from a voltage divider. I first verified that the code works on its own, then I made it into a function kept within the same .ino file and now I want it to be a separate file. I saved the function as a .cpp file and I made a .h file based off what I saw here:

here is my .ino file:

#include "readTherm.h"
// which analog pin to connect
float THERMISTORPIN = A3;          
// the value of the 'other' resistor
float SERIESRESISTOR = 1100;    
// "A" variable from S-H Equation

void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL);
}

void loop(void) {
  float T1;
  T1 = readTherm(THERMISTORPIN, SERIESRESISTOR);
  Serial.print("T1 "); 
  Serial.print(T1);
  Serial.println(" *C");
  delay(1000);
}

here is my .h file:

#ifndef readTherm_h
#define readTherm_h

readTherm();

#endif

and lastly here is my .cpp file:

float readTherm(float pin, float Rseries) {
  // how many samples to take and average, more takes longer but is more 'smooth'
  int NUMSAMPLES = 5;
  // "A" variable from S-H Equation
  float A = 1.471021608E-3;
  // "B" variable from S-H Equation
  float B = 2.376674669E-4;
  // "C" variable from S-H Equation
  float C = 1.050016627E-7;
  
  int samples[NUMSAMPLES];

  uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(pin);
   delay(10);
  }
  
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples[i];
  }
  average /= NUMSAMPLES;

  Serial.print("Average analog reading "); 
  Serial.println(average);
  
  // convert the value to resistance
  average = 1024 / average - 1;
  average = Rseries / average;
 // average = average + 200; // manual adjustment by me
  Serial.print("Thermistor resistance "); 
  Serial.println(average);
  
  float steinhart;
  float D;
  float E;
  float R;
  R = log(average);            // lnR
  D = B * R;                   // B * R
  E = C * R * R * R;           // C * R^3
  steinhart = A + D + E;       // Final S-H Equation
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;         // convert absolute temp to C

  //Serial.print("Temperature "); 
  //Serial.print(steinhart);
  //Serial.println(" *C");
  return steinhart;
}

The error I am getting says:
"exit status 1

Compilation error: 'readTherm' was not declared in this scope"

I assume there's something wrong with my header file, but as I have never made one before I have no clue what I am doing. Any and all help is greatly appreciated.

I chose a float because I am using the analog pins so I need a letter and number. If you have any better ideas please send them my way. What is wrong with using a float there?

I did that and now a different error came up: "Compilation error: exit status 1"

In case it helps, here is the full error log:

P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp: In function 'float readTherm(float, float)':
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:13:3: error: 'uint8_t' was not declared in this scope
   uint8_t i;
   ^~~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:17:8: error: 'i' was not declared in this scope
   for (i=0; i< NUMSAMPLES; i++) {
        ^
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:18:17: error: 'analogRead' was not declared in this scope
    samples[i] = analogRead(pin);
                 ^~~~~~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:19:4: error: 'delay' was not declared in this scope
    delay(10);
    ^~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:19:4: note: suggested alternative: 'decltype'
    delay(10);
    ^~~~~
    decltype
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:24:8: error: 'i' was not declared in this scope
   for (i=0; i< NUMSAMPLES; i++) {
        ^
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:29:3: error: 'Serial' was not declared in this scope
   Serial.print("Average analog reading ");
   ^~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:43:7: error: 'log' was not declared in this scope
   R = log(average);            // lnR
       ^~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:43:7: note: suggested alternative: 'long'
   R = log(average);            // lnR
       ^~~
       long

exit status 1

Compilation error: exit status 1

I added #include <arduino.h> and it still has the same error

That doesn't make it a float!!! Check out the definition of A3 in pins_arduino.h (assuming an Uno):

#define PIN_A3   (17)

static const uint8_t A3 = PIN_A3;

Same error comes up with arrows, quotes, or not even having the #include line

BTW, for a basic guide on that topic, see My Post #5 in this Thread

.ino file (nothing has changed from the original post)

#include "readTherm.h"
// which analog pin to connect
float THERMISTORPIN = A3;          
// the value of the 'other' resistor
float SERIESRESISTOR = 1100;    
// "A" variable from S-H Equation

void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL);
}

void loop(void) {
  float T1;
  T1 = readTherm(THERMISTORPIN, SERIESRESISTOR);
  Serial.print("T1 "); 
  Serial.print(T1);
  Serial.println(" *C");
  delay(1000);
}

.h file (I added the #include part)

#ifndef readTherm_h
#define readTherm_h

#include "arduino.h"
float readTherm(float, float);

#endif

.cpp file (nothing has changed here)

float readTherm(float pin, float Rseries) {
  // how many samples to take and average, more takes longer but is more 'smooth'
  int NUMSAMPLES = 5;
  // "A" variable from S-H Equation
  float A = 1.471021608E-3;
  // "B" variable from S-H Equation
  float B = 2.376674669E-4;
  // "C" variable from S-H Equation
  float C = 1.050016627E-7;
  
  int samples[NUMSAMPLES];

  uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(pin);
   delay(10);
  }
  
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples[i];
  }
  average /= NUMSAMPLES;

  Serial.print("Average analog reading "); 
  Serial.println(average);
  
  // convert the value to resistance
  average = 1024 / average - 1;
  average = Rseries / average;
 // average = average + 200; // manual adjustment by me
  Serial.print("Thermistor resistance "); 
  Serial.println(average);
  
  float steinhart;
  float D;
  float E;
  float R;
  R = log(average);            // lnR
  D = B * R;                   // B * R
  E = C * R * R * R;           // C * R^3
  steinhart = A + D + E;       // Final S-H Equation
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;         // convert absolute temp to C

  //Serial.print("Temperature "); 
  //Serial.print(steinhart);
  //Serial.println(" *C");
  return steinhart;
}

Error Log

P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp: In function 'float readTherm(float, float)':
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:13:3: error: 'uint8_t' was not declared in this scope
   uint8_t i;
   ^~~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:17:8: error: 'i' was not declared in this scope
   for (i=0; i< NUMSAMPLES; i++) {
        ^
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:18:17: error: 'analogRead' was not declared in this scope
    samples[i] = analogRead(pin);
                 ^~~~~~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:19:4: error: 'delay' was not declared in this scope
    delay(10);
    ^~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:19:4: note: suggested alternative: 'decltype'
    delay(10);
    ^~~~~
    decltype
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:24:8: error: 'i' was not declared in this scope
   for (i=0; i< NUMSAMPLES; i++) {
        ^
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:29:3: error: 'Serial' was not declared in this scope
   Serial.print("Average analog reading ");
   ^~~~~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:43:7: error: 'log' was not declared in this scope
   R = log(average);            // lnR
       ^~~
P:\Daniel Sollima\Arduino\TempSense\readTherm.cpp:43:7: note: suggested alternative: 'long'
   R = log(average);            // lnR
       ^~~
       long

exit status 1

Compilation error: exit status 1

I am currently using an UNO but I will need to used a MEGA later because I will need more inputs.

AHA! I GOT IT! Thank you @gfvalvo it was your post you linked that gave me the solution. I didn't have the #include "readTherm.h" in my .cpp file.

Thank you @Delta_G for trying so hard to help me. I will change my pin number from a float to an int. If there are any other silly mistakes like that in which you can see please tell me. I am brand brand new to Arduino and there is a lot to learn

Please fix the incorrect function arguments. As you already advised, you don't need a floats

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.