// SPDX-License-Identifier: GPL-3.0-or-later
#include <Arduino.h>
// The DHT library reads temperature and humidity without a long delay() in loop().
#include <dht_nonblocking.h>

// Create a DHT11 sensor object; its signal wire uses Mega digital pin 2.
DHT_nonblocking sensor(2, DHT_TYPE_11);
// Remember the time of the last successful reading using millis().
unsigned long lastMeasurement = 0;

// Start the Serial Monitor at 9600 baud.
void setup() {
  Serial.begin(9600);
}

void loop() {
  // float variables can store measurements with decimal places.
  float temperature;
  float humidity;
  // Only report after at least 3 seconds and when the sensor has a completed reading.
  if (millis() - lastMeasurement > 3000UL && sensor.measure(&temperature, &humidity)) {
    // Save this time so the next successful report is spaced out.
    lastMeasurement = millis();
    Serial.print("T = ");
    // Print temperature with one digit after the decimal point.
    Serial.print(temperature, 1);
    Serial.print(" deg. C, H = ");
    Serial.print(humidity, 1);
    // println() prints text and starts a new line.
    Serial.println("%");
  }
}
