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