I have a device set up that changes the color of an led if the temperature is over or under a certain number (position) . You can change this (position) with a rotary encoder or an app. This is where the problem lies.
On the app, decreasing the "position" works fine but increasing is a different story. The position doesn't seem to update whenver I try to increase it. The first picture shows what I mean. I'm using MIT app inventor since Im new to programming.
Heres the Arduino code
#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;
const int BredPin= 11;
const int BgreenPin = 12;
const int BbluePin = 13;
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);
pinMode(BredPin, OUTPUT);
pinMode(BgreenPin, OUTPUT);
pinMode(BbluePin, OUTPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
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, 0, 0, 225);
}
}
{
if (DHT.humidity > 30) {
setColor(BredPin, BgreenPin, BbluePin, 148,0,211);
} else {
setColor(BredPin, BgreenPin, BbluePin, 0, 225, 0);
}
}
{
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;
}
}
String t; //create an empty string to store messages from Android
while(Serial.available()) { //keep reading bytes while they are still more in the buffer
t += (char)Serial.read(); //read byte, convert to char, and append it to string
}
if(t.length()) { //if string is not empty do the following
if(t == "1") { //if the string is equal to "on" then turn LED on
counter ++;
aState = digitalRead;
Serial.print("Position: ");
Serial.println(counter);
aLastState = aState;
}
}
while(Serial.available()) { //keep reading bytes while they are still more in the buffer
t += (char)Serial.read(); //read byte, convert to char, and append it to string
}
if (t == "2") {
counter --;
aState = digitalRead;
Serial.print("Position: ");
Serial.println(counter);
aLastState = aState;
} // turn the LED off by making the voltage LOW
}