Problems with making multi cpp file (platformio)

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?

I always start local Variables with an underscore e.g. _MyVar

Only global variable declarations belong in the .h file (using 'extern'), NEVER variable definitions. See the difference here:

Also, here are additional guidelines:

Has nothing to do with OP's problem.

Ok i took a look at these links and other resources and i get it a bit more now, i got it to compile,

temp_calc.cpp

#include "Arduino.h"
#include "temp_calc.h"

#define Thermistor2Pin A0 // pin that's connected to thermistor2
#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 = 32;
int ThermistorValue = 0, ThermistorValue1 = 0, ThermistorValue2 = 0;
int readingsHeatblockTemperature[numReadings], readIndex = 0;
long total = 0, average = 0;

void Calculate_Temperature(int Thermistor_Pin, double Temperature)
{
    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
    }
}

temp_calc.h

pragma once

#ifndef __TEMP_H

#define __TEMP_H

extern void Calculate_Temperature(int Thermistor_Pin, double Temperature);

#endif

And in the main cpp i defined a global variable for thermistor pin and i called the function in loop.

Calculate_Temperature(Thermistor1Pin,
HeatblockTemperature);

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?

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