Data within menus don't update continuously

Hi there everyone. I started programming on arduino about a week ago and this is one of the projects I'm working on. Basically I combined a few projects together to get different outputs on the OLED screen when you turn the rotary encoder. However I'm running into an issue. The data out of the sensors don't update when I'm on the screen. By that I mean, if I'm on the distance screen the distance does not update continuously. I can't figure out what's wrong. I'd appreciate any help I can get. the sensors I'm using are the dht11 sensor, a rotary encoder and a ultrasonic distance sensor.


#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>

#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 32 

#define DHTPIN 4
#define DHTTYPE DHT11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE);
long scrnWrite(String x, float y, int z);

 #define outputA 11
 #define outputB 12

 int counter = 0; 
 int aState;
 int aLastState; 
 int trigPin = 2;
 int echoPin = 3;
 long duration, cm;




 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   aLastState = digitalRead(outputA);
    Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  dht.begin(); 

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);   
 } 

 void loop() { 
  float h;
  float t;
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
   aState = digitalRead(outputA); 
   
   if (aState != aLastState){     
     
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     if (counter >= 5 || counter <= -1)
     {
      counter = 0;
     }
          
     if (counter == 0)
     {
      
        h = dht.readHumidity();
        scrnWrite("H(%):", h, 2 );
      
     }

     else if (counter == 2)
     {
      
        t = dht.readTemperature();
        scrnWrite("T(C):", t, 2 );
      
     }

     else if (counter == 4)
     {
      
        digitalWrite(trigPin, LOW);
        delayMicroseconds(5);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
        cm = (duration/2) / 29.1;
        scrnWrite("CM:", cm, 2);
      
     }
     
     } 
   aLastState = aState; 
 
 
 }

long scrnWrite(String x, float y, int z)
{
  display.clearDisplay();
  display.setTextSize(z);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.print(x);
  display.println(y);
  display.display();
}


It looks like you only do one update per change in aState:

I’d guess that that design decision would interfere with updates.

Oh I didn't realize that. Thanks for the help!

You declare two variables that will contain unknown, random values (since you did not initialize them to anything) and then you test to see if that a not a number? That test should be further down in your code after you read the humidity or temperature.

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