Using two sensors at the same time (DHT and rotary encoder)

Right now I'm trying to use two different sensors together, a DHT and rotary encoder. When I try to use them separately, they work just fine. But when I try to combine the two into a single piece of code, the rotary encoder doesn't seem to be very responsive and even if it does, the position doesn't seem to update quickly enough. I tried getting rid of the delays in case they were the problem but it doesn't seem to solve the issue. Theres a screenshot to roughly show the problem.

Heres my code

#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
#define outputA 6
#define outputB 7
 int counter = 0; 
 int aState;
 int aLastState; 
dht DHT;
 
void setup(){

  pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA);   
  Serial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor
 
}//end "setup()"
 
void loop()
{
 
 {
  //Start of Program 


{ 
   aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
     aLastState = aState;
   }
} 
  
 
    DHT.read11(dht_apin);
    
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
   
  }

   
}

Don't read the DHT11 sensor that often. Take a look at the BlinkWithoutDelay example to see how you can implement the loop() to read the sensor only every 5 seconds or so (reading it more often doesn't make sense as it's sluggish anyway). The reason for this is that reading the DHT sensor is quite slow and because of the rigid timing it blocks the complete processor for longer periods of time.

Those print statements take 50-60 ms to complete (about 1 ms per character at 9600 bps). Try increasing your Serial speed to 115200 or so, that should help a lot. And indeed don't try to read the DHT every loop(), no use doing it any faster than every 2 seconds (that's the update speed of the sensor itself - not counting reaction time to changes in the environment).

What do you mean by serial speed? Sorry I'm not familiar with coding

tylerman1014:
What do you mean by serial speed? Sorry I'm not familiar with coding

Baud rate, the number you provide to the Serial.begin() method and that you configure in your serial monitor.

I've updated my code but the rotary motor still seems to be failing to update the position. switching between the same two numbers when I twist the knob.

#include "dht.h"
#define dht_apin A0 
#define outputA 6
#define outputB 7
int counter = 0; 
int aState;
int aLastState; 
const int redPin= 8;
const int greenPin = 9;
const int bluePin = 10;
unsigned long previousMillis = 0;
const long interval = 5000; 
dht DHT;

void setup(){

  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
 
  Serial.begin (9600);
  // Reads the initial state of the outputA
  aLastState = digitalRead(outputA);   
 Serial.begin(9600);
 delay(500);//Delay to let system boot
 Serial.println("DHT11 Humidity & temperature Sensor\n\n");
 delay(1000);//Wait before accessing Sensor

}

void setColor(int redPin, int greenPin, int bluePin, int red, int green, int blue)
{
#ifdef COMMON_ANODE
 red = 255 - red;
 green = 255 - green;
 blue = 255 - blue;
#endif
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);

}

void loop()
{

{ 
  aState = digitalRead(outputA); // Reads the "current" state of the outputA
  // If the previous and the current state of the outputA are different, that means a Pulse has occured
  if (aState != aLastState){     
    // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
    if (digitalRead(outputB) != aState) { 
      counter ++;
    } else {
      counter --;
    }
    Serial.print("Position: ");
    Serial.println(counter);
    aLastState = aState;
  }
} 
 
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;

   DHT.read11(dht_apin);
   
   Serial.print("Current humidity = ");
   Serial.print(DHT.humidity);
   Serial.print("%  ");
   Serial.print("temperature = ");
   Serial.print(DHT.temperature); 
   Serial.println("C  ");
}

{
 if (DHT.temperature > counter) {
   setColor(redPin, greenPin, bluePin, 225, 0, 0); 
 } else {
  setColor(redPin, greenPin, bluePin, 225, 0, 225);
}

}

{ 
  aState = digitalRead(outputA); // Reads the "current" state of the outputA
  // If the previous and the current state of the outputA are different, that means a Pulse has occured
  if (aState != aLastState){     
    // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
    if (digitalRead(outputB) != aState) { 
      counter ++;
    } else {
      counter --;
    }
    Serial.print("Position: ");
    Serial.println(counter);
    aLastState = aState;
  }
    

} 


}

The baudrate is still 9600.

Which two numbers do you get?

Why do you have the same code block at the beginning and at the end of the loop()?

Depending on circuitry you use you may need the internal pullup activated:

  pinMode (outputA, INPUT_PULLUP);
  pinMode (outputB, INPUT_PULLUP);