Insert temp/humidity sensor in user interface == FAIL

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();
}

See the pinned post re how to get the best from the forum.

pinned post re ?

how to get the best from the forum. That post is pinned in every category.

I just tried to compile your code and it failed on missing library #include <DHT_Async.h>. I couldn't find it so I tried commenting it out. The sketch compiled just fine.
Why do you have an unused library that appears to also deal with temp and humid?

DHT_Async.h was probably left over from debugging my DHT tests, it is useless. DHT.h is running the DHT and returning values normally, exept when integrated into the interface program. compile put run erratically atrinting on lcd.

I wonder if the is too much communication with lcd that creates "noise" in the micro controller... reading and searching

If a code does compile. The code runs. So what do you obesrve ? Post a very detailed description what you observe that makes you describe it rather foggy as

I suspect that polling the encoder does not work anymore, because reading the DHT-sensors takes too long.

You should use an encoder-code that is based either on a timer-interrupt or on pin-interrupt.
Depending on what microcontroller you are using and if you need IO-pins for interrupts use a timer-interrupt.

Otherwise use NewEncoder.h configured to use interrupts on the encoder data and clock line

This is s democ-ode that uses a timer-interrupt.
hardware-timers are microcontroller-specific.
You will have to post what exact type of microcontroller you are using to determine if this code will work with your microcontroller

/* Demo-Code that uses the Rotary-library from GitHub-User https://github.com/buxtronix
 * using his library https://github.com/buxtronix/arduino/tree/master/libraries/Rotary  
 * in combination with a timer-interrupt executed 10000 times per second
 * Copyright 2023 StefanL38. Licenced under the GNU GPL Version 3.
 * A T T E N T I O N ! 
 * this demo-code uses Timer2 which is used by other libraries too. 
 * This means using this code can interfere with other libraries
 * causing malfunction of both
 * 
 * As timer-interrupts are very hardware-sepcific
 * this demo-code works with Arduino-Uno R3 based on the chip AtMega328P
 * if you want to use it on other microcontrollers you will have to
 * modify setting up the timer-interrupt according to the hardware
 * of this other microcontroller
 * 
 * The demo-code simply prints the value of variable myCounter 
 * each time the value of the variable changes
 */
#include <Rotary.h>
#include <MobaTools.h>

const byte dirPin = 4;
const byte stepPin = 5;
const int  stepsPerRev = 200;

MoToStepper myStepper( stepsPerRev, STEPDIR );  // create a stepper instance

const byte CLK1 = 2;
const byte DT1  = 8;

const byte CLK2 = 3;
const byte DT2  = 11;

const byte CLK3 = 21;
const byte DT3  = 12;

Rotary rotary1 = Rotary(CLK1, DT1);
Rotary rotary2 = Rotary(CLK2, DT2);
Rotary rotary3 = Rotary(CLK3, DT3);

unsigned long myISR_TimerFrequency = 1000;
// Cnt1000, Cnt100, Cnt10 that will be incremented or decremented by rotation.
// as these variables are changed in an interrupt-service-routine
// this variable MUST !! be declared volatile to make sure 
// that it works properly !

volatile unsigned char Cnt1000 = 0;
volatile unsigned char Cnt100  = 0;
volatile unsigned char Cnt10   = 0;

long runSpeed = 0;
long last_runSpeed = 0;


void setupTimerInterrupt(unsigned long ISR_call_frequency) {
  long OCR2A_value;

  const byte Prescaler___8 = (1 << CS21);
  const byte Prescaler__32 = (1 << CS21) + (1 << CS20);
  const byte Prescaler__64 = (1 << CS22);
  const byte Prescaler_128 = (1 << CS22) + (1 << CS20);
  const byte Prescaler_256 = (1 << CS22) + (1 << CS21);
  const byte Prescaler1024 = (1 << CS22) + (1 << CS21) + (1 << CS20);

  const unsigned long CPU_Clock = 16000000;

  cli();//stop interrupts

  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0

  TCCR2A |= (1 << WGM21); // turn on CTC mode
  TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt

  TCCR2B = Prescaler__32; 
  // OCR2A_value must be smaller than 256
  // use a different prescaler if OCR2A_value is > 256
  OCR2A_value = (CPU_Clock / ( 32 * ISR_call_frequency) )  - 1;

  OCR2A = OCR2A_value; // set the value of OCR2A

  sei();//allow interrupts
}


// timer-interrupt-service-function for AtMega328P
ISR(TIMER2_COMPA_vect) {
  processEncoders();
}


// counts up/down with each encoder-click
void processEncoders() {
  unsigned char result;
  
  result = rotary1.process();
  if (result == DIR_CW) {
    Cnt1000++;
  }
  else if (result == DIR_CCW) {
    Cnt1000--;
  }
  if (Cnt1000 < 0) {
    Cnt1000 = 0;
  }
  
  result = rotary2.process();
  if (result == DIR_CW) {
    Cnt100++;
  }
  else if (result == DIR_CCW) {
    Cnt100--;
  }
  if (Cnt100 < 0) {
    Cnt100 = 0;
  }
  
  result = rotary3.process();
  if (result == DIR_CW) {
    Cnt10++;
  }
  else if (result == DIR_CCW) {
    Cnt10--;
  }
  if (Cnt10 < 0) {
    Cnt10 = 0;
  }
}


void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer = 0;  // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);

  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  setupTimerInterrupt(myISR_TimerFrequency);
  
  myStepper.attach( stepPin, dirPin );
  myStepper.setMaxSpeed(1000 * 10);
  myStepper.setSpeed(50 * 10);
  myStepper.setRampLen(0);
  myStepper.rotate(1); // other rotation-direction myStepper.rotate(-1);
}


void loop() {

  BlinkHeartBeatLED(OnBoard_LED, 250);

  runSpeed = Cnt1000 * 1000 + Cnt100 * 100 + Cnt10 * 10;

  // check if value has changed
  if (last_runSpeed != runSpeed) {
    // only if value REALLY HAS changed
    myStepper.setSpeed(runSpeed);
    last_runSpeed = runSpeed; // update last_runSpeed
    Serial.print("runSpeed=");
    Serial.println(runSpeed);
  }
}

newEncoder-library

I slightly changed the code since last post but still the same problem. I think the idea of to many polls and slow read of DHT could be the problem indeed . Here is a picture of what it does when I try to print the H and T values on lcd followed by last version of code:

Using original Uno atmel mega 328P


#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.h"

#define DHTPIN 11    
#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 ];

//---------MODE 0------------------

  //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;
  //}  



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;
  //}  
  lcd.setCursor(5, 0);
  lcd.print("Running..."); 

  Serial.print (t);
  lcd.setCursor(6, 1);
  lcd.print("  ");
  lcd.setCursor(6, 1);
  lcd.print(t); 

  Serial.print (h);
  lcd.setCursor(6, 3);
  lcd.print("  ");
  lcd.setCursor(6, 3);
  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);
    switchOne = 1;
    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(encoderValue == 0 && pressSwitch()){
          //Mode = 0;
          //readTH();
          //if (Mode == 0 && pressSwitch ()){
          //lcd.setCursor (5,0);
          //lcd.print ("           ");
          //setAdmin (setting1, setting2);
          //return;
         // }
     // }

      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(F("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


}
//--------------VOID LOOP----------------------
void loop() {
  menuEncoder();
  showSetting (setting1, setting2 );
  readTH ();
  //blink();
}

not showing actual readings is likely to have a different cause than too slow polling.

Maybe you can see now that it is very important to give very detailed informations.

Still not really clear what happends in your code.
Usually reading in any kind of value and then printing these values should work.

What does the picture show? Actual readings of the DHT-sensors that do not work ?
or
is this some kind of value setting? This makes a huge difference.

The picture shows H and T values in real time. Ìt is the part of code to show real time data when in Mode0. The set data menu already worked fine. I am pretty sure the sensor reads properly; it works in my test code.

It looks that the problem is about writing the H and T values to the lcd (not reading). With my short test sketch, sensor works fine and send accurate data to the lcd.

So if it does not seem to be a pool thing, could it but that the code does not allow the time for the sensor to read properly ? Here is my working test code with same Library and set up:


void loop() {
 
  delay(2000);
  //float h = dht.readHumidity();
  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  int t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //float f = dht.readTemperature(true);

  // 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(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print( t);
  Serial.print(F("°C "));
  //Serial.print(f);
  //Serial.print(F("°F  Heat index: "));
  //Serial.print(hic);
  //Serial.print(F("°C "));
  //Serial.print(hif);
  //Serial.println(F("°F"));

  //MA PARTIE LCD
  lcd.setCursor(6,1);
  lcd.print(t);
  lcd.setCursor(6,3);
  lcd.print(h);

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