0

I'm trying to get my code to read multiple thermistors and I can't quite seem to get it correct using my arduino 2560. The first issue is it's not reading more than the first thermistor. The next issue is the for loop that displays the current temperature always reads "TURN OFF" when its below the 60C range.

Here is my code:

/* Here we have a few constants that make editing the code easier. I will go
 through them one by one.
 A reading from the ADC might give one value at one sample and then a little
 different the next time around. To eliminate noisy readings, we can sample
 the ADC pin a few times and then average the samples to get something more 
 solid. This constant is utilized in the readThermistor function. 
 */
const int SAMPLE_NUMBER = 10;
/* In order to use the Beta equation, we must know our other resistor
 within our resistor divider. If you are using something with large tolerance,
 like at 5% or even 1%, measure it and place your result here in ohms. */
const double BALANCE_RESISTOR0 = 10060.0; //
const double BALANCE_RESISTOR1 = 10000.0;
const double BALANCE_RESISTOR2 = 10000.0;
const double BALANCE_RESISTOR3 = 10000.0;
const double BALANCE_RESISTOR4 = 10000.0;
const double BALANCE_RESISTOR5 = 10000.0;
const double BALANCE_RESISTOR6 = 10000.0;
const double BALANCE_RESISTOR7 = 10000.0;
const double BALANCE_RESISTOR8 = 10000.0;
const double BALANCE_RESISTOR9 = 10000.0;
// This helps calculate the thermistor's resistance (check article for details).
const double MAX_ADC = 1023.0;
/* This is thermistor dependent and it should be in the datasheet, or refer to the
 article for how to calculate it using the Beta equation.
 I had to do this, but I would try to get a thermistor with a known
 beta if you want to avoid empirical calculations. */
const double BETA = 3977.0;
/* This is also needed for the conversion equation as "typical" room temperature
 is needed as an input. */
const double ROOM_TEMP = 295.950; // room temperature in Kelvin
/* Thermistors will have a typical resistance at room temperature so write this 
 down here. Again, needed for conversion equations. */
const double RESISTOR_ROOM_TEMP = 10980.0;
//===============================================================================
// Variables
//===============================================================================
// Here is where we will save the current temperature
double currentTemperature = 0;
//===============================================================================
// Pin Declarations
//===============================================================================
//Inputs:
int thermistorPin0 = 0; // Where the ADC samples the resistor divider's output
int thermistorPin1 = 1;
int thermistorPin2 = 2;
int thermistorPin3 = 3;
int thermistorPin4 = 4;
int thermistorPin5 = 5;
int thermistorPin6 = 6;
int thermistorPin7 = 7;
int thermistorPin8 = 8;
//Outputs:
//===============================================================================
// Initialization
//===============================================================================
 
void setup() 
{ 
 // Set the port speed for serial window messages
 Serial.begin(9600);
}
//===============================================================================
// Main
//===============================================================================
//===============================================================================
// Functions
//===============================================================================
/////////////////////////////
////// readThermistor ///////
/////////////////////////////
double readThermistor() 
{
 // variables that live in this function
 double rThermistor0 = 0; // Holds thermistor resistance value
 double rThermistor1 = 0;
 double rThermistor2 = 0;
 double rThermistor3 = 0;
 double rThermistor4 = 0;
 double rThermistor5 = 0;
 double rThermistor6 = 0;
 double rThermistor7 = 0;
 double rThermistor8 = 0;
 double tKelvin0 = 0; // Holds calculated temperature
 double tKelvin1 = 0;
 double tKelvin2 = 0;
 double tKelvin3 = 0;
 double tKelvin4 = 0;
 double tKelvin5 = 0;
 double tKelvin6 = 0;
 double tKelvin7 = 0;
 double tKelvin8 = 0;
 double tCelsius0 = 0; // Hold temperature in celsius
 double tCelsius1 = 0;
 double tCelsius2 = 0;
 double tCelsius3 = 0;
 double tCelsius4 = 0;
 double tCelsius5 = 0;
 double tCelsius6 = 0;
 double tCelsius7 = 0;
 double tCelsius8 = 0;
 double adcAverage0 = 0; // Holds the average voltage measurement
 double adcAverage1 = 0;
 double adcAverage2 = 0;
 double adcAverage3 = 0;
 double adcAverage4 = 0;
 double adcAverage5 = 0;
 double adcAverage6 = 0;
 double adcAverage7 = 0;
 double adcAverage8 = 0;
 
 int adcSamples0[SAMPLE_NUMBER]; // Array to hold each voltage measurement
 int adcSamples1[SAMPLE_NUMBER];
 int adcSamples2[SAMPLE_NUMBER];
 int adcSamples3[SAMPLE_NUMBER];
 int adcSamples4[SAMPLE_NUMBER];
 int adcSamples5[SAMPLE_NUMBER];
 int adcSamples6[SAMPLE_NUMBER];
 int adcSamples7[SAMPLE_NUMBER];
 int adcSamples8[SAMPLE_NUMBER];
 /* Calculate thermistor's average resistance:
 As mentioned in the top of the code, we will sample the ADC pin a few times
 to get a bunch of samples. A slight delay is added to properly have the
 analogRead function sample properly */
 
 for (int i = 0; i < SAMPLE_NUMBER; i++) 
 {
 adcSamples0[i] = analogRead(thermistorPin0); // read from pin and store
 adcSamples1[i] = analogRead(thermistorPin1);
 adcSamples2[i] = analogRead(thermistorPin2);
 adcSamples3[i] = analogRead(thermistorPin3);
 adcSamples4[i] = analogRead(thermistorPin4);
 adcSamples5[i] = analogRead(thermistorPin5);
 adcSamples6[i] = analogRead(thermistorPin6);
 adcSamples7[i] = analogRead(thermistorPin7);
 adcSamples8[i] = analogRead(thermistorPin8);
 
 delay(10); // wait 10 milliseconds
 }
 /* Then, we will simply average all of those samples up for a "stiffer"
 measurement. */
 for (int i = 0; i < SAMPLE_NUMBER; i++) 
 {
 adcAverage0 += adcSamples0[i]; // add all samples up . . .
 adcAverage1 += adcSamples1[i];
 adcAverage2 += adcSamples2[i];
 adcAverage3 += adcSamples3[i];
 adcAverage4 += adcSamples4[i];
 adcAverage5 += adcSamples5[i];
 adcAverage6 += adcSamples6[i];
 adcAverage7 += adcSamples7[i];
 adcAverage8 += adcSamples8[i];
 
 }
 adcAverage0 /= SAMPLE_NUMBER; // . . . average it w/ divide
 adcAverage1 /= SAMPLE_NUMBER;
 adcAverage2 /= SAMPLE_NUMBER;
 adcAverage3 /= SAMPLE_NUMBER;
 adcAverage4 /= SAMPLE_NUMBER;
 adcAverage5 /= SAMPLE_NUMBER;
 adcAverage6 /= SAMPLE_NUMBER;
 adcAverage7 /= SAMPLE_NUMBER;
 adcAverage8 /= SAMPLE_NUMBER;
 
 
 
 /* Here we calculate the thermistor’s resistance using the equation 
 discussed in the article. */
 rThermistor0 = BALANCE_RESISTOR0 * ( (MAX_ADC / adcAverage0) - 1);
 rThermistor1 = BALANCE_RESISTOR1 * ( (MAX_ADC / adcAverage1) - 1);
 rThermistor2 = BALANCE_RESISTOR2 * ( (MAX_ADC / adcAverage2) - 1);
 rThermistor3 = BALANCE_RESISTOR3 * ( (MAX_ADC / adcAverage3) - 1);
 rThermistor4 = BALANCE_RESISTOR4 * ( (MAX_ADC / adcAverage4) - 1);
 rThermistor5 = BALANCE_RESISTOR5 * ( (MAX_ADC / adcAverage5) - 1);
 rThermistor6 = BALANCE_RESISTOR6 * ( (MAX_ADC / adcAverage6) - 1);
 rThermistor7 = BALANCE_RESISTOR7 * ( (MAX_ADC / adcAverage7) - 1);
 rThermistor8 = BALANCE_RESISTOR8 * ( (MAX_ADC / adcAverage8) - 1);
 /* Here is where the Beta equation is used, but it is different
 from what the article describes. Don't worry! It has been rearranged
 algebraically to give a "better" looking formula. I encourage you
 to try to manipulate the equation from the article yourself to get
 better at algebra. And if not, just use what is shown here and take it
 for granted or input the formula directly from the article, exactly
 as it is shown. Either way will work! */
 tKelvin0 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor0 / RESISTOR_ROOM_TEMP)));
 tKelvin1 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor1 / RESISTOR_ROOM_TEMP)));
 tKelvin2 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor2 / RESISTOR_ROOM_TEMP)));
 tKelvin3 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor3 / RESISTOR_ROOM_TEMP)));
 tKelvin4 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor4 / RESISTOR_ROOM_TEMP)));
 tKelvin5 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor5 / RESISTOR_ROOM_TEMP)));
 tKelvin6 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor6 / RESISTOR_ROOM_TEMP)));
 tKelvin7 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor7 / RESISTOR_ROOM_TEMP)));
 tKelvin8 = (BETA * ROOM_TEMP) / 
 (BETA + (ROOM_TEMP * log(rThermistor8 / RESISTOR_ROOM_TEMP)));
 /* I will use the units of Celsius to indicate temperature. I did this
 just so I can see the typical room temperature, which is 25 degrees
 Celsius, when I first try the program out. I prefer Fahrenheit, but
 I leave it up to you to either change this function, or create
 another function which converts between the two units. */
 tCelsius0 = tKelvin0 - 273.15; // convert kelvin to celsius 
 tCelsius1 = tKelvin1 - 273.15;
 tCelsius2 = tKelvin2 - 273.15;
 tCelsius3 = tKelvin3 - 273.15;
 tCelsius4 = tKelvin4 - 273.15;
 tCelsius5 = tKelvin5 - 273.15;
 tCelsius6 = tKelvin6 - 273.15;
 tCelsius7 = tKelvin7 - 273.15;
 tCelsius8 = tKelvin8 - 273.15;
 return tCelsius0; // Return the temperature in 
 return tCelsius1;
 return tCelsius2;
 return tCelsius3;
 return tCelsius4;
 return tCelsius5;
 return tCelsius6;
 return tCelsius7;
 return tCelsius8;
 
}
void loop() 
{
 /* The main loop is pretty simple, it prints what the temperature is in the
 serial window. The heart of the program is within the readThermistor
 function. */
 currentTemperature = readThermistor();
 delay(3000);
 /* Here is how you can act upon a temperature that is too hot,
 too cold or just right. */
 if (currentTemperature > 0.0 && currentTemperature < 60.0)
 {
 Serial.print("It is ");
 Serial.print(currentTemperature);
 
 } 
 else (currentTemperature >= 60.0);
 
 {
 Serial.print("It is ");
 Serial.print(currentTemperature);
 Serial.println("C. SHUT OFF!");
 } 
}

I'm no expert in coding, so if you have any suggestions that could help, I would very much appreciate it.

jsotola
1,5342 gold badges12 silver badges20 bronze badges
asked Apr 19, 2022 at 18:38
1
  • 1
    your code complexity would be reduced if you used arrays for all variables, because loops could be used to simplify the code ... tKelvin[0] Commented Apr 19, 2022 at 20:13

1 Answer 1

2

Welcome to Stack Exchange!

The problem causing the first issue is your multiple return statements in the readThermistor function. return exits the function, so when the code runs return tCelsius0 it will exit the function and not run any of the later return statements.

Try storing your temperatures in an array and then returning that array. However, you also only read one of the values in the main() function anyway.

The problem causing the second issue is you have an if and an else. To properly check for your second condition currentTemperature >= 60.0, you need to have an else if instead.

answered Apr 19, 2022 at 19:28
1
  • 1
    Thank you very much. Commented Apr 19, 2022 at 20:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.