Switch cases with different if statements

I am creating an huimdity/temp control with different conditions (a greenhouse)
I already created the code for every condition that I want but there is 1 case that interferes with 2 conditions.

#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <DHT_U.h>
const int numRows = 4;
const int numCols = 3;
const int debounceTime = 20;

const char keymap[numRows][numCols] = {
 {
   '1', '2', '3'            }
 ,
 {
   '4', '5', '6'           }
 ,
 {
   '7', '8', '9'           }
 ,
 {
   '*', '0', '#'            }
};


const int rowPins[numRows] = {
 9, 8, 7, 6 }; // Rows 0 through 3
const int colPins[numCols] = {
 5, 4, 3 };    // Columns 0 through 2
LiquidCrystal_I2C lcd(0x3F, 16, 2); 
int sensor = 2;
int sp_hum;
int sp_temp;
float sp_temp2;
float hum;
float temp;

//relay-outputs 
const int R1 = 10; 
const int R2 = 11; 
const int R3 = 12; 
const int R4 = 13; 

DHT dht(sensor, DHT22);
float setpoint;
void setup()
{
 Serial.begin(9600);
 lcd.init(); 
 lcd.backlight();
  lcd.begin(16, 2);
  lcd.print("Greenhouse v1.0");
  lcd.setCursor(0,1);
  delay(2000);
  lcd.clear();

 for (int row = 0; row < numRows; row++)
 {
   pinMode(rowPins[row],INPUT); 
   digitalWrite(rowPins[row],HIGH); 
 }
 for (int column = 0; column < numCols; column++)
 {
   pinMode(colPins[column],OUTPUT); 
   // for writing
   digitalWrite(colPins[column],HIGH); 
 }
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);

 dht.begin(); 
 sp_hum=0;
 sp_temp=0;
}
//================
void loop(){
 char key = getKey();

 if(key != 0) {
   Serial.print("key pressed = ");
   Serial.println(key);
 }

 switch (key)
 {
 case '1' :
   
   Serial.println("Setpoint#1");
   sp_hum = 82;
   sp_temp = 90;
   break;

 case '2':
   Serial.println("Setpoint#2");
   sp_hum = 73;
   sp_temp = 91;
   break;

 case '3':
   Serial.println("Setpoint#3");
   sp_hum = 87;
   sp_temp = 70;
   break;

 case '0':
   Serial.println("Turn Off");
   sp_hum = 0;
   sp_temp = 0;
   break;
 }// close switch

float converted = 0.00;
    
    float hum = dht.readHumidity();
    float temp = dht.readTemperature();

    //Fahrenheit
    //T(°F) = T(°C) × 9/5 + 32
    converted = ( temp * 1.8 ) + 32;
    
if (hum < sp_hum) {
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    } else  {
      digitalWrite(R1, HIGH);
      digitalWrite(R2, HIGH);
      }

if (converted < sp_temp) {
    digitalWrite(R3, LOW);
    } else  {
      digitalWrite(R3, HIGH);
      }

if (converted > sp_temp)  {
    digitalWrite(R4, LOW);
    } else  {
      digitalWrite(R4, HIGH);
      }      

//Data on LCD
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SP=");
  lcd.print(sp_hum);
  lcd.print("%HR/");
  lcd.print("T=");
  lcd.print(sp_temp);
  lcd.print("F");
  lcd.setCursor(0,1);
  lcd.print("HR=");
  lcd.print(hum);
  lcd.print(" T=");
  lcd.print(converted);

}// close void

//==============================
char getKey()
{
 char key = 0; 

 for(int column = 0; column < numCols; column++)
 {
   digitalWrite(colPins[column],LOW);
   for(int row = 0; row < numRows; row++)
   {
     if(digitalRead(rowPins[row]) == LOW)
     {
       delay(debounceTime); 
       while(digitalRead(rowPins[row]) == LOW)
         ;    
       key = keymap[row][column];  
     }
   }
   digitalWrite(colPins[column],HIGH);  
 }
 return key;
}

I want the case 1 for the 1st if statement, the case 2 for the second statement and the case 3 for the last one. But the case 2 interferes with the if's #2 and #3.

Thanks

float converted = 0.00; Where does the switch end?

   case '0':
      Serial.println("Turn Off");
      sp_hum = 0;
      sp_temp = 0;
      break;
      float converted = 0.00;

Why have you got a break before the end of this case ?

Is there any reason why you are not using the KeyPad library ?

TheMemberFormerlyKnownAsAWOL:

float converted = 0.00;

Where does the switch end?

Sorry, I accidentally deleted. I already updated the code

 }// close switch

UKHeliBob:

   case '0':

Serial.println("Turn Off");
     sp_hum = 0;
     sp_temp = 0;
     break;
     float converted = 0.00;



Why have you got a break before the end of this case ?

Is there any reason why you are not using the KeyPad library ?

Since this code was something that I already used years ago I decided to use it again...
I never tried with the library but any suggestion is welcome

This may be of interest Keypad data entry

UKHeliBob:
This may be of interest Keypad data entry

Thank you, I will try with this asap.

Other thing is...
I tried something like this:

if (converted = 91) {
    digitalWrite(R3, LOW);
    } else  {
      digitalWrite(R3, HIGH);
      }

to specify the case #2, but this only assign that value to the variable converted.

Any idea to solve it? meanwhile I will try with the keypad library.

if (converted = 91)

= for assignment
== for comparison

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.