Improve output formatting in examples

This commit is contained in:
Phillip Burgess 2019-01-11 17:05:42 -08:00
parent bdd2756ed1
commit 261e997a90
2 changed files with 38 additions and 41 deletions

View File

@ -11,7 +11,7 @@
#include <DHT.h> #include <DHT.h>
#include <DHT_U.h> #include <DHT_U.h>
#define DHTPIN 2 // Pin which is connected to the DHT sensor. #define DHTPIN 2 // Pin connected to the DHT sensor.
// Uncomment the type of sensor in use: // Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT11 // DHT 11
@ -29,30 +29,29 @@ void setup() {
Serial.begin(9600); Serial.begin(9600);
// Initialize device. // Initialize device.
dht.begin(); dht.begin();
Serial.println("DHTxx Unified Sensor Example"); Serial.println(F("DHTxx Unified Sensor Example"));
// Print temperature sensor details. // Print temperature sensor details.
sensor_t sensor; sensor_t sensor;
dht.temperature().getSensor(&sensor); dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------"); Serial.println(F("------------------------------------"));
Serial.println("Temperature"); Serial.println(F("Temperature Sensor"));
Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C"); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C"); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C"); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println("------------------------------------"); Serial.println(F("------------------------------------"));
// Print humidity sensor details. // Print humidity sensor details.
dht.humidity().getSensor(&sensor); dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------"); Serial.println(F("Humidity Sensor"));
Serial.println("Humidity"); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%"); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%"); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%"); Serial.println(F("------------------------------------"));
Serial.println("------------------------------------");
// Set delay between sensor readings based on sensor details. // Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000; delayMS = sensor.min_delay / 1000;
} }
@ -64,21 +63,21 @@ void loop() {
sensors_event_t event; sensors_event_t event;
dht.temperature().getEvent(&event); dht.temperature().getEvent(&event);
if (isnan(event.temperature)) { if (isnan(event.temperature)) {
Serial.println("Error reading temperature!"); Serial.println(F("Error reading temperature!"));
} }
else { else {
Serial.print("Temperature: "); Serial.print(F("Temperature: "));
Serial.print(event.temperature); Serial.print(event.temperature);
Serial.println(" *C"); Serial.println(F("°C"));
} }
// Get humidity event and print its value. // Get humidity event and print its value.
dht.humidity().getEvent(&event); dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) { if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!"); Serial.println(F("Error reading humidity!"));
} }
else { else {
Serial.print("Humidity: "); Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity); Serial.print(event.relative_humidity);
Serial.println("%"); Serial.println(F("%"));
} }
} }

View File

@ -25,7 +25,7 @@ DHT dht(DHTPIN, DHTTYPE);
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
Serial.println("DHTxx test!"); Serial.println(F("DHTxx test!"));
dht.begin(); dht.begin();
} }
@ -44,7 +44,7 @@ void loop() {
// Check if any reads failed and exit early (to try again). // Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) { if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!"); Serial.println(F("Failed to read from DHT sensor!"));
return; return;
} }
@ -53,17 +53,15 @@ void loop() {
// Compute heat index in Celsius (isFahreheit = false) // Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false); float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: "); Serial.print(F("Humidity: "));
Serial.print(h); Serial.print(h);
Serial.print(" %\t"); Serial.print(F("% Temperature: "));
Serial.print("Temperature: ");
Serial.print(t); Serial.print(t);
Serial.print(" *C "); Serial.print(F("°C "));
Serial.print(f); Serial.print(f);
Serial.print(" *F\t"); Serial.print(F("°F Heat index: "));
Serial.print("Heat index: ");
Serial.print(hic); Serial.print(hic);
Serial.print(" *C "); Serial.print(F("°C "));
Serial.print(hif); Serial.print(hif);
Serial.println(" *F"); Serial.println(F("°F"));
} }