I just finished my user interface to change temp and humidity setting amongs other. The program is sending High and Low when setting over 0 for debugging
In order to introduce the t/h sensor to compare values, to controll heat and humidity, I added a function void readTH ().
As soon as I add the function to have the R and H reading, the rest of the program compiles but userinterface functions fail.
The sensor part "readTH ()" is debbuged and work fine outside my user interface program.
See "readTH () //------------READ WRITE TEMP----------- " function mid-sketch and the void loop () at the end of sketch.
//----DONE WITH PRECIOUS HELP OF J-M-L jackson-----
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
#include <Toggle.h> // https://www.arduinolibraries.info/libraries/toggle$0
#include <DHT_Async.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
hd44780_I2Cexp lcd;
const uint8_t nbCols = 20;
const uint8_t nbRows = 4;
const byte encoderCLKPin = 2;
const byte encoderDTPin = 3;
const byte encoderSWPin = 4;
const byte relayPin1 = 5;
const byte relayPin2 = 6;
Encoder encoder(encoderDTPin, encoderCLKPin);
Toggle encoderSwitch;
int32_t ticksPerClick = 1;
boolean switchOne = 0; // a flag variable
long encoderValue;
//char mode [] = {retour, temp, humi, vent, uv};
//char ventVal [] = [vOff, v10, v25, v33, v50, v75, vOn ];
//char uvVal = [] = [uOff, u10, u25, u33, u50, v75, uOn ];
byte tempSet = 0;
byte humiSet = 0;
//byte ventSet = 0;
//byte uvSet = 0;
byte setting1 = 0;
byte setting2 = 0;
byte setting3 = 0;
byte setting4 = 0;
byte Mode = 0; // This is which menu mode we are(top level or one of the submenus)
const byte modeMinValue = 0;
const byte modeMaxValue = 4;
const byte tempMinValue = 0;
const byte tempMaxValue = 25;
const byte humiMinValue = 0;
const byte humiMaxValue = 99;
void blink() { // DEBUGGING
static unsigned long chrono = 0;
static bool flipflop = true;
unsigned long now = millis();
if (now - chrono >= 500) {
chrono = now;
digitalWrite(relayPin1, flipflop ? HIGH : LOW);
digitalWrite(relayPin2, flipflop ? LOW : HIGH);
flipflop = !flipflop;
}
}
//--------------------------------
void encoderSetValue(int32_t newValue) {
int32_t currentTicks = encoder.read();
int32_t newPos = (currentTicks - (currentTicks / ticksPerClick) * ticksPerClick) * ticksPerClick;
encoder.write(newPos);
}
//---------MENU LIMIT-------------
bool menuChange(){ //issu de encoderChanged
long newPosition = encoder.read() >> 1; // divide by 2 as the rotary sends 2 ticks per click(change pour 1)
if (newPosition < modeMinValue) {
newPosition = (encoderValue ); //encoderValue au lieu de encoderMinValue ou encoderMaxValue
encoderSetValue(modeMinValue);
} else if (newPosition > modeMaxValue) {
newPosition = modeMinValue;
encoderSetValue(modeMinValue);
}
if (newPosition != encoderValue) {
encoderValue = newPosition;
return true;
}
return false;
//Serial.println(newPosition);
encoderValue = 0;
}
//-----------TEMP LIMIT------------
bool tempChange() { //issu de encoderChanged
long newPosition = encoder.read() >> 1; // divide by 4 as the rotary sends 4 ticks per click(chang/ pour 1)
if (newPosition < tempMinValue) {
newPosition = (encoderValue ); //encoderValue au lieu de tempMinValue ou tempMaxValue
encoderSetValue(tempMaxValue);
} else if (newPosition > tempMaxValue) {
newPosition = tempMinValue;
encoderSetValue(tempMinValue);
}
if (newPosition != encoderValue) {
encoderValue = newPosition;
//newPosition = tempSet;
return true;
}
return false;
}
//------------HUMI LIMIT------------
bool humiChange() { //issu de encoderChanged
long newPosition = encoder.read() >> 1; // divide by 4 as the rotary sends 4 ticks per click(chang/ pour 1)
if (newPosition < humiMinValue) {
newPosition = (encoderValue ); //encoderValue au lieu de humiMinValue ou humiMaxValue
encoderSetValue(humiMaxValue);
} else if (newPosition > humiMaxValue) {
newPosition = humiMinValue;
encoderSetValue(humiMinValue);
}
if (newPosition != encoderValue) {
encoderValue = newPosition;
//newPosition = tempSet;
return true;
}
return false;
}
//------------READ/WRITE TEMP------------
void readTH () {
delay(2000);
//float f = dht.readTemperature(true);
int t = dht.readTemperature();
//float h = dht.readHumidity();
int h = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
//if (isnan(h) || isnan(t)) {
//Serial.println(F("Failed to read from DHT sensor!"));
//lcd.print("Failed to read from DHT sensor!");
//return;
//}
Serial.print (t);
lcd.setCursor(9, 0);
lcd.print(t);
Serial.print (h);
lcd.setCursor(14, 0);
lcd.print(h);
}
//------------PRESS SWITCH----------------
bool pressSwitch() {
encoderSwitch.poll();
long newPosition = encoder.read() >> 1;
if (encoderSwitch.onPress()) {
Serial.println("PRESSED");
Serial.println("new value");
Serial.println(newPosition);
return true;
}
return false;
}
//---------REALTIME LCD SETTING PRINT----
void showSetting (int setting1, int setting2 ) { //FONCTION AFFICHE VAL TEMPS REEL
if (Mode == 1) {
lcd.setCursor(6,1);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(encoderValue);
delay(10);
}
if (Mode == 2) {
lcd.setCursor(6,3);
lcd.print(" ");
lcd.setCursor(6,3);
lcd.print(encoderValue);
delay(10);
}
}
//----------RESET TO MENU ITEM------
void setAdmin(byte name, byte setting){
Serial.print("Setting ");
Serial.print(name);
Serial.print(" = ");
Serial.println(setting);
encoderValue = Mode; // reorientate the menu index to menu, same item
Mode = 0; // back to top of menu, value set
Serial.println("Main Menu");
}
//----------------MENU ENCODER----------------
void menuEncoder() { //anciennement testEncoder
if (Mode == 0) {
menuChange();
//CURSEUR
switch(encoderValue){
case 0 : //MENU RETOUR
lcd.setCursor(0,1);lcd.print(" ");lcd.setCursor(0,3);lcd.print(" ");lcd.setCursor(11,1);
lcd.print(" ");lcd.setCursor(11,3);lcd.print(" ");lcd.setCursor(0,0);lcd.print("<<");
break;
case 1:
lcd.setCursor(13,0);lcd.print(" ");lcd.setCursor(0,3);lcd.print(" ");lcd.setCursor(11,3);
lcd.print(" ");lcd.setCursor(11,1);lcd.print(" ");lcd.setCursor(0,1);lcd.print(">");
lcd.setCursor(0,0);lcd.print(" ");
break;
case 2:
lcd.setCursor(13,0);lcd.print(" ");lcd.setCursor(0,1);lcd.print(" ");lcd.setCursor(11,1);
lcd.print(" ");lcd.setCursor(11,3);lcd.print(" ");lcd.setCursor(0,0);lcd.print(" ");
lcd.setCursor(0,3);lcd.print(">");
break;
case 3:
lcd.setCursor(13,0);lcd.print(" ");lcd.setCursor(0,1);lcd.print(" ");lcd.setCursor(0,3);
lcd.print(" ");lcd.setCursor(11,3);lcd.print(" ");lcd.setCursor(0,0);lcd.print(" ");
lcd.setCursor(11,1);lcd.print(">");
break;
case 4:
lcd.setCursor(13,0);lcd.print(" ");lcd.setCursor(0,1);lcd.print(" ");lcd.setCursor(0,3);
lcd.print(" ");lcd.setCursor(11,1);lcd.print(" ");lcd.setCursor(0,0);lcd.print(" ");
lcd.setCursor(11,3);lcd.print(">");
break;
}
if (pressSwitch()){
Mode = encoderValue; // set the Mode to the current value of input if button has been pressed
//Serial.print("Mode selected: "); //DEBUGGING: print which mode has been selected
//Serial.println(Mode); //DEBUGGING: print which mode has been selected
}
}
if (Mode == 1){
//encoderValue = setting1;
tempChange();
//Serial.println("TEMP_Mode 1"); //DEBUGGING: print which mode has been selected
Serial.print (setting1);
}
if (Mode == 2) {
humiChange();
//Serial.println("HUMI_Mode 2"); //DEBUGGING: print which mode has been selected
//Serial.print (setting2); // start adjusting HUMIDITY from last set point
}
if (Mode == 3) {
// Serial.println("VENT_Mode 3"); //DEBUGGING: print which mode has been selected
encoderValue = setting3; // start turning FAN on or off
}
if (Mode == 4) {
// Serial.println("UV_Mode 4"); //DEBUGGING: print which mode has been selected
encoderValue = setting4; // start turning UV on or off
}
if(Mode == 1 && pressSwitch()){
setting1 = encoderValue;
if(setting1 > 0){ //led when relay is ON
digitalWrite(5, HIGH);
}
if (setting1 == 0){ //no led when relay is OFF
digitalWrite(5,LOW);
}
lcd.setCursor(6,1);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(setting1);
setAdmin (1,setting1);
}
if (Mode == 2 && pressSwitch()) {
setting2 = encoderValue; // record whatever value your encoder has been turned to, to setting 2
if (setting2 > 0){ //DEBUGGING
digitalWrite(6, HIGH);//DEBUGGING
}
if (setting2 == 0){ //DEBUGGING
digitalWrite(6,LOW);//DEBUGGING
}
lcd.setCursor(6,3);
lcd.print(" ");
lcd.setCursor(6,3);
lcd.print(setting2);
setAdmin(2,setting2);
}
}
void setup() {
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
encoderSwitch.begin(encoderSWPin);
Serial.begin(9600);
int result = lcd.begin(nbCols, nbRows);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("CharcUmami");
lcd.setCursor(3, 2);
lcd.print("Version 1.1.2");
lcd.setCursor(0, 2);
lcd.print("");
delay(1000);
lcd.clear(); //clear the whole LCD
lcd.setCursor(1, 1);
lcd.print("TEMP"); //text
//----------------------
lcd.setCursor(1, 3);
lcd.print("HUMI"); //text
//----------------------
lcd.setCursor(12, 1);
lcd.print("VENT"); //text
//----------------------
lcd.setCursor(12, 3);
lcd.print("UV"); //text
//----------------------
lcd.setCursor(8, 1);
lcd.print((char)223); //signe de degrees
//----------------------
lcd.setCursor(9, 1);
lcd.print("C"); //signe de degrees
//----------------------
lcd.setCursor(9, 3);
lcd.print("%"); //text
//----------------------
lcd.setCursor(17, 1);
lcd.print("off"); //text
//----------------------
lcd.setCursor(17, 3);
lcd.print("off"); //text
lcd.setCursor(17, 3);
lcd.print("off"); //text
lcd.setCursor(12, 0); // afficher humi en haut
lcd.print("H");
lcd.setCursor(5, 0); // afficher temp en haut
lcd.print("T");
}
void loop() {
menuEncoder();
showSetting (setting1, setting2 );
//readTH ();
//blink();
}
