![]()
const int pingPin = 7;
int LEDRED = 9;
int LEDGREEN = 10;
int LEDBLUE = 11;
const int buzzer = 5;
#include <dht.h>
dht DHT;
#define DHT11_PIN 2
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C LCD(0x27,16,2);
void setup() {
Serial.begin(9600);
LCD.init();
LCD.backlight();
pinMode(buzzer, OUTPUT);
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
pinMode(LEDRED, OUTPUT);
pinMode(LEDGREEN, OUTPUT);
pinMode(LEDBLUE, OUTPUT);
int chk = DHT.read11(DHT11_PIN);
if (cm < 20) {
//LCD.clear();
digitalWrite(LEDRED, LOW);
digitalWrite(LEDBLUE, LOW);
digitalWrite(LEDGREEN, HIGH);
LCD.setCursor(0, 0);
LCD.print("O ");
LCD.setCursor(0, 1);
LCD.print("O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print(" O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("A O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("An O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Ana O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Anal O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analy O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analys O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysi O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysin O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O ");
LCD.setCursor(0, 1);
LCD.print(" O ");
delay(100);
LCD.setCursor(0, 0);
LCD.print("Analysing O");
LCD.setCursor(0, 1);
LCD.print(" O");
delay(100);
tone(buzzer, 1174);
delay(200);
tone(buzzer, 1396);
delay(200);
tone(buzzer, 1318);
delay(200);
noTone(buzzer);
LCD.setCursor(0, 0);
LCD.print("Temp: ");
LCD.print(DHT.temperature);
LCD.print((char)223);
LCD.print("C ");
LCD.setCursor(0, 1);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
LCD.print("Humi: ");
LCD.print(DHT.humidity);
LCD.print("% ");
delay(10000);
}
else if (cm >= 20 && cm < 50) {
//LCD.clear();
digitalWrite(LEDRED, LOW);
digitalWrite(LEDBLUE, HIGH);
digitalWrite(LEDGREEN, LOW);
tone(buzzer, 440);
delay(200);
tone(buzzer, 493);
delay(200);
noTone(buzzer);
LCD.setCursor(0, 1);
LCD.print("come closer :) ");
}
else if (cm >= 50) {
LCD.clear();
digitalWrite(LEDRED, HIGH);
digitalWrite(LEDBLUE, LOW);
digitalWrite(LEDGREEN, LOW);
LCD.setCursor(0, 1);
LCD.print("where are you ? ");
}
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}