Button command error with code !

Hello everyone, I am writing to you because I can not finish my project of control of temperature and humidity.

The material for my project is:

Keypad 1602 Shield (to control from a menu)
I2C module (to save space behind the screen and gain arduino pin)
Relay module 2 channels (1 for cold “fridge” 2 for humidifier in the fridge)
Uno as a microcrontroller
Breadboard and its external power supply

As i am amateur, i was inspired by 2 tutorials, the first shows how to use the i2c module with the shield 1602 the second to control the temperature and humidity with Shield 1602, relay 2 channels and dht22.

With help i managed to adapt the 2 codes with editing but i have major problems to finalize my project. firstly i had no backlight and after removing the Pin 10 LCD to Pin 1 of the module I2C the problem is set, now I see the screen,

I can adjust the temperature and humidity +1 or -1 without problem, but unfortunately the arduino does not take into account the setpoint changes, it works according to the instruction of end of the scketch that with (if temp <20 relay …)

it does not want to accept the change buttons, but the buttons work very well. Relays also work without problem according to the guidelines of the sketch but not buttons. Realiter the menu is simple but as I am amateur this is a new world for me.

if you can help me finalize please I would be happy. Here is the complete code
(the delays are long for the sensor, in order to avoid the frequent starts of the compressor of the fridge.

#include <DHT.h> // inclure les librairies
#include <Wire.h>
#include <LCD.h>
#include <DFR_Key.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0.27 est l'adresse du bus I2C

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int  humidityRelay = 11; // PIN 11pour le relais
const int  heaterRelay = 12; // PIN 12

// Demarrage des valeurs, apres le demarrage on peut les changer
int temp = 19;
int hum = 49;
int readkey;
long unsigned lastmillis; 
long int hours;
long int minutes;
long int seconds;

char l2c1;
char l2c2;
char l2c3;


DFR_Key keypad(0); 

int localKey = 0;
int lcd_key = 0;
int adc_key_in = 0;

const byte btnRIGHT = 0;  
const byte btnUP = 1;     
const byte btnDOWN = 2;   
const byte btnLEFT = 3;   
const byte btnSELECT = 4; 
const byte btnNONE = 5;   

int read_LCD_buttons() 
{
  adc_key_in = analogRead(0);
  if (adc_key_in > 1000) return btnNONE;
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 150)  return btnUP;
  if (adc_key_in < 300)  return btnDOWN;
  if (adc_key_in < 460)  return btnLEFT;
  if (adc_key_in < 690)  return btnSELECT;
}

void setup() 
{
  pinMode(humidityRelay, OUTPUT);
  pinMode(heaterRelay, OUTPUT);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" ThanksKeith control");
  lcd.setCursor(0, 1);
  lcd.print("Instr on Select");
  delay(2500);
  Serial.begin(9600);
  dht.begin();
  delay(10000); 
  lcd.clear();

  // Taux d'echantillonnage (par defaut 10ms)
  // Sampling rate (default 10ms)
  keypad.setRate(100); 
  digitalWrite(humidityRelay, HIGH);
  digitalWrite(heaterRelay, HIGH);

}

void loop() 
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(t) || isnan(h)) 
  { 
  if (millis() - lastmillis > 20000)                        
    lcd.setBacklight(HIGH);
    lcd.begin(16, 2);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Capteur N/C!!");
    delay(15000);    
  } 
  else 
  {
    lcd_key = read_LCD_buttons();
    switch (lcd_key) 
    {
      case btnLEFT:
        {
          temp = temp + 1;
          break;
        }
      case btnRIGHT:
        {
          temp = temp - 1;
          break;
        }
      case btnUP:
        {
          hum = hum + 1;
          break;
        }
      case btnDOWN:
        {
          hum = hum - 1;
          break;
        }
      case btnSELECT:
        {
          lcd.begin(16, 2);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Hum Up/Down +-1%");
          lcd.setCursor(0, 1);
          lcd.print("Temp L/R +-1");
          lcd.print((char)223);
          lcd.print("C");
          delay (10000);   
          break;
        }
    } 
    lcd.setCursor(0, 0);
    lcd.print("Hum: ");
    lcd.print(h);
    lcd.print("%");
    lcd.print("(");
    lcd.print(hum);
    lcd.print("%)");
    lcd.setCursor(0, 1);
    lcd.print("Tem: ");
    lcd.print(t);
    lcd.print((char)223);
    lcd.print("(");
    lcd.print(temp);
    lcd.print((char)223);
    lcd.print(")");
    
    
    if (h > 45) digitalWrite(humidityRelay, HIGH);
    else if (h < 40) digitalWrite(humidityRelay, LOW);
    
    
    if (t < 25)
      digitalWrite(heaterRelay, HIGH);
    else if (t > 20)
      digitalWrite(heaterRelay, LOW);
  }

}

In pictures of the diagram fritzing the pins may have been changed but the base is the same.
the blue wire between lcd pin 10 and I2C pin 1 (from the right side) has to be put out if not no light.
Thank you for your reading, I apologize for the mistakes I use a translator, cordially.

Sorry Diagram !

If you want someone to fix all your problems, post in the Gigs & Collaborations section and be prepared to pay for the help.

Otherwise, with forum help, solve one problem at a time.

For example, if the buttons don't work, explain what goes wrong, post the code that demonstrates the problem, and post a hand drawn (not some one else's Fritzing) wiring diagram of your project, with all the pins and connections clearly labeled.

For posting instructions, read the "How to use this forum" post.

but unfortunately the arduino does not take into account the setpoint changes, it works according to the instruction of end of the scketch that with (if temp <20 relay …)

You have measured variables h and t.

You have button modified variables hum and temp which I think you want to use as setpoints.

if (h > 45) digitalWrite(humidityRelay, HIGH);
    else if (h < 40) digitalWrite(humidityRelay, LOW);
    
    
    if (t < 25)
      digitalWrite(heaterRelay, HIGH);
    else if (t > 20)
      digitalWrite(heaterRelay, LOW);
  }

You are controlling the relays based on fixed values not hum and temp. I’m not sure if your outputs are active low or high, but try something like below where you control off the setpoints with some hysteresis band.

if (h > hum +1) digitalWrite(humidityRelay, HIGH);
    else if (h < hum -1) digitalWrite(humidityRelay, LOW);
    
    
    if (t < temp-1)
      digitalWrite(heaterRelay, HIGH);
    else if (t > temp+1)
      digitalWrite(heaterRelay, LOW);
  }

Hello Cat. , thank you very much for your help,
the relay is activated and stops as wish, only the problem is the setpoint adjustment from the buttons as you said.

I think my relays are off high and turn on low. all I need is to use the right and left and up and down buttons to set the desired values from the buttons,

currently I can indicate instructions with the buttons but it remains only on the screen or in theory, in practice this does not change the relay. I will try your instruction.

 if (h > hum +1) digitalWrite(humidityRelay, HIGH);
    else if (h < hum -1) digitalWrite(humidityRelay, LOW);
    
    
    if (t < temp-1)
      digitalWrite(heaterRelay, HIGH);
    else if (t > temp+1)
      digitalWrite(heaterRelay, LOW);
  }

Thank you very much for your help.

Yes, yes, yes, it works !!!

Thank you very much dear ☆cattledog☆ , thanks to you i realize my dream of amateur in electronics :slight_smile: very satisfied with the result.

I make cheese at home and i had to manage the temperature and humidity of the fridge to refine the cheese, thanks to you my dream this achieves.

I wish you good health, good humor and happiness, i will not forget, Karma for ☆cattledog☆ morning-noon-night, thank you very much.

Here is the final result, it has a hysteresis of 2 ° C and 2% to moisten, I will work step by step, step by step to add to the menu a setting of hysteresis from the buttons, but for now i am already very happy Thank you 1000 times cattledog you are a genie.


here we can see the 2 relays turned off because the humidity is higher than the setpoint and the temperature lower.


we see the 2 relays at work because the relative humidity is lower than the set point and the temperature higher than the set point (beautiful)


and finally, here only the temperature relay is at work because the temperature is higher than the set point, the humidifier is stopped :slight_smile:

Great respect for ☆cattledog☆ Thank you again and again.