Hi,
I have a project where I want to display two DHT22 readings on a 128x64 OLED display. I have everything working seperatly but are struggeling when combining.
The problem seems to be that the displays stops updating when I initiate the dht reading. The serial keeps printing, so I know the code is running. I dont understand where the issue is.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <DHT.h>
// Pin definitions
#define BUTTON_START_STOP 12
#define BUTTON_INC_ID 13
#define RGB_RED 3
#define RGB_GREEN 5
#define RGB_BLUE 6
#define DHT_PIN1 10
#define DHT_PIN2 11
// Constants
#define DHTTYPE DHT22
// Objects
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
DHT dht1(DHT_PIN1, DHTTYPE);
DHT dht2(DHT_PIN2, DHTTYPE);
// Variables
bool testRunning = false;
bool errorState = false;
unsigned long testStartTime = 0;
unsigned long lastBlinkTime = 0;
unsigned long lastUpdateTime = 0;
unsigned long currentTime = 0;
int testID = 1;
bool blinkState = false;
bool buttonStartStopLastState = LOW;
bool buttonIncIdLastState = LOW;
float h0 = 0;
float t0 = 0;
float h1 = 0;
float t1 = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_START_STOP, INPUT_PULLUP);
pinMode(BUTTON_INC_ID, INPUT_PULLUP);
pinMode(RGB_RED, OUTPUT);
pinMode(RGB_GREEN, OUTPUT);
pinMode(RGB_BLUE, OUTPUT);
dht1.begin();
dht2.begin();
display.begin(0x3C, true); // Address 0x3C default
display.setRotation(1);
display.display(); // Show splashscreen
delay(1000);
display.clearDisplay();
updateDisplay();
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 50);
}
void loop() {
currentTime = millis();
// Handle start/stop button
bool buttonStartStopState = digitalRead(BUTTON_START_STOP);
if (buttonStartStopState == LOW && buttonStartStopLastState == HIGH) {
if (!errorState) {
testRunning = !testRunning;
if (testRunning) {
testStartTime = millis();
}
if (testRunning == false){
testID++;
}
updateLEDs();
}
}
buttonStartStopLastState = buttonStartStopState;
//Blinking dot
if (currentTime - lastBlinkTime > 500) {
blinkState = !blinkState;
lastBlinkTime = currentTime;
updateDisplay();
}
// Update display with test data every second
if (testRunning && (currentTime - lastUpdateTime >= 1000)) {
lastUpdateTime = currentTime;
//Save to SD
}
if (currentTime - lastUpdateTime >= 5000) {
lastUpdateTime = currentTime;
h0 = dht1.readHumidity();
t0 = dht1.readTemperature();
h1 = dht2.readHumidity();
t1 = dht2.readTemperature();
}
}
void updateLEDs() {
if (errorState) {
// Flickering red
analogWrite(RGB_RED, 50);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 0);
} else if (testRunning) {
// Green
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 50);
} else {
// Yellow (Standby)
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 50);
analogWrite(RGB_BLUE, 0);
}
}
// void updateData() {
// h0 = dht1.readHumidity();
// t0 = dht1.readTemperature();
// h1 = dht2.readHumidity();
// t1 = dht2.readTemperature();
// }
void updateDisplay() {
currentTime = millis();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.print("Test ID: ");
display.print(testID);
display.setCursor(0, 10);
display.print("Status: ");
if (errorState) {
display.print("Error");
} else if (testRunning) {
display.print("Running");
} else {
display.print("Standby");
}
Serial.print(F("Humidity_0: "));
Serial.print(h0);
Serial.print(F("% Temperature_0: "));
Serial.print(t0);
Serial.println(F("°C "));
Serial.print(F("Humidity_1: "));
Serial.print(h1);
Serial.print(F("% Temperature_1: "));
Serial.print(t1);
Serial.println(F("°C "));
Serial.println("------------------------");
display.setCursor(0, 20);
display.print("Sen1_hum: ");
display.print(h1);
display.print("RH");
display.setCursor(0, 30);
display.print("Sen1_tmp: ");
display.print(t1);
display.print("C");
display.setCursor(0, 40);
display.print("Time: ");
display.print((millis() - testStartTime) / 1000);
display.print("s");
display.setCursor(120, 0);
display.print(blinkState ? "." : " ");
display.display();
}