Sketch stops

This has been driving me nuts for two days and the part of the sketch below seems to be the stumbling block. Is there anything wrong with the attached code?

HighSetPoint and LowSetPoint are declared as floats and relayStatus as int. As soon as I /* */ what is shown the sketch will carry on. Any help would be much appreciated

if(temp >= HighSetPoint && relayStatus == 1)
{
digitalWrite(relayPin, LOW);
relayStatus = 0;
}

if(temp <= LowSetPoint && relayStatus == 0)
{
digitalWrite(relayPin, HIGH);
relayStatus = 1;
}

The problem is quite possibly in the code that you did not post (HINT)

What evidence have you got the the sketch stops ?
Have you tried printing the value of the values being tested to see if they match your expectations ?

UKHeliBob:
The problem is quite possibly in the code that you did not post (HINT)

What evidence have you got the the sketch stops ?
Have you tried printing the value of the values being tested to see if they match your expectations ?

Here is the full Sketch although it is a bit messy

//Libraries

#include <Wire.h>;
#include <DHT.h>;
#include <DHT_U.h>;
#include <LiquidCrystal_I2C.h>;

#define DHTPIN 2 // Sensor connection pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize sensor
int relayPin = 6; // Signal pin for switching Relay
int offPin = 13; // Onboard LED Pin
float setPoint = 20; // Original Set Point
float LowSetPoint = setPoint - 2;
float HighSetPoint = setPoint + 2;
int relayStatus = 1;
const int upButton = 4;
const int downButton = 5;
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
pinMode(relayPin, OUTPUT); // Delclare pin as output
pinMode(offPin, OUTPUT); // Delclare pin as output
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
digitalWrite(offPin, LOW); // Drive pin low
digitalWrite(relayPin, LOW); // Drive pin low
digitalWrite(upButton, HIGH); // Pull up for setpoint buttons
digitalWrite(downButton, HIGH); // Pull up for setpoint buttons
Serial.begin(9600);

Serial.println(HighSetPoint);
Serial.println(LowSetPoint);
Serial.println(relayStatus);
Serial.println(temp);
Serial.println(setPoint);

lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print(“C/H Thermostat”);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(“Sierra Golf Mike”);

dht.begin();

delay(5000);

}

void loop()
{
temp = dht.readTemperature(); // Read and store temperature

lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("Temp: ");
lcd.print(temp);
lcd.print((char)223);
lcd.print(“C”);
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("Set Point: ");
lcd.print(setPoint);
lcd.print((char)223);
lcd.print(“C”);

//Get user input for setpoints
if(digitalRead(downButton) == LOW)
{
if(setPoint > 0)
{
setPoint – ;
}
}
if(digitalRead(upButton) == LOW)
{
if(setPoint < 35)
{
setPoint ++ ;
}
}

//Display Set point on LCD
lcd.setCursor(0,1);
lcd.print("Set Point: ");
lcd.print(setPoint);
lcd.print((char)223);
lcd.print(“C”);

if(temp >= HighSetPoint && relayStatus == 1)
{
digitalWrite(relayPin, LOW);
relayStatus = 0;
}

if(temp <= LowSetPoint && relayStatus == 0)
{
digitalWrite(relayPin, HIGH);
relayStatus = 1;
}

Serial.println(HighSetPoint);
Serial.println(LowSetPoint);
Serial.println(relayStatus);
Serial.println(temp);
Serial.println(setPoint);

delay(5000);

}

If I move the Serial.print lines to before the offending code, it prints the expected values in the Serial Monitor once then stops. if I grey out the offend section of code, it prints the values out every 5 seconds

SGM - no code tags = naughty boy

//Get user input for setpoints
if(digitalRead(downButton) == LOW)
{
if(setPoint > 0)
{
setPoint – ;
}
} ?? . :o
if(digitalRead(upButton) == LOW)
{
if(setPoint < 35)
{
setPoint ++ ;
}
} ?? . :o

cherk:
//Get user input for setpoints
if(digitalRead(downButton) == LOW)
{
if(setPoint > 0)
{
setPoint – ;
}
} ?? . :o
if(digitalRead(upButton) == LOW)
{
if(setPoint < 35)
{
setPoint ++ ;
}
} ?? . :o

What’s your question? Bad formatting? Nothing a little bit of code tagging and Ctrl+t can’t take care of.

if (digitalRead(downButton) == LOW)
{
  if (setPoint > 0)
  {
    setPoint -- ;
  }
}
if (digitalRead(upButton) == LOW)
{
  if (setPoint < 35)
  {
    setPoint ++ ;
  }
}

Apologies for the lack of code tags, now remedied!

//Libraries

#include <Wire.h>;
#include <DHT.h>;
#include <DHT_U.h>;
#include <LiquidCrystal_I2C.h>;


//Constants
#define DHTPIN 2     // Sensor connection pin
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize sensor
int relayPin = 6;   // Signal pin for switching Relay
int offPin = 13;    // Onboard LED Pin
float setPoint = 20; // Original Set Point
float LowSetPoint = setPoint - 2;
float HighSetPoint = setPoint + 2;
int relayStatus = 1;
const int upButton = 4;
const int downButton = 5;


//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
    pinMode(relayPin, OUTPUT);   // Delclare pin as output
    pinMode(offPin, OUTPUT);   // Delclare pin as output
    pinMode(upButton, INPUT);
    pinMode(downButton, INPUT);
    digitalWrite(offPin, LOW);   // Drive pin low
    digitalWrite(relayPin, LOW);   // Drive pin low
    digitalWrite(upButton, HIGH);   // Pull up for setpoint buttons
    digitalWrite(downButton, HIGH);   // Pull up for setpoint buttons
    Serial.begin(9600);

    Serial.println(HighSetPoint);
    Serial.println(LowSetPoint);
    Serial.println(relayStatus);
    Serial.println(temp);
    Serial.println(setPoint);
    
    lcd.begin();
    lcd.backlight();
    lcd.clear();
    lcd.setCursor (0,0); // go to start of 1st line
    lcd.print("C/H Thermostat");
    lcd.setCursor (0,1); // go to start of 2nd line
    lcd.print("Sierra Golf Mike");
     
    dht.begin();

    delay(5000);

}

void loop()
{
    temp = dht.readTemperature();  // Read and store temperature
    
    lcd.clear();
    lcd.setCursor (0,0); // go to start of 1st line
    lcd.print("Temp: ");
    lcd.print(temp);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor (0,1); // go to start of 2nd line
    lcd.print("Set Point: ");
    lcd.print(setPoint);
    lcd.print((char)223);
    lcd.print("C");


    //Get user input for setpoints  
    if(digitalRead(downButton) == LOW)
    {
    if(setPoint > 0)
    {
      setPoint -- ;    
    }
    }
    if(digitalRead(upButton) == LOW)
    {
    if(setPoint < 35)
    {
      setPoint ++ ;
    }
   }

   //Display Set point on LCD
   lcd.setCursor(0,1);
   lcd.print("Set Point: ");
   lcd.print(setPoint);
   lcd.print((char)223);
   lcd.print("C");


   
    if(temp >= HighSetPoint && relayStatus == 1)
      {
        digitalWrite(relayPin, LOW);
        relayStatus = 0;
      }
      
    if(temp <= LowSetPoint && relayStatus == 0)
      {
        digitalWrite(relayPin, HIGH);
        relayStatus = 1;
      }



    Serial.println(HighSetPoint);
    Serial.println(LowSetPoint);
    Serial.println(relayStatus);
    Serial.println(temp);
    Serial.println(setPoint);
      
    delay(5000);
      
      
      
}

There is obviously something I’m not getting or missed. It’s basically switching a relay on when Temp drops below 18C and switching it of when it reaches 22C when the set temp is 20C. The buttons can adjust the set temp up or down.

Thanks for your help!

Your button inputs change the value of setPoint but you never use its value in the program apart from in setup(). LowSetPoint and HighSetPoint won't magically update themselves just because you change setPoint

What does your serial output look like?

UKHeliBob:
Your button inputs change the value of setPoint but you never use its value in the program apart from in setup(). LowSetPoint and HighSetPoint won’t magically update themselves just because you change setPoint

Thanks for that, i will look at changing that but that shouldn’t stop the program from working if the set point is unaltered, or will it?
AS I said earlier, if I move the Serial Print block of code to just before the first if statement it will bring out the relevant values then stops but if I comment out the if statements the loop works, printing out the values every 5 seconds.

Do you have to comment out both if statements, or will commenting out just one or the other work?

wildbill:
What does your serial output look like?

Not sure what I have altered but the loop now works and the serial monitor prints out the values every 5 seconds but the if statements do not work!

What do you mean they don't work? "Do not work" doesn't give much information.

It seems to me that setPoint is going to be either 0 or 35. How many times does loop() go around before you can remove your finger from the button?

You need to change the code so that setPoint is changed when a button becomes pressed instead of when it is pressed and get rid of the delay() to make the sketch responsive. Use millis() for timing instead to allow the buttons to be read frequently. See
Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

I have managed to get my sketch working but adjusting HighSetPoint and LowSetPoint when setPoint is adjusted within the Loop.

Thank you to all contributors.