#include #include #include #include #include "DHT.h" #include Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position int buz = 0; // variable to store the servo position #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define DHTPIN 7 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 DHT dht(DHTPIN, DHTTYPE); void setup() { myservo.attach(8); // attaches the servo on pin 8 to the servo object // initialize Buzzer pins pinMode(11, OUTPUT); Serial.begin(9600); dht.begin(); Serial.println(F("DHT Ready to go!")); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } display.display(); delay(1000); // Pause for 2 seconds // Clear the buffer display.clearDisplay(); } void loop() { display.clearDisplay(); display.setTextSize(2); // Draw 2X-scale text display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println(F("Failed to read from DHT sensor!")); return; } buz = map(h, 0, 100, 100, 1000); pos = map(t, 25, 75, 0, 130); myservo.write(pos); // tell servo to go to mapped position in variable 'pos' delay(15); display.print(F("Hr: ")); display.print(h); display.println(F("%")); display.print(F("T: ")); display.print(t); display.println(F("C")); display.display(); delay (1000); display.clearDisplay(); Serial.print(F("buz: ")); Serial.print(buz); Serial.print(F("Angle: ")); Serial.println(pos); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.println(F("°C ")); alarm(); } void alarm() { tone(11, buz); // turn on arduino active buzzer delay(1000); noTone(11); // disable arduino active buzzer delay(500); }