Adding buttons to an existing sketch

I have recently built a temperature controller unit for a fermentation bucket using a DS18B20 temperature sensor, a heater pad and an Arduino Uno. I have based this around a project I found (https://www.instructables.com/Multiple-Fermenter-Temperature-Control-With-Arduin/), however, unlike that project, I am just using one heater and have not included a cooler.
I have butchered the existing code and everything works fine but I would like to add 2 buttons, one to increment the Set Temperature and the other to decrement it. This will save me having to re-upload a new sketch every time I want to have a different threshold temperature. I have read various posts, on a number of forums, regarding adding buttons to an existing project and have tried my best to resolve this, but I'm not getting very far.
The code, which works fine is as follows: -

/* DS18B20 Temperature Sensors on 1 wire for controlling heating fermenter
   
   -----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
// Digital pin 2 for the DS18B20 data line.
#define ONE_WIRE_BUS_PIN 2

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

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

/*-----( Declare Variables )-----*/

//Get Device Address for Probe01;
DeviceAddress Probe01;
//Relay digital write pin on Arduino
int Relay1 = 5;

float Probe01Temp = 0;

//Limit and floor are Celcius
//Adjust these temperature limits as needed
int Temp01SetPoint = 20;
int Temp01SetDiff = 1;

static bool Temp01WarmMaxed;
static bool Relay01Status;

//Adjust the address of the I2C as needed
//See https://github.com/todbot/arduino-i2c-scanner
//LiquidCrystal_I2C lcd(0x27, 16, 2); LiquidCrystal_I2C lcd(0x27, 20, 4);
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()   /****** SETUP: RUNS ONCE ******/
{
    
  // start lcd to show results
  lcd.begin(20,4);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Starting Up");
  
  // Initialize the Temperature measurement library
  sensors.begin();

  // Comment out the following line if pre-assigned above
  sensors.getAddress(Probe01, 0);
    
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 9);
 
  //set the relays
  pinMode(Relay1, OUTPUT);
    //turn off the relays to start
  digitalWrite(Relay1, HIGH);

  Temp01WarmMaxed = false;
  
  Relay01Status = false;
  
  lcd.setCursor(0, 1);
  lcd.print("No. Sensors:");
  lcd.setCursor(12, 1);
  lcd.print(sensors.getDeviceCount());
  lcd.setCursor(0, 2);
  lcd.print("Getting temperature");

  delay(5000);
  lcd.clear();

 //Set up labels on LCD
 lcd.setCursor(0, 0);
 lcd.print("Set temp:");
 lcd.setCursor(12, 0);
 lcd.print(char(223));
 lcd.setCursor(13,0);
 lcd.print("+/-");
 lcd.setCursor(17, 0);
 lcd.print(char(223));
 lcd.setCursor(18,0);
 lcd.print("C");
 lcd.setCursor(0, 1);
 lcd.print("Barrel temperature:");
 lcd.setCursor(4, 2);
 lcd.print(char(223));
 lcd.setCursor(5, 2);
 lcd.print("C");
 lcd.setCursor(0, 3);
 lcd.print("Heater:");
 lcd.setCursor(10,0);
 lcd.print(Temp01SetPoint);
 lcd.setCursor(16,0);
 lcd.print(Temp01SetDiff);
  
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(5000);
   
  // Command Probe01 to read temperature  
  sensors.requestTemperatures();
  //Barrel temperature
  Probe01Temp = sensors.getTempC(Probe01);
   
  //Change calls to triggerCoolingRelay and triggerWarmingRelay
  //as needed such as both Relay01 and Relay02 both call triggerWarmingRelay()
  
  Relay01Status=triggerWarmingRelay(Probe01Temp, Relay1, Temp01SetPoint, Temp01SetDiff, Temp01WarmMaxed);
  Temp01WarmMaxed=Relay01Status;
  
  //Print to LCD
  lcd.setCursor(0,2);
  lcd.print(Probe01Temp, 1);
  
  lcd.setCursor(7,3);
  if (Relay01Status)
    {
      lcd.print("On ");
    }
    else
    {
      lcd.print("Off");
    }
     
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

bool triggerWarmingRelay(float tempC, int Relay, int TempSetLimit, int TempSetDiff, bool TempWarmMaxed)
{
  bool result;
  
    if (TempWarmMaxed)
      {
      if (tempC > (TempSetLimit+TempSetDiff))
        {
        //TempWarmMaxed=false;
        result=false;
        }
      }
    else
      {
      if (tempC < TempSetLimit)
         {
         //TempWarmMaxed=true;
         digitalWrite(Relay, LOW);
         result=true;
         } 
        else 
          {
          digitalWrite(Relay, HIGH);
          result=false;
          }
       }
   return result;
   
 }// End triggerWarmingRelay

//*********( THE END )***********

What I think needs to be done is to increase and decrease the Temp01SetPoint (line 36) by 1 degree each time the relevant button is pressed. This value will then be used to turn the heater on and off once the Temp01SetPoint, + or - the Temp01SetDiff (line 37), has been breached.
Typing this out, it all sounds so simple, however, I am struggling and would appreciate any help.
TIA

Using a Button library would be the easiest route.

Here's the basic usage example from the one referenced above:

#include <Button.h>

Button button1(2); // Connect your button between pin 2 and GND
Button button2(3); // Connect your button between pin 3 and GND
Button button3(4); // Connect your button between pin 4 and GND

void setup() {
	button1.begin();
	button2.begin();
	button3.begin();
	
	while (!Serial) { }; // for Leos
	Serial.begin(9600);
}

void loop() {
	if (button1.pressed())
		Serial.println("Button 1 pressed");
	
	if (button2.released())
		Serial.println("Button 2 released");
	
	if (button3.toggled()) {
		if (button3.read() == Button::PRESSED)
			Serial.println("Button 3 has been pressed");
		else
			Serial.println("Button 3 has been released");
	}
}```

I hope I am answering your question. If not, please provide more details about what you need help with.

One issue I see is the use of a 5-second delay. You will need to poll the buttons
and having this delay will mean you will only see a button push every 5 seconds.

One way around this is to do something like the following:

unsigned long current_millis;
unsigned long previous_millis;
unsigned long temperature_interval = 5000;
unsigned long button_interval = 20;



void temperature_scan() {
  current_millis = millis();
  if (current_millis - previous_millis > temperature_interval) {
    previous_millis = current_millis;

    // do the reset of handling the temperature processing here
  }
}

void button_scan() {
  current_millis = millis();
  if (current_millis - previous_millis > button_interval) {
    previous_millis = current_millis;
    // do the reset of button handling
  }
}

void setup() {
  // normal setup with the addition of adding button pins
}

void loop() {
  button_scan();
  temperature_scan();
}

This way, you process each "task" within the appropriate time interval with minimal blocking.

Yes. And @van_der_decken has a good idea, just use a button library and do the increase or decrease if the corresponding button gets pressed,

But. You have a 5000 millisecond delay in the loop, which means such changes would only go at one step every five seconds... excruciatingly slow.

Why is there a long delay every loop? There shouldn't need to be; anything it accomplishes could be done differently, then you buttons could get looked at fast enough to be responsive not annoying.

a7

Thanks for your replies folks. As you will see from the below, I have used the Button.h library and removed the 5000m/s delay and everything works fine.

`

/* DS18B20 Temperature Sensors on 1 wire for controlling heating fermenter
   
   -----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
#include <Button.h>

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
// Digital pin 2 for the DS18B20 data line.
#define ONE_WIRE_BUS_PIN 2
Button button1(3); // Connect your button between pin 2 and GND
Button button2(4); // Connect your button between pin 3 and GND

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

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

/*-----( Declare Variables )-----*/

//Get Device Address for Probe01;
DeviceAddress Probe01;
//Relay digital write pin on Arduino
int Relay1 = 5;

float Probe01Temp = 0;

//Limit and floor are Celcius
//Adjust these temperature limits as needed
int Temp01SetPoint = 20;
int Temp01SetDiff = 1;

static bool Temp01WarmMaxed;
static bool Relay01Status;

//Adjust the address of the I2C as needed
//See https://github.com/todbot/arduino-i2c-scanner
//LiquidCrystal_I2C lcd(0x27, 16, 2); LiquidCrystal_I2C lcd(0x27, 20, 4);
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()   /****** SETUP: RUNS ONCE ******/
{
    
  // start lcd to show results
  lcd.begin(20,4);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Starting Up");
  
  // Initialize the Temperature measurement library
  sensors.begin();
  button1.begin();
	button2.begin();
  // Comment out the following line if pre-assigned above
  sensors.getAddress(Probe01, 0);
    
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 9);
 
  //set the relays
  pinMode(Relay1, OUTPUT);
    //turn off the relays to start
  digitalWrite(Relay1, HIGH);

  Temp01WarmMaxed = false;
  
  Relay01Status = false;
  
  lcd.setCursor(0, 1);
  lcd.print("No. Sensors:");
  lcd.setCursor(12, 1);
  lcd.print(sensors.getDeviceCount());
  lcd.setCursor(0, 2);
  lcd.print("Getting temperature");

  delay(5000);
  lcd.clear();

 //Set up labels on LCD
 lcd.setCursor(0, 0);
 lcd.print("Set temp:");
 lcd.setCursor(11, 0);
 lcd.print(char(223));
 lcd.setCursor(13,0);
 lcd.print("+/-");
 lcd.setCursor(18, 0);
 lcd.print(char(223));
 lcd.setCursor(19,0);
 lcd.print("C");
 lcd.setCursor(0, 1);
 lcd.print("Barrel temperature:");
 lcd.setCursor(4, 2);
 lcd.print(char(223));
 lcd.setCursor(5, 2);
 lcd.print("C");
 lcd.setCursor(0, 3);
 lcd.print("Heater:");
 lcd.setCursor(17,0);
 lcd.print(Temp01SetDiff);
  
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //delay(5000);
   
  // Command Probe01 to read temperature  
  sensors.requestTemperatures();
  //Barrel temperature
  Probe01Temp = sensors.getTempC(Probe01);
  if (button1.pressed())
   Temp01SetPoint++;
   if (button2.pressed())
   Temp01SetPoint--;
  //Change calls to triggerCoolingRelay and triggerWarmingRelay
  //as needed such as both Relay01 and Relay02 both call triggerWarmingRelay()
  
  Relay01Status=triggerWarmingRelay(Probe01Temp, Relay1, Temp01SetPoint, Temp01SetDiff, Temp01WarmMaxed);
  Temp01WarmMaxed=Relay01Status;
  
  //Print to LCD
  lcd.setCursor(0,2);
  lcd.print(Probe01Temp, 1);
  
  lcd.setCursor(9,0);
 lcd.print(Temp01SetPoint);


  lcd.setCursor(7,3);
  if (Relay01Status)
    {
      lcd.print("On ");
    }
    else
    {
      lcd.print("Off");
    }
     
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

bool triggerWarmingRelay(float tempC, int Relay, int TempSetLimit, int TempSetDiff, bool TempWarmMaxed)
{
  bool result;
  
    if (TempWarmMaxed)
      {
      if (tempC > (TempSetLimit+TempSetDiff))
        {
        //TempWarmMaxed=false;
        result=false;
        }
      }
    else
      {
      if (tempC < TempSetLimit)
         {
         //TempWarmMaxed=true;
         digitalWrite(Relay, LOW);
         result=true;
         } 
        else 
          {
          digitalWrite(Relay, HIGH);
          result=false;
          }
       }
   return result;
   
 }// End triggerWarmingRelay

//*********( THE END )***********

`
It took me a while to find out that I had to move the lcd.print(Temp01SetPoint); into the loop, but once I did that the buttons work as expected. One increases the Set Temperature by 1° and the other decrements by the same amount.
I am sure that the code could be tidied up significantly, but I'm just happy that it all works as it should.
Thanks again for your assistance.