I am trying to learn to split my cpp code to make it easier to read so i tried to make just measuring temperature with themistors for beggining.
I made a header file named temp_calc.h
#pragma once
#ifndef __TEMP_CALC__
#define __TEMP_CALC__
#define Thermistor_Pin A1 // pin that's connected to thermistor1
#define R1 470
float logR2, R2, c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; // constants for calculating temperature
double Temperature = 0;
const int numReadings = 16;
int ThermistorValue = 0, ThermistorValue1 = 0, ThermistorValue2 = 0;
int readingsHeatblockTemperature[numReadings], readIndex = 0;
long total = 0, average = 0;
double Calculate_Temperature(int Thermistor_Pin);
#endif
temp_calc.cpp
#include "Arduino.h"
#include "temp_calc.h"
double Calculate_Temperature(int Thermistor_Pin)
{
for (int i = 0; i < numReadings; i++)
{
total = total - readingsHeatblockTemperature[readIndex];
readingsHeatblockTemperature[readIndex] = analogRead(Thermistor_Pin);
total = total + readingsHeatblockTemperature[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
{
readIndex = 0;
}
average = total / numReadings;
ThermistorValue = average;
R2 = R1 * ((1023.0 / ThermistorValue) - 1.0);
logR2 = log(R2); // math shit
Temperature = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); // temperature in kelvins
Temperature = Temperature - 273.15; // temperature in celsius
// HeatblockTemperature = (HeatblockTemperature * 9.0) / 5.0 + 32.0; // temperature in fanrenheit
}
return Temperature;
}
and in the main cpp file i just call the function with HeatblockTemperature1 = Calculate_Temperature(Thermistor1Pin); HeatblockTemperature2 = Calculate_Temperature(Thermistor2Pin);
I have heatblockTemperature variables defined in the main cpp, i get errors that there are duplicates/multiple definitons of variables, is this because i named the header and the cpp file that holds the function the same name?
It compiled but haven't tested yet if it really works, i have some more questions, i usually define pin names with #define, but i don't know how to put define in function as a parameter?
And if i put void before function that tells the compiler the function won't return any values, would it be better if i put double before function and make it return value, i'm not sure how it's done correctly?