|
| 1 | +// Example testing sketch for various DHT humidity/temperature sensors |
| 2 | +// Written by ladyada, public domain |
| 3 | + |
| 4 | +// REQUIRES the following Arduino libraries: |
| 5 | +// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library |
| 6 | +// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor |
| 7 | + |
| 8 | +//*********************************************************************************************** |
| 9 | +// Adafruit's example has been extended for being used with the FluxGarage RoboEyes, |
| 10 | +// showing different robot face expressions and animations in relation to the temperature |
| 11 | +// variable "t". Beside the DHT22 sensor, add an I2C SSD1306 OLED display to get started. |
| 12 | +// |
| 13 | +// Published in August 2025 by Dennis Hoelscher, FluxGarage |
| 14 | +// www.youtube.com/@FluxGarage |
| 15 | +// www.fluxgarage.com |
| 16 | +// |
| 17 | +//*********************************************************************************************** |
| 18 | + |
| 19 | +#include "DHT.h" |
| 20 | + |
| 21 | +#define DHTPIN 2 // Digital pin connected to the DHT sensor |
| 22 | +// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- |
| 23 | +// Pin 15 can work but DHT must be disconnected during program upload. |
| 24 | + |
| 25 | +// Uncomment whatever type you're using! |
| 26 | +//#define DHTTYPE DHT11 // DHT 11 |
| 27 | +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 |
| 28 | +//#define DHTTYPE DHT21 // DHT 21 (AM2301) |
| 29 | + |
| 30 | +// Connect pin 1 (on the left) of the sensor to +5V |
| 31 | +// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 |
| 32 | +// to 3.3V instead of 5V! |
| 33 | +// Connect pin 2 of the sensor to whatever your DHTPIN is |
| 34 | +// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins) |
| 35 | +// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins) |
| 36 | +// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor |
| 37 | + |
| 38 | +// Initialize DHT sensor. |
| 39 | +// Note that older versions of this library took an optional third parameter to |
| 40 | +// tweak the timings for faster processors. This parameter is no longer needed |
| 41 | +// as the current DHT reading algorithm adjusts itself to work on faster procs. |
| 42 | +DHT dht(DHTPIN, DHTTYPE); |
| 43 | + |
| 44 | + |
| 45 | +#include <Adafruit_SSD1306.h> |
| 46 | + |
| 47 | +#define SCREEN_WIDTH 128 // OLED display width, in pixels |
| 48 | +#define SCREEN_HEIGHT 64 // OLED display height, in pixels |
| 49 | +// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) |
| 50 | +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) |
| 51 | +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); |
| 52 | + |
| 53 | +#include <FluxGarage_RoboEyes.h> // https://github.com/FluxGarage/RoboEyes |
| 54 | +roboEyes roboEyes; // create RoboEyes instance |
| 55 | + |
| 56 | +// EVENT TIMER |
| 57 | +unsigned long eventTimer; // will save the timestamps |
| 58 | + |
| 59 | +void setup() { |
| 60 | + Serial.begin(9600); |
| 61 | + |
| 62 | + // Startup OLED Display |
| 63 | + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally |
| 64 | + if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C or 0x3D |
| 65 | + Serial.println(F("SSD1306 allocation failed")); |
| 66 | + for(;;); // Don't proceed, loop forever |
| 67 | + } |
| 68 | + |
| 69 | + // Startup dht sensor lib |
| 70 | + dht.begin(); |
| 71 | + //Serial.println(F("DHTxx test!")); |
| 72 | + |
| 73 | + // Startup robo eyes |
| 74 | + roboEyes.begin(SCREEN_WIDTH, SCREEN_HEIGHT, 100); // screen-width, screen-height, max framerate |
| 75 | + roboEyes.setPosition(DEFAULT); // eye position should be middle center |
| 76 | + roboEyes.setAutoblinker(ON, 3, 2); // start auto blinker |
| 77 | + |
| 78 | + display.setRotation(2); // Rotate the display by 180 degrees - this makes sense if your display is mounted upside down |
| 79 | + |
| 80 | + eventTimer = millis(); // start event timer from here |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +void loop() { |
| 85 | + roboEyes.update(); // update eyes drawings |
| 86 | + // dont' use delay() here in order to ensure fluid eyes animations |
| 87 | + |
| 88 | + // Measure temperature every 5 seconds |
| 89 | + if(millis() >= eventTimer+5000){ |
| 90 | + // Reading temperature or humidity takes about 250 milliseconds! |
| 91 | + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) |
| 92 | + float h = dht.readHumidity(); |
| 93 | + // Read temperature as Celsius (the default) |
| 94 | + float t = dht.readTemperature(); |
| 95 | + // Read temperature as Fahrenheit (isFahrenheit = true) |
| 96 | + float f = dht.readTemperature(true); |
| 97 | + |
| 98 | + // Check if any reads failed and exit early (to try again). |
| 99 | + if (isnan(h) || isnan(t) || isnan(f)) { |
| 100 | + Serial.println(F("Failed to read from DHT sensor!")); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Compute heat index in Fahrenheit (the default) |
| 105 | + float hif = dht.computeHeatIndex(f, h); |
| 106 | + // Compute heat index in Celsius (isFahreheit = false) |
| 107 | + float hic = dht.computeHeatIndex(t, h, false); |
| 108 | + |
| 109 | + /*Serial.print(F("Humidity: ")); |
| 110 | + Serial.print(h); |
| 111 | + Serial.print(F("% Temperature: ")); |
| 112 | + Serial.print(t); |
| 113 | + Serial.print(F("°C ")); |
| 114 | + Serial.print(f); |
| 115 | + Serial.print(F("°F Heat index: ")); |
| 116 | + Serial.print(hic); |
| 117 | + Serial.print(F("°C ")); |
| 118 | + Serial.print(hif); |
| 119 | + Serial.println(F("°F")); |
| 120 | + */ |
| 121 | + Serial.println(t); |
| 122 | + |
| 123 | + if (t < 15){ |
| 124 | + // Robot is freezing if temperature is below 15° Celsius |
| 125 | + roboEyes.setMood(TIRED); |
| 126 | + roboEyes.setHFlicker(ON, 2); |
| 127 | + } |
| 128 | + else if (t > 25){ |
| 129 | + // Robot is sweating if temperature is above 25° Celsius |
| 130 | + roboEyes.setMood(TIRED); |
| 131 | + roboEyes.setSweat(ON); |
| 132 | + roboEyes.setPosition(S); |
| 133 | + } |
| 134 | + else { |
| 135 | + // Robot's comfort zone: temperature is between 15° and 25° Celsius |
| 136 | + roboEyes.setMood(DEFAULT); // neutral face expression |
| 137 | + roboEyes.setPosition(DEFAULT); // eye position: middle center |
| 138 | + roboEyes.setHFlicker(OFF); |
| 139 | + roboEyes.setSweat(OFF); |
| 140 | + } |
| 141 | + |
| 142 | + eventTimer = millis(); // reset timer |
| 143 | + } |
| 144 | +} // end of main loop |
| 145 | + |
0 commit comments