#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
// set the LCD address to 0x27 for a 16 chars and 2 line display
// SCL - A5
// SDA - A4
//////////////////// DHT ////////////////////
// Global Variables
int DHT11_Pin = 3; // DHT11 Data Pin
// This pin also has to be connected to VCC trough the 5kΩ resistor
int Humidity = 0;
int Temp = 0;
bool DHTError = false; // Checksum Error
// a Delay routine, call DelayTimer (time in uSec)
void DelayTimer(long int DelayValue){
long int DelayTime = micros();
do {
}while (micros()-DelayTime < DelayValue);
}
// Main DHT Void
void DHT11(){
long int DataTime = 0;
byte Result[45];
byte DataArray = 0;
byte DataCounter = 0;
byte DHTData[4];
bool BlockDHT=false;
// Trigger Sensor (described in the Datasheet)
pinMode(DHT11_Pin,OUTPUT);
digitalWrite(DHT11_Pin,HIGH);
DelayTimer(250000);
digitalWrite(DHT11_Pin,LOW);
DelayTimer(30000);
digitalWrite(DHT11_Pin,HIGH);
DelayTimer(50);
pinMode(DHT11_Pin,INPUT);
// read the Bits and put them into a Result array
do {
if (digitalRead(DHT11_Pin) == 0 && BlockDHT == false) {
BlockDHT = true;
Result[DataArray]=(micros()-DataTime);
DataArray++;DataTime=micros();} //If DHT pin is LOW, go to next Dataset
if (digitalRead(DHT11_Pin) == 1) {
BlockDHT = false;
} // As long as DHT pin is High add time to Result
}while((micros()-DataTime) < 150); // if DTH Sensor high for more than 150 usec, leave loop
for (int i=2; i< DataArray; i++) {
if (Result[i] <= 90) Result[i]=0; else Result[i]=1;
}
for (int j=0; j< 5; j++){ // redo it for the 5 Bytes (40 Databits /8 = 5)
for (int i=0; i< 8; i++) {
bitWrite(DHTData[j], 7-i, Result[i+2+(j*8)]);
} // Create 5 Databytes from the 40 Databits
}
// check checksum
if (DHTData[4] == (DHTData[0] + DHTData[1] + DHTData[2] + DHTData[3])){
Humidity = DHTData[0];
Temp = DHTData[2]; // we are not using DHTData[4], because it's check sum
DHTError=false;
} else
DHTError=true;
}
void DHTPrint(void){
DHT11();
lcd.setCursor(0, 0); //1st row
if (DHTError == false || Humidity != NULL || Temp != NULL){
lcd.print("Hum=");
lcd.print(Humidity);
lcd.print("% ");
lcd.print("Temp=");
lcd.print(Temp);
//lcd.print("°C");
lcd.print("C");
} else {
lcd.clear();
lcd.print("Error");
}
}
////////////// PIR //////////////
int ledPinR = 5; // red LED
int ledPinG = 4; // green LED
int PirPin = 2; // PIR sensor
int PirState = LOW; // sensor state
int v = LOW;
void PIR(void){
v = digitalRead(PirPin);
if (v == HIGH){
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, HIGH);
if (PirState == HIGH){
lcd.clear();
lcd.setCursor(0, 1); // 2nd row
lcd.print("Motion detected!");
PirState = LOW;
}
} else {
digitalWrite(ledPinG, LOW);
digitalWrite(ledPinR, HIGH);
if (PirState == LOW){
lcd.clear();
lcd.setCursor(3, 1); // 2nd row
lcd.print("No motion");
PirState = HIGH;
}
}
}
////////////// LOOPS //////////////
void setup() {
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(PirPin, INPUT);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW);
Serial.begin(9600);
//lcd.begin(16, 2);
lcd.init();
lcd.backlight();
}
void loop() {
//lcd.clear();
DHTPrint();
PIR();
delay(1000);
}
// made by pio200 & MM