Hi, I'm trying to set up an Arduino project about Temperature and Humidity sensor which shows up on the LCD. I added a buzzer and LED to it. The buzzer is supposed to turn on when temperature > 25 or humidity > 25.
but I can't seem to get it to work on the buzzer part. Please help me. I need this for a school competition, which is on 1st of March.
#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT11
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
int Distance;
int Duration;
int bz = 8;
int r = 7;
void setup()
{
pinMode(r,OUTPUT);
pinMode(bz,OUTPUT);
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop()
{
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
Serial.print("Temperature :");
Serial.println(temperature);
Serial.print("humidity:");
Serial.println(humidity);
delay(2000);
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.setCursor(12,0);
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print("Humidity:");
lcd.setCursor(9,1);
lcd.print(humidity);
if (temperature>25)
{
digitalWrite(r,HIGH);
tone(8,730,800);
delay(1000);
digitalWrite(r,LOW);
tone(8,730,800);
delay(1000);
digitalWrite(r,HIGH);
tone(8,730,800);
}
if (humidity>25)
{
digitalWrite(r,HIGH);
tone(8,730,800);
delay(1000);
digitalWrite(r,LOW);
tone(8,730,800);
delay(1000);
digitalWrite(r,HIGH);
tone(8,730,800);
}
else
{
digitalWrite(r,LOW);
tone(0,0,0);
}
}**strong text**
and use your variable name for the buzzer pin rather than a bald constant:
tone(bz,730,800);
But neither of those changes will likely fix your problem. Because whatever it is, you have not spelled it out. "I can't seem to get it to work" tells us nothing. You need to tell us exactly what happens. And what you expected to happen that didn't.
So: show us a schematic, show us clear, well lit, in focus pictures of your wiring, and if your wiring is an absolute mess of Dupont cables that no one will be able to make heads or tails of - clean it up beforehand. Tell us what parts you're using. Links to technical data would be a good idea. Show us the output you're getting, both when the buzzer should be on, and when it should be off. You need to step up to the plate and provide comprehensive and accurate information.
And until you do that, not much is going to happen.
Oh, and your deadline is your problem and no one else's. As @dougp has already brought to your attention, you made a mistake by even bringing it up.
What i meant was the buzzer turns but i cant get t to turn off when the humidity or temperature is still under 25. It's only supposed to sound when either of it is above 25.
You need to think about your logic. I think what you want is:
if(temperature < 25 && humidity < 25)
{
// turn off
}
else
{
// turn on
}
That will do what you want, but it probably won't quite give the result you want. If either value is close to 25 it will probably switch on and off quickly. A crude fix would be to put a delay in the loop to limit how often it switches. A more sophisticated approach would be to keep track of the time when it switches state and not switch again for a set amount of time.