Hola, en mi proyecto de control de temperatura tengo una variable llamada "tempDeseadaH1" y quiero modificarla con un encoder rotativo con pulsador, pero no he encontrado la forma de hacer que funcione y que se muestre en pantalla.
Utilizo distintas librerías como "LiquidMenu", "LiquidCrystal_I2C", "OneWire" y "DallasTemperature".
#include <LiquidMenu.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Definimos sensores
const int pinDatosDQ = 3;
OneWire oneWireObjeto(pinDatosDQ);
DallasTemperature sensorDS18B20(&oneWireObjeto);
//Definimos relees
const int pinReleCalefon = 8;
const int pinReleElectH1 = 9;
const int pinReleElectH2 = 10;
const int pinReleElectH3 = 11;
const int pinReleElectrovalvula = 12;
const int pinReleBombaAgua = 13;
//Nuestra forma de elegir relee
int relee_seleccionado = 0;
//Defino Encoder
#define outputA 6
#define outputB 7
#define sw 4
int aState;
int aLastState;
void fn_atras();
void fn_temp();
void fn_H1();
void fn_H2();
void fn_H3();
void fn_C();
float temp0 = sensorDS18B20.getTempCByIndex(0);
float temp1 = sensorDS18B20.getTempCByIndex(1);
float temp2 = sensorDS18B20.getTempCByIndex(2);
float temp3 = sensorDS18B20.getTempCByIndex(3);
float tempDeseadaH1, tempDeseadaH2, tempDeseadaH3,
tempDeseadaC, tempAgua;
unsigned long valorEncoder = 0;
// Defino Pantalla 1
LiquidLine linea1(0, 0," Hab. 1: ", temp0, " C");
LiquidLine linea2(0, 1," Hab. 2: ", temp1, " C");
LiquidLine linea3(0, 2," Hab. 3: ", temp2, " C");
LiquidLine linea4(0, 3," Living: ", temp3, " C");
LiquidScreen pantalla1(linea1, linea2, linea3, linea4);
//Defino panalla que modifica la temperatura
LiquidLine linea1_2(0, 0, "Temp. deseada");
LiquidLine linea2_2(1, 1, fn_temp);
LiquidLine linea3_2(1, 0, "Atras");
LiquidScreen pantallaModTemp(linea1_2, linea2_2, linea3_2);
LiquidMenu menu(lcd, pantalla1, pantallaModTemp);
void setup() {
pinMode(sw, INPUT_PULLUP);
menu.init();
lcd.init();
lcd.begin(16, 2);
lcd.backlight();
//Indicador de posiscion
linea1.set_focusPosition(Position::LEFT);
linea2.set_focusPosition(Position::LEFT);
linea3.set_focusPosition(Position::LEFT);
linea4.set_focusPosition(Position::LEFT);
//Funcion de cada linea
linea1.attach_function(1, fn_H1);
linea2.attach_function(1, fn_H2);
linea3.attach_function(1, fn_H3);
linea4.attach_function(1, fn_C);
menu.add_screen(pantalla1);
//Indicador de posiscion
linea2_2.set_focusPosition(Position::LEFT);
linea3_2.set_focusPosition(Position::LEFT);
//Funcion de cada linea
linea2_2.attach_function(1, fn_temp);
linea3_2.attach_function(1, fn_atras);
menu.add_screen(pantallaModTemp);
//Se indicará la cantidad de lineas de cada pantalla
pantalla1.set_displayLineCount(2);
pantallaModTemp.set_displayLineCount(2);
menu.set_focusedLine(0);
Serial.begin(9600);
sensorDS18B20.begin();
pinMode(pinReleCalefon, OUTPUT);
pinMode(pinReleElectH1, OUTPUT);
pinMode(pinReleElectH2, OUTPUT);
pinMode(pinReleElectH3, OUTPUT);
pinMode(pinReleElectrovalvula, OUTPUT);
pinMode(pinReleBombaAgua, OUTPUT);
menu.update();
}
// void setup() {
// lcd.init();
// lcd.backlight();
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print(" FELIZ NAVIDAD "); // Texto en LCD, linea 1
// lcd.setCursor(0,1);
// lcd.print("y PROSPERO 2050"); // Texto en LCD, linea 2
// delay(4000); // 4 Seg.
// }
void loop() {
sensorDS18B20.requestTemperatures();
float tempH1 = sensorDS18B20.getTempCByIndex(0);
float tempH2 = sensorDS18B20.getTempCByIndex(1);
float tempH3 = sensorDS18B20.getTempCByIndex(2);
float tempC = sensorDS18B20.getTempCByIndex(3);
float agua = sensorDS18B20.getTempCByIndex(4);
selectOption();
aState = digitalRead(outputA);
if (aState != aLastState) {
if (digitalRead(outputB) != aState) {
menu.switch_focus(false);
} else {
menu.switch_focus(true);
}
menu.update();
aLastState = aState;
}
//Control de relee de habitación 1
if (tempH1 < tempDeseadaH1 && tempAgua > 40) {
// Enciende la electroválvula y la bomba de agua
digitalWrite(pinReleElectrovalvula, LOW);
digitalWrite(pinReleBombaAgua, LOW);
} else if (tempH1 > tempDeseadaH1) {
// Apaga la bomba de agua y cierra la electroválvula
digitalWrite(pinReleBombaAgua, HIGH);
digitalWrite(pinReleElectrovalvula, HIGH);
}
//Control de relee de habitación 2
if (tempH2 < tempDeseadaH2 && tempAgua > 40) {
// Enciende la electroválvula y la bomba de agua
digitalWrite(pinReleElectrovalvula, LOW);
digitalWrite(pinReleBombaAgua, LOW);
} else if (tempH2 > tempDeseadaH2) {
// Apaga la bomba de agua y cierra la electroválvula
digitalWrite(pinReleBombaAgua, HIGH);
digitalWrite(pinReleElectrovalvula, HIGH);
}
//Control de relee de habitación 3
if (tempH3 < tempDeseadaH3 && tempAgua > 40) {
// Enciende la electroválvula y la bomba de agua
digitalWrite(pinReleElectrovalvula, LOW);
digitalWrite(pinReleBombaAgua, LOW);
} else if (tempH3 > tempDeseadaH3) {
// Apaga la bomba de agua y cierra la electroválvula
digitalWrite(pinReleBombaAgua, HIGH);
digitalWrite(pinReleElectrovalvula, HIGH);
}
//Control de relee de comedor
if (tempC < tempDeseadaC && tempAgua > 40) {
// Enciende la electroválvula y la bomba de agua
digitalWrite(pinReleElectrovalvula, LOW);
digitalWrite(pinReleBombaAgua, LOW);
} else if (tempC > tempDeseadaC) {
// Apaga la bomba de agua y cierra la electroválvula
digitalWrite(pinReleBombaAgua, HIGH);
digitalWrite(pinReleElectrovalvula, HIGH);
}
// Control del calefón
if (tempAgua < 40) {
// Enciende el calefón
digitalWrite(pinReleCalefon, LOW);
} else if (tempAgua > 50) {
// Apaga el calefón
digitalWrite(pinReleCalefon, HIGH);
}
}
//Funciones
//Selección de datos
void selectOption() {
if (digitalRead(sw) == LOW) {
Serial.print("boton");
menu.call_function(1);
delay(1000);
}
}
//Pantalla 1
void fn_H1() {
menu.change_screen(2);
menu.set_focusedLine(1);
relee_seleccionado = 1;
}
void fn_H2() {
menu.change_screen(2);
menu.set_focusedLine(1);
relee_seleccionado = 2;
}
void fn_H3() {
menu.change_screen(2);
menu.set_focusedLine(1);
relee_seleccionado = 3;
}
void fn_C () {
menu.change_screen(2);
menu.set_focusedLine(1);
relee_seleccionado = 4;
}
//Pantalla para modificar la temperatura
void fn_temp() {
switch (relee_seleccionado) {
case 1:
int tempDeseadaH1 = constrain(valorEncoder, 16, 32);
break;
case 2:
int tempDeseadaH2 = constrain(valorEncoder, 16, 32);
break;
case 3:
int tempDeseadaH3 = constrain(valorEncoder, 16, 32);
break;
case 4:
int tempDeseadaC = constrain(valorEncoder, 16, 32);
break;
}
}
void fn_atras() {
menu.change_screen(1);
menu.set_focusedLine(0);
}
Para poder compilarlo hay que modificar el código de LiquidMenu para que sea compatible con I2C, el archivo es: "LiquidMenu/src/LiquidMenu_config". Dejo aca el codigo ya modificado:
/**
@file
Configuration file for LiquidMenu library.
Contains global constants the configure the size of some of the arrays
used in the library, also configures the debugging messages.
*/
#pragma once
#include "LiquidMenu_const.h"
// Select a "LiquidCrystal" library:
// ---------------------------------
/*!
* @name Select a "LiquidCrystal" library
* @{
*/
/*!
* @name Arduino's parallel "LiquidCrystal" library
* @{
*/
//#ifndef LIQUIDMENU_LIBRARY
/// Wrapped library ID
// #define LIQUIDMENU_LIBRARY LiquidCrystal_LIBRARY
//#endif
//#ifndef DisplayClass
/// Name of wrapped library's class
// #define DisplayClass LiquidCrystal
//#endif
//!@}
/*!
* @name I2C library
* @see https://github.com/johnrickman/LiquidCrystal_I2C
* @{
*/
#ifndef LIQUIDMENU_LIBRARY
#define LIQUIDMENU_LIBRARY LiquidCrystal_I2C_LIBRARY
#endif
#ifndef DisplayClass
#define DisplayClass LiquidCrystal_I2C
#endif
//!@}
/*!
* @name Some other library
* @{
*/
// #include <LIBRARY_HEADER.h>
// #ifndef DisplayClass
// #define DisplayClass LIBRARY_CONSTRUCTOR
// #endif
//!@}
//!@}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// Configures the number of available variables per line.
const uint8_t MAX_VARIABLES = 5; ///< @note Default: 5
/// Configures the number of available functions per line.
const uint8_t MAX_FUNCTIONS = 8; ///< @note Default: 8
/// Configures the number of available lines per screen.
const uint8_t MAX_LINES = 12; ///< @note Default: 12
/// Configures the number of available screens per menu.
const uint8_t MAX_SCREENS = 14; ///< @note Default: 14
/// Configures the number of available menus per menus system.
const uint8_t MAX_MENUS = 8; ///< @note Default: 8
/*!
* Enable/disable hiding the focus indicator.
*
* When enabled the focus indicator will disappear for one step after
* completing an iteration through the focusable lines. When disabled the focus
* indicator will go from the last focusable line directly to the first
* focusable line without disappearing (i.e. it will be always visible).
*/
#define LM_FOCUS_INDICATOR_GHOSTING true ///< @note Default: true
// Turns the debugging messages on or off.
#define LIQUIDMENU_DEBUG false ///< @note Default: false