/* Arduino example code for DHT11, DHT22/AM2302 and DHT21/AM2301 temperature and humidity sensors. More info: www.www.makerguides.com */
// Include the libraries:
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <EEPROM.h>
#define SAMPLE_TIME 2000 //The time between each EEPROM write function call in ms
int tempPin = 0; //the ADC pin
int printPin = 2; //the print button pin
int erasePin = 4; //the erase button pin
int sensor = 3; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int address = 0; //EEPROM address counter
unsigned long timer;
float conv_coeff = 0.0; //coefficient for converting from 0-1024 to 0-5 range
void printTemp();
void clearEEPROM();
void writeTemp();
// Set DHT pin:
#define DHTPIN 2
// Set DHT type, uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor for normal 16mhz Arduino:
DHT dht = DHT(DHTPIN, DHTTYPE);
void setup() {
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
// Setup sensor:
dht.begin();
pinMode(sensor, INPUT); // initialize sensor as an input
conv_coeff = 4; //find the coefficient to do the conversion
timer = millis(); //millis() returns the time since program start in ms
clearEEPROM();
}
void loop() {
// Wait a few seconds between measurements:
delay(2000);
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read the humidity in %:
float h = dht.readHumidity();
// Read the temperature as Celsius:
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again):
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius:
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % |");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" \xC2\xB0");
Serial.print("C | ");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" \xC2\xB0");
Serial.println("C | ");
EEPROM.write(address, hic * conv_coeff); //write value to current address counter address
Serial.print("Sensor value stored at address ");
Serial.println(address);
address++; //increment address counter
if(address == EEPROM.length()) //check if address counter has reached the end of EEPROM
{
printTemp(); //if yes: reset address counter
}
if(address == 20)
{
printTemp();
}
}
void printTemp()
{
for (int i = 0 ; i < EEPROM.length() ; i++) {
byte value = EEPROM.read(i); //read EEPROM data at address i
if(value != 0) //skip "empty" addresses
{
float temp = value / conv_coeff; //convert ADC values to temperature
//temp = (temp - 0.5)*100; //take care of the offset
Serial.println(temp);
}
}
}
void clearEEPROM()
{
for (int i = 0 ; i < EEPROM.length() ; i++) {
if(EEPROM.read(i) != 0) //skip already "empty" addresses
{
EEPROM.write(i, 0); //write 0 to address i
}
}
Serial.println("EEPROM erased");
address = 0; //reset address counter
}