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