Este es un código algo mas simple pero aún hay mucho por mejorar/corregir para que sea mas eficiente, solo te muestro una etapa intermedia para que veas como puedes simplificar cosas.
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
#include <DHT.h> // Definimos el pin digital donde se conecta el
#define DHTPIN 3// Dependiendo del tipo de sensor
#define DHTTYPE DHT11 // Inicializamos el sensor DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <Keypad.h>
const byte filas = 4; //Numero de filas del teclado
const byte columnas = 4; //Numero de columnas del teclado
//Defino una matriz 4x4 con la posicion de las filas y columnas
char matriz[filas][columnas] =
{
{ '1', '2', '3', 'A'},
{ '4', '5', '6', 'B'},
{ '7', '8', '9', 'C'},
{ '*', '0', '#', 'D'},
};
byte pinesFilas[filas] = {6, 7, 8, 9}; //Pines donde van conectadas las filas del teclado
byte pinesColumnas[columnas] = {10, 11, 12, 13}; //Pines donde van conectadas las columnas del teclado
Keypad teclado = Keypad( makeKeymap(matriz), pinesFilas, pinesColumnas, filas, columnas);
float tempCafe = 38;
float tempCafeMini = 34;
float tempCafeMax = 42;
float humCafe = 20;
float humCafeMax = 50;
float humMaiz = 24;
float tempMaiz = 49;
float humMaizMax = 48;
float tempMaizMini = 45;
float tempMaizMax = 53;
float tempCacao = 45;
float humCacao = 20;
float humCacaoMax = 50;
float tempCacaoMax = 49;
float tempCacaoMini= 41;
float tempEstandr = 45;
float humEstandrMax= 50;
float tempEstandrMax= 49;
float tempEstandrMini= 38;
float humEstandr = 20;
float h, t;
int motor=5;
int Resistencia=4;
void setup() {
pinMode(motor,OUTPUT);
pinMode(Resistencia,OUTPUT);
dht.begin();
Serial.begin(9600);
lcd.begin(16, 2); // medidas de LCD 16 x 2
lcd.setCursor(1, 0);
lcd.print("Secador Granos");
delay(3000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Selec Opc:");
lcd.setCursor(0, 1);
lcd.print("A) ");
lcd.setCursor(4, 1);
lcd.print("B) ");
lcd.setCursor(8, 1);
lcd.print("C) ");
lcd.setCursor(12, 1);
lcd.print("D) ");
}
void loop() {
menuinicio();
}
void menuinicio() {
lcd.setCursor(1, 0);
lcd.print("Selec Opc:");
lcd.setCursor(0, 1);
lcd.print("A) ");
lcd.setCursor(4, 1);
lcd.print("B) ");
lcd.setCursor(8, 1);
lcd.print("C) ");
lcd.setCursor(12, 1);
lcd.print("D) ");
selecOpc();
}
void selecOpc(){
char tecla_presionada = teclado.getKey(); //Almaceno en una variable la tecla presionada
if (tecla_presionada) {
switch(tecla_presionada) {
case 'A':
lcdOpcion("Opcion A...", "Cafe.");
Cafe();
break;
case 'B':
lcdOpcion("Opcion B...", "Maiz..");
Maiz();
break;
case 'C':
lcdOpcion("Opcion C...", "Cacao.");
Cacao();
break;
case 'D':
lcdOpcion("Opcion D...", "Estandar.");
Estandar();
break;
}
}
}
void Cafe() {
h = dht.readHumidity();
t = dht.readTemperature();
if (h > humCafe){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cafe:");
lcd.setCursor(5,0);
lcd.print("Hum:");
lcd.print(h);
lcd.print("%");
lcd.setCursor(5,1);
lcd.print("Tem:");
lcd.print(t);
lcd.print("C");
controlMotor(1000, HIGH, HIGH);
Cafe();
}
if (t > tempCafeMax){
humtemp();
controlMotor(5000, HIGH, LOW);
Cafe();
}
if (t < tempCafeMini) {
humtemp();
controlMotor(5000, HIGH, LOW);
Cafe();
}
if (t == tempCafe){
humtemp();
controlMotor(5000, HIGH, HIGH);
Cafe();
}
if (h == humCafe){
lcd.clear();
lcd.print("Proc. Terminado");
delay(5000);
digitalWrite(motor,LOW);
digitalWrite(Resistencia,LOW);
menuinicio();
}
}
void Maiz()
{
h = dht.readHumidity();
t = dht.readTemperature();
if (h > humMaizMax){
humtemp();
controlMotor(5000, HIGH, HIGH);
Maiz();
}
if (t > tempMaizMax){
humtemp();
controlMotor(5000, HIGH, LOW);
Maiz();
}
if (t < tempMaizMini) {
humtemp();
controlMotor(5000, HIGH, LOW);
Maiz();
}
if (t == tempMaiz){
humtemp();
controlMotor(5000, HIGH, HIGH);
Maiz();
}
if (h == humMaiz){
lcd.clear();
lcd.print("Proc. Terminado");
delay(5000);
digitalWrite(motor,LOW);
digitalWrite(Resistencia,LOW);
menuinicio();
}
}
void Cacao(){
h = dht.readHumidity();
t = dht.readTemperature();
if (h > humCacaoMax){
humtemp();
controlMotor(5000, HIGH, HIGH);
Cacao();
}
if (t > tempCacaoMax){
humtemp();
controlMotor(5000, HIGH, LOW);
Cacao();
}
if (t < tempCacaoMini) {
humtemp();
controlMotor(5000, HIGH, LOW);
Cacao();
}
if (t == tempCacao){
humtemp();
controlMotor(5000, HIGH, HIGH);
Cacao();
}
if (h == humCacao){
lcd.clear();
lcd.print("Proc. Terminado");
delay(5000);
digitalWrite(motor,LOW);
digitalWrite(Resistencia,LOW);
menuinicio();
}
}
void Estandar(){
h = dht.readHumidity();
t = dht.readTemperature();
if (h > humEstandrMax){
humtemp();
controlMotor(5000, HIGH, HIGH);
Estandar();
}
if (t > tempEstandrMax){
humtemp();
controlMotor(5000, HIGH, LOW);
Estandar();
}
if (t < tempEstandrMini) {
humtemp();
controlMotor(5000, HIGH, LOW);
Estandar();
}
if (t == tempEstandr){
humtemp();
controlMotor(5000, HIGH, HIGH);
Estandar();
}
if (h == humEstandr){
lcd.clear();
lcd.print("Proc. Terminado");
delay(5000);
digitalWrite(motor,LOW);
digitalWrite(Resistencia,LOW);
menuinicio();
}
}
void lcdOpcion(String tmp1, String tmp2) {
lcd.clear();
lcd.print(tmp1);
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(tmp2);
delay(1000);
lcd.setCursor(0,0);
lcd.print(tmp2);
delay(1000);
lcd.setCursor(0,0);
lcd.print(tmp2);
}
void humtemp() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humedad:");
lcd.print(h);
lcd.setCursor(0,1);
lcd.print("Temperatura:");
lcd.print(t);
}
void controlMotor(int _delay, bool tmp1, bool tmp2) {
digitalWrite(motor,tmp1);
delay(_delay);
digitalWrite(Resistencia,tmp2);
delay(_delay);
}