IF statements only work when I restart the program

Hi all,
This is my first program for the arduino, I have been into electronics for 20+ years but only now getting interested in the programming side of things, I would like to be able to help my son get into programming so figured I should try and learn myself first.

I am trying to make a beer chilling cupboard with temperature control and LCD display. Being my first project I am taking it step by step. This is what I've done so far.

  1. I got the temperature probe DS18B20 printing the temperature to the serial monitor

  2. Got the LCD to work via an I2C and show "hello world" (or something rude)

  3. Got LCD to display the temperature and worked out how to use both lines

  4. set the variables for temperature and target temperatures

  5. "Tried" to tell a relay (LED for testing) to switch on and off at 28 deg C (28 C is for testing, not for beer)

The problem I am having now is that if I reset the Arduino and the temperature is above 28C then the LED turns on but will not turn off if the temperature drops bellow 28C. If I reset the Arduino and the temperature is bellow 28C then the LED will be off but will not turn on if the temperature rises above 28C.

All along the way I have found Google and the example codes have been able to help me get past my problems but this time I am stuck. Any help would be great full and ideally if someone could as well as pointing out my error perhaps post a link to somewhere that would help me understand my mistake better so I don't make it again. My aim is to learn rather than just get it working.

Thanks
Chris

Here is my sketch.

/* This is my first Arduino project and I will be taking it step by step.
The aim of this code is to regulate the temperature of my beer in an insulated cupboard. 
I am using a peltier heat pump with a fan to be turned on and off with a relay from the Arduino.
I will be using a DS18B20 digital temperature probe installed in the cupboard to read the temperature.
I will be using a 1602 LCD with I2C adaptor to tell me the current temperature and target temperature.
I will use a rotary encoder to set the target temperature by pushing the encoders switch for 3 seconds
to enter target temperature mode and then rotating the encoder to set the temperature in 0.1C increments.
I will also have the screen turn itself off after 30 seconds of non use and back on when the encoder is turned.

*/

// include the librarys:
// 
// I2C bus support
#include <Wire.h>
// I2C LCD support
#include <LiquidCrystal_I2C.h>
//i2c pins 
//NOTE ic2 address changed to 0x3F
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //

// DS18B20 Temperature Sensor Library
#include <DallasTemperature.h>
#include <OneWire.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

//Name the variable for the temperature and setting the targrt temperature
int Temp = sensors.getTempCByIndex(0);
int TargetTemp = 28.00;

// Name relay digital pin 
int Relay = 7;

void setup() {
 /*DS18B20 Temperature code
 Here I will start the temperature probe*/

// start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.backlight();//Power on the back light
//lcd.nobacklight();// Power off the back light
}

void loop() {
  // put your main code here, to run repeatedly:

 // Code to read the temperature from the DS18B20
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 

// Print the Temperature to the LCD.
// Top Line Display
  lcd.setCursor(0,0);
  lcd.print("Temp = ");
  lcd.print(sensors.getTempCByIndex(0)); //Temperature read by DS18B20
  lcd.print("C");
// Bottom Line Display
  lcd.setCursor(0,1);
  lcd.print("Target = ");
  lcd.print(TargetTemp); //Desired temperature
  lcd.print("C");

 // When to switch the relay on or off
 if (Temp >=TargetTemp)
   digitalWrite(Relay,HIGH);
 if (Temp <TargetTemp)
    digitalWrite(Relay,LOW);

 delay(1000);

}

I'm off to bed now so will check back here after work tomorrow or at work if not to busy.

Post a link to where you got the DallasTemperature library from. Please use the chain links icon on the toolbar to make it clickable. Or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

Add

pinMode(Relay, OUPUT);

to your setup().

Because you told your program to do exactly so. You need to move the whole "int Temp ..." statement to the loop()

// When to switch the relay on or off
 if (Temp >=TargetTemp)
   digitalWrite(Relay,HIGH);
 if (Temp <TargetTemp)
    digitalWrite(Relay,LOW);

Temp does not have the current value from sensors.getTempCByIndex(0) at this point in the code. You only initialized it with the assignment from the function, and it does not update itself automatically.

You can add it here with

// Top Line Display
  lcd.setCursor(0,0);
  lcd.print("Temp = ");
  Temp = sensors.getTempCByIndex(0);
  lcd.print(Temp); //Temperature read by DS18B20
  lcd.print("C");[code]

After I see that you use an int variable but you assign it a gloat number. The only variable that can contiens float number is the float vatiable, not the int

Thanks for all the replies, many more than I was expecting. I will go through them all when I get home tonight and let you know how it goes.

Thanks again

Chris

pert:
Post a link to where you got the DallasTemperature library from. Please use the chain links icon on the toolbar to make it clickable. Or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

I got the library from here and used the latest version DallasTemperature-3.8.0.zip

Thanks

Chris

I am a happy man now. Not only is my code working but I understand why. It was simply that I needed to add the output from the temperature probe to the loop as a couple of you told me, makes total sense as it is a changing value. I also now know(ish) what a Float Number is and what PinMode does.

Loving the Arduino and the great people on this forum.

Thanks

Chris

Here is the code now (still not finished but working as it should)

/* This is my first Arduino project and I will be taking it step by step.
The aim of this code is to regulate the temperature of my beer in an insulated cupboard. 
I am using a peltier heat pump with a fan to be turned on and off with a relay from the Arduino.
I will be using a DS18B20 digital temperature probe installed in the cupboard to read the temperature.
I will be using a 1602 LCD with I2C adaptor to tell me the current temperature and target temperature.
I will use a rotary encoder to set the target temperature by pushing the encoders switch for 3 seconds
to enter target temperature mode and then rotating the encoder to set the temperature in 0.1C increments.
I will also have the screen turn itself off after 30 seconds of non use and back on when the encoder is turned.

*/

// include the librarys:
// 
// I2C bus support
#include <Wire.h>
// I2C LCD support
#include <LiquidCrystal_I2C.h>
//i2c pins 
//NOTE ic2 address changed to 0x3F
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //

// DS18B20 Temperature Sensor Library
#include <DallasTemperature.h>
#include <OneWire.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

//Name the variable for the temperature and setting the targrt temperature
int Temp = sensors.getTempCByIndex(0);
int TargetTemp = 28;

// Name relay digital pin 
int Relay = 7;

void setup() {
 /*DS18B20 Temperature code
 Here I will start the temperature probe*/

// start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
  
// set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.backlight();//Power on the back light
//lcd.nobacklight();// Power off the back light

// Tell pin 7 aka Relay that it is an output
   pinMode(Relay, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

 // Code to read the temperature from the DS18B20
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 

// Print the Temperature to the LCD.
// Top Line Display
  lcd.setCursor(0,0);
  lcd.print("Temp = ");
  Temp = sensors.getTempCByIndex(0);
  lcd.print(Temp); //Temperature read by DS18B20
  lcd.print("C");
// Bottom Line Display
  lcd.setCursor(0,1);
  lcd.print("Target = ");
  lcd.print(TargetTemp); //Desired temperature
  lcd.print("C");

 // When to switch the relay on or off
 if (Temp >=TargetTemp)
   digitalWrite(Relay,HIGH);
 if (Temp <TargetTemp)
    digitalWrite(Relay,LOW);
 delay(1000);

}