Back to Parent

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"    //DTH sensor module
#define DHTPIN D5     // Digital pin connected to the DHT sensor

//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

int relayPin = D2;
float HUMID_PANICPARAM_LOW = 40.0;
float HUMID_PANICPARAM_HIGH = 85.0;

//define temperature baseline based on current ambient temperature
float TEMP_PANICPARAM_LOW = 74;
float TEMP_PANICPARAM_HIGH = TEMP_PANICPARAM_LOW + 8;


// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  // Wait one second before the things starts.
  delay(1000);
  pinMode(relayPin, OUTPUT);
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float readHumid = dht.readHumidity();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float readTemp = dht.readTemperature(true);


  // Check if any reads failed and exit early (to try again).
  if (isnan(readHumid) || isnan(readTemp)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;}


  //print to represent current humidity and temperature readings.
  Serial.print(F("Humidity: "));
  Serial.print(readHumid);
  Serial.print(F("%  Temperature: "));
  Serial.print(readTemp);
  Serial.println(F("F"));

  // fully turn on Bob when temp and humid level reaches 'happy level'
  if ((readHumid > HUMID_PANICPARAM_HIGH ) && (readTemp > TEMP_PANICPARAM_HIGH)) {
    Serial.println("Bob is happy");
    digitalWrite(relayPin, HIGH); // Call the relay panic mode function with the current temperature
    delay(5000);
  }

  // when temp and humid level drops below 'happy level' Bob begins to panic/blinking and defined interval
  else if ((readHumid < HUMID_PANICPARAM_HIGH ) || ((readTemp < TEMP_PANICPARAM_HIGH) && (readTemp > TEMP_PANICPARAM_LOW ))) {
    Serial.println("Bob is panicing");
    relayPanicMode(readTemp); // Call the relay panic mode function with the current temperature
  }

  //Bob 'dies' when the humid and temp level drops below acceptable range 
  else {
     digitalWrite(relayPin, LOW);
     Serial.println("Bob is dead");
     delay(3000);
  }
}

//panic mode function to define the blinking interval of Bob.
void relayPanicMode(float currentTemp) {
  //random factor to randomize interval a bit
  int rd1 = random(-300, 300);
  int rd2 = random(-1000, 1000);

  //parameters for on/off time of Bob when panicing
  int onTimeMax = 2500 + rd2;
  int onTimeMin = 50 + rd1;
  int offTimeMax = 2500 + rd2;
  int offTimeMin = 50 + rd1;

  //remap temperature level to corresponding panic time interval
  int remapOnTime = constrain(map(currentTemp, TEMP_PANICPARAM_LOW, TEMP_PANICPARAM_HIGH, onTimeMin, onTimeMax),50, 3000);
  int remapOffTime = constrain(map(currentTemp, TEMP_PANICPARAM_LOW, TEMP_PANICPARAM_HIGH, offTimeMax, offTimeMin),50, 3000);

  //send data to control the on/off status of the relay
  digitalWrite(relayPin, HIGH); // Turn on the relay
  Serial.print("Bob ON time: ");
  Serial.println( remapOnTime);
  delay(remapOnTime); // Wait for the on time

  digitalWrite(relayPin, LOW); // Turn off the relay
  delay(remapOffTime); // Wait for the off time
  Serial.print("Bob OFF time: ");
  Serial.println( remapOffTime);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0