Main and Frost Temperature control with two thermistors

I am totally new to programming and have been learning plenty just by playing with the kit. I am trying to control two digital outputs one for frost heat and one for main heater. I have two thermistor inputs and a four line lcd display unit. Line one is totally fine it shows the temperaure reading of the thermistor and the set point of the analogue input , the digital output switches when the temperaure is lower than the set point, however line two just isn't working, it is as if the loop stops as soon as it finishes the code for the main heater, it is probaly something really silly I am doing but I have tried loads to get it to work and no luck.
Any help would be greatly appreciated.

#define THERMISTOR_PIN A0
#define SET_POINT_PIN A1
#define DIGITAL_OUTPUT_PIN 13
#define THERMISTOR2_PIN A2
#define SET_POINT2_PIN A3
#define DIGITAL_OUTPUT2_PIN 6
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  lcd.begin(20, 4);
  pinMode(DIGITAL_OUTPUT_PIN, OUTPUT);
  pinMode(DIGITAL_OUTPUT2_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int thermistorReading = analogRead(THERMISTOR_PIN);
  int setPointReading = analogRead(SET_POINT_PIN);
  float temperature = calculateTemperature(thermistorReading);
  float setPoint = calculateSetPoint(setPointReading);
  double tempK = log(10000.0 * ((1024.0 / thermistorReading - 1)));
  tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );       //  Temp Kelvin
  float tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  /*  replaced
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 10.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
   
  */
  // Display Temperature in C
    float setPointdisplay = (setPoint);
  lcd.setCursor(0, 0);
  lcd.print("Temp:    C ");
  // Display Temperature in F
  //lcd.print("Temp         F  ");
  lcd.setCursor(5, 0);
  // Display Temperature in C
  lcd.print(tempC, 1);
  // Display Temperature in F
  //lcd.print(tempF);
  
  // Display setpoint in C
  lcd.setCursor(12, 0);
   delay(500);
  lcd.print("SP:   C");
  // Display Temperature in F
  //lcd.print("Set       F  ");
  lcd.setCursor(15, 0);
  // Display Temperature in C
  lcd.print(setPointdisplay, 0);
  // Display Temperature in F
  //lcd.print(tempF);
   delay(500);
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" Set point: ");
  Serial.println(setPoint);

  if (tempC < setPoint) {
    digitalWrite(DIGITAL_OUTPUT_PIN, HIGH);
  } else {
    digitalWrite(DIGITAL_OUTPUT_PIN, LOW);
  }
}

float calculateTemperature(int reading) {
  float resistance = 10000.0 / ((1023.0 / reading) - 1.0);
  float temperature = log(resistance / 10000.0) / 3950.0;
  temperature = temperature + (1.0 / (25.0 + 273.15));
  temperature = 1.0 / temperature;
  temperature = temperature - 273.15;
  return temperature;
}

float calculateSetPoint(int reading) {
  float setPoint = map(reading, 0, 1023, 0, 50);
 
  return setPoint;
  
{
{
 int thermistorReading = analogRead(THERMISTOR2_PIN);
  int setPointReading = analogRead(SET_POINT2_PIN);
  float temperature = calculateTemperature(thermistorReading);
  float setPoint2 = calculateSetPoint(setPointReading);
  double tempK = log(10000.0 * ((1024.0 / thermistorReading - 1)));
  tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );       //  Temp Kelvin
  float tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  /*  replaced
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 10.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
   
  */
  // Display Temperature in C
    float setPointdisplay2 = (setPoint2);
  lcd.setCursor(0, 1);
  lcd.print("Temp:    C ");
  // Display Temperature in F
  //lcd.print("Temp         F  ");
  lcd.setCursor(5, 1);
  // Display Temperature in C
  lcd.print(tempC, 1);
  // Display Temperature in F
  //lcd.print(tempF);

  // Display setpoint in C
  lcd.setCursor(12, 1);
   delay(500);
  lcd.print("SP:   C");
  // Display Temperature in F
  //lcd.print("Set       F  ");
  lcd.setCursor(15, 1);
  // Display Temperature in C
  lcd.print(setPointdisplay2, 0);
  // Display Temperature in F
  //lcd.print(tempF);
  delay(500);
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" Set point: ");
  Serial.println(setPoint);
   if (tempC < setPoint) {
    digitalWrite(DIGITAL_OUTPUT2_PIN, HIGH);
  } else {
    digitalWrite(DIGITAL_OUTPUT2_PIN, LOW);
   }
}

float calculateTemperature2(int reading); 
  float resistance = 10000.0 / ((1023.0 / reading) - 1.0);
  float temperature = log(resistance / 10000.0) / 3950.0;
  temperature = temperature + (1.0 / (25.0 + 273.15));
  temperature = 1.0 / temperature;
  temperature = temperature - 273.15;
  return temperature;
}


float calculateSetPoint(int reading);  
  float setPoint2 = map(reading, 0, 1023, 0, 50);
 
  return setPoint2;
}

It could be the LCD: Does the 4-line LCD work with a test program?

It could be the power/wiring: Can you make the outputs work with a simple button+LED-like script?

It could be the code: Add some Serial.print(...) stuff around where "it finishes the code for the main heater,"

Hello DaveX, thank you for your reply, The display is working fine as I changed the code so different parts of the lcd.print for the main heater were set to display on different lines and they displayed fine.

not sure what Serial.print (...) should be but I will go and do some investigating.

So you figured it out and everything works OK now?

1 Like

If you properly format your code (Ctrl-T in the IDE) then you will see that several of your functions near the end of the sketch are inside other functions. Also, you have a function that converts your ADC reading to temperature and then you have the same code inside loop()???

#define THERMISTOR_PIN A0
#define SET_POINT_PIN A1
#define DIGITAL_OUTPUT_PIN 13
#define THERMISTOR2_PIN A2
#define SET_POINT2_PIN A3
#define DIGITAL_OUTPUT2_PIN 6
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  lcd.begin(20, 4);
  pinMode(DIGITAL_OUTPUT_PIN, OUTPUT);
  pinMode(DIGITAL_OUTPUT2_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int thermistorReading = analogRead(THERMISTOR_PIN);
  int setPointReading = analogRead(SET_POINT_PIN);
  float temperature = calculateTemperature(thermistorReading);
  float setPoint = calculateSetPoint(setPointReading);

  // Display Temperature in C
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature, 1);
  lcd.print(" C  ");

  lcd.setCursor(0, 1);
  lcd.print("SP: ");
  lcd.print(setPoint, 0);
  lcd.print(" C  ");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" Set point: ");
  Serial.println(setPoint);

  lcd.setCursor(0, 2);
  if (temperature < setPoint) {
    digitalWrite(DIGITAL_OUTPUT_PIN, HIGH);
    Serial.println("ON");
    lcd.print("ON ");
  } else {
    digitalWrite(DIGITAL_OUTPUT_PIN, LOW);
    Serial.println("OFF");
    lcd.print("OFF");
  }
  delay(500);
}

float calculateTemperature(int reading) {
  float resistance = 10000.0 / ((1023.0 / reading) - 1.0);
  float temperature = log(resistance / 10000.0) / 3950.0;
  temperature = temperature + (1.0 / (25.0 + 273.15));
  temperature = 1.0 / temperature;
  temperature = temperature - 273.15;
  return temperature;
}

float calculateSetPoint(int reading) {
  float setPoint = map(reading, 0, 1023, 0, 50);

  return setPoint;
}

Sometimes I would use prints like Serial.print('H'); for main heater, Serial.print('t') for thermistors, etc., to help me identify where something breaks and what it is. (E.g the commented out-Serial.print()s in UnoClockScope - Wokwi ESP32, STM32, Arduino Simulator show you how much faster the loop() goes than the clock detection or report().)

If you're still having problems, I'd consider designing how often you want to read temperatures (every 50ms? Serial.print('t')?) and update the outputs (every time the inputs change? Serial.print('o')?) and update the LCD (every 0.5sec? Serial.print('d')?) and then separate them into their own distinct parts as per Demonstration code for several things at the same time since if you break it into components and interactions, it's easier to figure out what's going wrong.

Thank you for your reply, I am totally new to this so am learning every day really greatful for your comments.

Thank you DaveX that is great

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