Hysteresis / "if +/- 1"

First things first. here is my code:

#include <Keypad.h>
char hexaKeys[4][4] = {
  'A', 'B', 'C', 'D',
  'E', 'F', 'G', 'H',
  'I', 'J', 'K', 'L',
  'M', 'N', 'O', 'P'
};
byte rows = 4; 
byte cols = 4; 
byte rowPins[] = {
  2,3,4,5}; 
byte colPins[] = {
  6,7,8,9}; 


Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, rows, cols);
float tempC; 		
int tempPin = 0;
char keyPress [2];
int pressIndex = 0;
int key1 = 0;
int key2 = 0;
int keytot = 0;

void setup(){
  Serial.begin(9600);
  pinMode(32, OUTPUT); 
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  digitalWrite(39, 1);
  pinMode(42, OUTPUT); 
  pinMode(43, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(45, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(47, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(49, OUTPUT);
  digitalWrite(49, 1);
  pinMode(50, OUTPUT);
}

void loop(){
  tempC = analogRead(tempPin); 	
  tempC = (5.0 * tempC * 100.0)/1024.0;  
  Serial.print(tempC); 
  Serial.println(" Grader Celcius"); 	    
  char key = keypad.getKey();
  keytot = (key1 * 10 + key2);
  Serial.println (keytot);
  if (keytot > tempC) {
    digitalWrite(50, 1);
  }
  if (keytot < tempC) {
    digitalWrite(50, 0);
  }

  if (key != NO_KEY) {
    key -= 'A'-1;
    keyPress [pressIndex++] = key;
    if (pressIndex == 2) {    
      Serial.println(keyPress[0], DEC);
      Serial.println(keyPress[1], DEC);
      pressIndex = 0;  
    }
    if (keyPress[0] == 1) {
      key1 = 1;
    }
    if (keyPress[0] == 2) {
      key1 = 2;
    }
    if (keyPress[0] == 3) {
      key1 = 3;
    }
    if (keyPress[0] == 5) {
      key1 = 4;
    }
    if (keyPress[0] == 6) {
      key1 = 5;
    }
    if (keyPress[0] == 7) {
      key1 = 6;
    }
    if (keyPress[0] == 9) {
      key1 = 7;
    }
    if (keyPress[0] == 10) {
      key1 = 8;
    }
    if (keyPress[0] == 11) {
      key1 = 9;
    }
    if (keyPress[0] == 14) {
      key1 = 0;
    }
    if (keyPress[1] == 1) {
      key2 = 1;
    }
    if (keyPress[1] == 2) {
      key2 = 2;
    }
    if (keyPress[1] == 3) {
      key2 = 3;
    }
    if (keyPress[1] == 5) {
      key2 = 4;
    }
    if (keyPress[1] == 6) {
      key2 = 5;
    }
    if (keyPress[1] == 7) {
      key2 = 6;
    }
    if (keyPress[1] == 9) {
      key2 = 7;
    }
    if (keyPress[1] == 10) {
      key2 = 8;
    }
    if (keyPress[1] == 11) {
      key2 = 9;
    }
    if (keyPress[1] == 14) {
      key2 = 0;
    }

    if (keyPress[0] == 1){ // 1
      digitalWrite(42, 1);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 1);
      digitalWrite(46, 1);
      digitalWrite(47, 1);
      digitalWrite(48, 1);
    }
    if (keyPress[0] == 2){ // 2
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 1);
      digitalWrite(45, 0);
      digitalWrite(46, 0);
      digitalWrite(47, 1);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 3) { // 3
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
      digitalWrite(46, 1);
      digitalWrite(47, 1);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 5) { //4
      digitalWrite(42, 1);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 1);
      digitalWrite(46, 1);
      digitalWrite(47, 0);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 6) { //5
      digitalWrite(42, 0);
      digitalWrite(43, 1);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
      digitalWrite(46, 1);
      digitalWrite(47, 0);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 7) { //6
      digitalWrite(42, 0);
      digitalWrite(43, 1);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
      digitalWrite(46, 0);
      digitalWrite(47, 0);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 9) { // 7
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 1);
      digitalWrite(46, 1);
      digitalWrite(47, 1);
      digitalWrite(48, 1);
    }
    if (keyPress[0] == 10) { // 8
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
      digitalWrite(46, 0);
      digitalWrite(47, 0);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 11) { // 9
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 1);
      digitalWrite(46, 1);
      digitalWrite(47, 0);
      digitalWrite(48, 0);
    }
    if (keyPress[0] == 14) { // 0
      digitalWrite(42, 0);
      digitalWrite(43, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
      digitalWrite(46, 0);
      digitalWrite(47, 0);
      digitalWrite(48, 1);
    }
    if (keyPress[1] == 1){ // 1
      digitalWrite(32, 1);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 1);
      digitalWrite(36, 1);
      digitalWrite(37, 1);
      digitalWrite(38, 1);
    }
    if (keyPress[1] == 2){ // 2
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 1);
      digitalWrite(35, 0);
      digitalWrite(36, 0);
      digitalWrite(37, 1);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 3) { // 3
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 1);
      digitalWrite(37, 1);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 5) { //4
      digitalWrite(32, 1);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 1);
      digitalWrite(36, 1);
      digitalWrite(37, 0);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 6) { //5
      digitalWrite(32, 0);
      digitalWrite(33, 1);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 1);
      digitalWrite(37, 0);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 7) { //6
      digitalWrite(32, 0);
      digitalWrite(33, 1);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 0);
      digitalWrite(37, 0);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 9) { // 7
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 1);
      digitalWrite(36, 1);
      digitalWrite(37, 1);
      digitalWrite(38, 1);
    }
    if (keyPress[1] == 10) { // 8
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 0);
      digitalWrite(37, 0);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 11) { // 9
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 1);
      digitalWrite(36, 1);
      digitalWrite(37, 0);
      digitalWrite(38, 0);
    }
    if (keyPress[1] == 14) { // 0
      digitalWrite(32, 0);
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 0);
      digitalWrite(37, 0);
      digitalWrite(38, 1);
    }

  }
}

Now for the question

the problem lies here:

if (keytot > tempC) {
    digitalWrite(50, 1);
  }
  if (keytot < tempC) {
    digitalWrite(50, 0);
  }

When my temperature reaches the preset it goes into hysteresis, so i want it to trigger on at 20 and off on 22 when i set it to 21 to avoid it. or something liek that. so i was wondering if i could make an IF keytot is more than 1 above tempC then turn off and if keytot is more than 1 beneath tempC then turn on. so kinda like if (keytot > tempC +1) ish. to counter the hysteresis. does anyone know any solution to this?.. or do anyone even know what i’m talking baout :slight_smile: ?

please use meaningfull names for pin’s and variables in your code, that helps reading it

keytot is meant as the switching temperature, lets call it tempSW

#define HEATINGPIN      50    // I assume it is some sort of heating
#define HEATOFFSET     1       // to prevent hysteresis - value to be defined

// assume tempSW = 21;

if ( tempC < (tempSW - HEATOFFSET) ) digitalWrite(HEATINGPIN , HIGH);   // heating on  
else if ( tempC > (tempSW + HEATOFFSET) ) digitalWrite(HEATINGPIN , LOW);   // heating of

another, maybe better, approach is to define two offsets, gives more freedom

#define HEATINGPIN   50     // I assume it is some sort of heating
#define OFFSETHIGH  2       // to prevent hysteresis - value to be defined
#define OFFSETLOW   0       // to prevent hysteresis - value to be defined

// assume tempSW = 21;

if ( tempC < (tempSW - OFFSETHIGH  ) ) digitalWrite(HEATINGPIN , HIGH);   // heating on  
else if ( tempC > (tempSW + OFFSETLOW   ) ) digitalWrite(HEATINGPIN , LOW);   // heating of

This makes it switch off at the entered value
and switch on if tempC has decreased 2 degrees

Please note the use of else to prevent the second test in case the first succeeded