Need Help Programming Dead Zone for Thermostat

Hi, I’m just getting my feet wet in the world of Arduino and programming. I’m going to be installing an Arduino Uno into my Bunn coffee maker to regulate the water temperature (the temp switch in the coffee maker broke and there’s no replacement available). I’m using a 16x2 LCD as well to display current water temperature and temperature setpoint (which I’ll set using up and down buttons). The problem I’m having is programming in a dead zone for the temperature, so it won’t try to turn the heating element on and off a million times a minute trying to maintain a precise temperature.

I’m shooting for a temperature swing of 2 degrees. My first thought was this:

//thermostat code
if (settemp - 2 > temp) digitalWrite (heatout, HIGH);

if (settemp <= temp) digitalWrite (heatout, LOW);

The problem with that code is that it will end up just hovering somewhere around “settemp-2.” What I’d like it to do is this:

Let’s say the settemp is 200 degrees F. I’d like it to do nothing until the water temp (“temp”) falls to about 198 degrees F. Then I would like it to set the heatout pin to HIGH until it gets all the way up to 200, or settemp, then set heatout pin back to LOW.

Does anyone have any suggestions on how I can code this? ANY help would be amazing! Thanks in advance!

If the temp is less than 198 turn it on. If the temp is greater than 200 turn it off. In between those two nothing will happen and it will keep its old state.

if(temp < 198) {
   //turn heater on
if(temp > 200) {
  // turn heater off

As it heats, it won’t turn off until greater than 200. Then as it cools it won’t turn back on until less than 198. Use your setpoint and setpoint - 2 for the 200 and 198. Just thought the example was easier to follow with real numbers.

Thanks for that! I've been staring at this code for about 3 hours getting everything else programmed in (the lcd display, button inputs, relay outputs, display format, pot warmer inputs/outputs) and I think I just had a dummy moment. I figured it out right as you replied, and you verified it. I think I need to put it away for the evening. Thanks again for your quick reply!