Hello and thank you in advance for reading.
I have a conceptual and structural issue with coding up a DHT sensor.
I have coded up an example that hopefully explains what I am doing and should highlight what Is going on.
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x3F);
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
lcd.begin(16, 2);
}
void loop() {
lcd_function();
}
void lcd_function(){
// Wait a few seconds between measurements.
delay(2000);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// serial porting
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.println("°C ");
//LCD porting
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Hum: "));
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print(F("Temp: "));
lcd.print(temperature);
lcd.print("C");
}
Now the issue is the "float variables" only work within a void sub program.
I need the DHT sensor to work globally! So... say for instance I write code in "void loop" or any other void for the matter; I that need the sensor information to operate other functions correctly (relays switching on and off).
HOW?
I welcome examples and links to further learning.