Coding Help For A Simple Temperature Relay

Hey all I've had my Arduino a total of 2 hours and have built a simple LCD temperature sensor to trip a relay at certain temperatures. The problem I've run into is that the temperature as my int "temp1" won't refresh unless a button is pressed from my understanding that section of code should run every 1 second (approximately) I need temp one to refresh without button presses

The code is as follows and thanks for any help! (Sorry I know I know comments and correct formatting)

#include <LiquidCrystal_I2C.h>
#include <AverageThermocouple.h>
#include <MAX6675_Thermocouple.h>
#include <SmoothThermocouple.h>
#include <Thermocouple.h>
#include <Wire.h>


const long interval = 1000; 
unsigned long previousMillis = 0;
int thermo1D0 = 4; // so
int thermo1CS = 5; //so
int thermo1CLK = 6; // sck
const int buttonPin1 =1;
const int buttonPin2 =2;
int button_State = 0;
int button_State2 = 0;
float temp1 = 0;
float tempSet_value = 0;
int boo = 0;
MAX6675_Thermocouple thermocouple1(thermo1CLK, thermo1CS, thermo1D0);
LiquidCrystal_I2C lcd(0x27,20,4);

void setup()
{
  Serial.begin(9600); 
  lcd.init();  //initialize the lcd
  lcd.setCursor ( 0, 0 );            // go to the top left corner
  lcd.backlight();  //open the backlight 
  pinMode(13, OUTPUT);    // sets the digital pin 13 as output
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  
}
void loop()
{
  
  int temp1 = round(thermocouple1.readFahrenheit());
  button_State = digitalRead(buttonPin1);
  button_State2 = digitalRead(buttonPin2);
  unsigned long currentMillis = millis();

   if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; 
    lcd.clear();
    lcd.print("Current Temp  ");
    lcd.print(temp1 - 9);
    lcd.setCursor(0, 1);
    lcd.print("Set Temp   ");
    lcd.print(tempSet_value);
    
    
   
    
   }
  
  if (button_State == LOW) {
    tempSet_value = tempSet_value + 100;
    delay(500);
  }
  
      if (button_State2 == LOW) {
    tempSet_value = tempSet_value - 100;
    delay(500);
  }
  
  if (temp1 <= tempSet_value){
      digitalWrite(13, HIGH);
  }
  if (temp1 >= tempSet_value){
    digitalWrite (13, LOW);
  }

} 

welcome to the forums. Take a moment and read the sticky post at the top of the forum to learn how to post your code with code tags. it will help people help you.

Then, with this new found knowledge, you can go back and edit your post, and include code tags.

Sorry. I didn't get and "sticky notes" at the top of my screen or on in the editing bar I had to manually add the code tags it should be fixed now.

Hello,
add an addtional Serial.println(temp1) to the timer function to see what happens.

The value "temp1 - 9" is displayed about once a second, possibly delayed up to half a second if a button is pressed because of the delay(500). What do you mean by "won't refresh"?

Why are you displaying "temp1 - 9" but comparing "tempSet_value" with "temp1"? Doesn't that mean your setpoint is 9 degrees above the temperature you display?

Yep temp1 is -9 degrees just for calibration issues with the thermocouple I have currently. I mean by won't refresh as in the LCD doesn't update temp1 every second like it should.

Classical approach in such a problem: try to narrow down the origin of the problem.
so replace the reading of temp1 by an incrementing value and see what it gives:

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const long interval = 1000;
unsigned long previousMillis = 0;
const int buttonPin1 = 1;
const int buttonPin2 = 2;
int button_State = 0;
int button_State2 = 0;
int temp1;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
{
  Serial.begin(9600);
  lcd.init();  //initialize the lcd
  lcd.setCursor ( 0, 0 );            // go to the top left corner
  lcd.backlight();  //open the backlight
  pinMode(13, OUTPUT);    // sets the digital pin 13 as output
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

}
void loop()
{
  temp1++;
  button_State = digitalRead(buttonPin1);
  button_State2 = digitalRead(buttonPin2);
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) 
  {
    previousMillis = currentMillis;
    lcd.clear();
    lcd.print("Current Temp  ");
    lcd.print(temp1 - 9);
    lcd.setCursor(0, 1);
    lcd.print("Set Temp   ");
    lcd.print(tempSet_value);
  }

  if (button_State == LOW) 
  {
    tempSet_value = tempSet_value + 100;
    delay(500);
  }

  if (button_State2 == LOW) 
  {
    tempSet_value = tempSet_value - 100;
    delay(500);
  }

  if (temp1 <= tempSet_value) 
  {
    digitalWrite(13, HIGH);
  }
  if (temp1 >= tempSet_value) 
  {
    digitalWrite (13, LOW);
  }
}

Other suggestion: try to read the temperatures at a slower pace, maybe also put them in the loop where you display the data. Some libs do not like to be called very often, but I have no experience with the one you are using.

Do you mean the screen doesn't even flicker? It should flicker once a seconds when it gets redrawn. Do you mean the screen does flicker but the displayed value of "tmp1 - 9" doesn't change even when the thermocouple temperature is changing? Does the "Set Temp" value on the LCD change after you press buttons?

I don't have your hardware and I don't see any problems in your sketch so I can't offer suggestions without more details about what you are seeing.

Hi John so that's why I was confused because the LCD does flicker like it is being written to. I am using a MAX6675 Module for the temperature and a SunFounder IIC I2C for the LCD standard momentary switches I had laying around for buttons with just a standard Uno board. Any ideas?

Best Regards

So it flickers but the values aren't ever changing?

Lets test the display of 'tempSet_value'. Try changing:
lcd.print(tempSet_value);
to
lcd.print(tempSet_value++);

That will change 'tempSet_value' each time it is displayed. If the display shows the changes then there is a problem with your buttons not working. If the display DOESN'T show the changes, something worse has gone wrong.

So when I do lcd.print(tempSet_value++); the lcd writes like normal adding 1 to tempSet_value every second but when I do lcd.print(temp1++); the lcd doesn't write as it should.

That's good. It means the LCD is working correctly. If the 'temp1' values aren't changing it means that the thermocouple isn't working. Are you sure you wired it correctly? Try the library examples to see if you can get the thermocouple to tell you the temperature.

That tells me nothing useful. What you see on the display is not what you were expecting to see. How does telling me that help me to help you?

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