Hi I was just got my DHT 11 sensor and wants to make project with arduino, DHT11, buzzer, LCD and RGB led.
I've write this code : -
// including the LCD library
#include <LiquidCrystal.h>
// including the DHT library
#include "DHT.h"
// Naming the componets and defining there pins on arduino
#define buzzer 13
#define red A3
#define green A4
#define blue A5
#define on 6
#define of 7
// initializing the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// initializing the DHT11 library with the various arguments
#define DHTPIN 8 // digital pin is connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
long t = 240000;
boolean x = false;
void setup() {
// seting up the DHT sensor
dht.begin();
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
// seting up the LCD's number of columns and rows
lcd.begin(16, 2);
// Printing the welcome message to the LCD
lcd.print("hello, world!");
// telling the work of its connected components
pinMode(buzzer, OUTPUT);
pinMode (red, OUTPUT);
pinMode (green, OUTPUT);
pinMode (blue, OUTPUT);
pinMode (on , INPUT);
pinMode (of , INPUT);
tone(buzzer, 80);
tone(buzzer, 300);
tone(buzzer, 500);
tone(buzzer, 800);
tone(buzzer, 1000);
tone(buzzer, 100);
tone(buzzer, 50);
digitalWrite (buzzer, LOW);
lcd.clear();
}
void loop() {
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 0);
if (digitalRead(on) == HIGH) {
x == true;
}
if (digitalRead(of) == HIGH) {
x == false;
}
if (x == true)
{
// Reading temperature or humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
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!");
lcd.println("Failed to read from DHT sensor!");
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 0);
tone(buzzer, 80);
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
lcd.print("Humidity: ");
lcd.print(h);
lcd.println(" %");
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
lcd.print("Temperature: ");
lcd.print(t);
lcd.print(" ^C");
Serial.print(" ^C ");
Serial.print(" %\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" ^C ");
lcd.print("Heat index: ");
lcd.print(hic);
lcd.print(" ^C ");
if (t <= 10) {
analogWrite(red, 255);
analogWrite(green, 255);
analogWrite(blue, 255);
}
else if ((t > 10) && (t < 18)) {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 255);
}
else if ((t > 18) && (t < 22)) {
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 255);
}
else if ((t > 22) && (t < 38)) {
analogWrite(red, 255);
analogWrite(green, 255);
analogWrite(blue, 0);
}
else {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
tone(buzzer, 90);
delay(5000);
}
digitalWrite (buzzer, LOW);
delay(t);
}
else {
lcd.print("Please Power off");
lcd.print(" if I'm done ");
}
}
Please tell me will it'll work?