Before I say anything, I'm new to computer hardware and software in general.
Right now, Im trying to use both a temperature/humidity sensor and rotary encoder. I got 2 different sources of code in order to read what with of these sensors are reading and they work. But when I try to combine their code, Arduino doesn't seem to update the position of the rotary encoder as quickly.
#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
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay (300);
}
{
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;
}
}
}