Hello! Im doing a school project- CO2 detector. It works good but i can't get the buzzer do what i want. I want it to beep for short time (10 seconds for example) if 150ppm is exceeded and if the value is under 150ppm then no beeping. Here is the code
/*
* Displays text sent over the serial port (e.g. from the Serial Monitor) on
* an attached LCD.
* YWROBOT
*Compatible with the Arduino IDE 1.0
*Library version:1.1
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int buzzer=12;
void setup()
{
pinMode(buzzer, OUTPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CO2=");
Serial.print("CO2=");
lcd.print (sensorValue, DEC);
Serial.print (sensorValue, DEC);
lcd.println(" PPM ");
Serial.println(" PPM ");
lcd.setCursor(0, 1);
if (sensorValue < 100)
{
lcd.print("Good");
Serial.print("Good ");
}
if (sensorValue >100 && sensorValue <150)
{
lcd.print("mediocre");
Serial.print("mediocre ");
}
if (sensorValue > 150)
{
lcd.print("BAD");
Serial.print("BAD");
}
delay(1200);
if (sensorValue > 100)
{
tone(buzzer, 1000, 500); // tone() is the main function to use with a buzzer, it takes 2 or 3 parameteres (buzzer pin, sound frequency, duration)
delay(1000);
tone(buzzer, 2000, 500); // You can also use noTone() to stop the sound it takes 1 parametere which is the buzzer pin
delay(1000);
noTone(buzzer);
}
else
{
noTone(buzzer);
}
{
}
}
I hope this would do the job. There is 16x2 also I2C display. Its a 3 pin active buzzer. I can do real photos but the cable management is terrible and nothing would be vissible.
It is a buzzer with internal oscillation, the third pin set to zero makes it sound. @vvventsi, why don't you have the third pin connected to pin 12 in your sketch?
This one is working kinda okay. It doesn't beep when the value is under 100 and otherwise works good. I want to edit it so i that it beeps only above 150 so i deleted the 3 lines for the buzzer on if(sensorValue > 100 && sensorValue < 150) but then the buzzer beeps constantly even tho that it should't beep at all at that moment. Any ideas?`
}
if (sensorValue > 100 && sensorValue < 150) {
lcd.print("mediocre");
Serial.print("mediocre ");
digitalWrite(buzzer, LOW); */When i delete this and the 2 lines below this it beeps constantly instead of not beeping at all /*
delay (250);
digitalWrite(buzzer, HIGH);
}
if (sensorValue > 150) {
lcd.print("BAD");
Serial.print("BAD");
digitalWrite(buzzer, LOW);
delay (1000);
digitalWrite(buzzer, HIGH);
}
delay(750);
}
I'm sure i haven't done anything else, i've made multiple attempts. It's really strange. I'm not the best at coding but I've learned how to find my mistakes and etc and as you said, it should work correctly by removing the 3 lines but it does not and there isn't any sence why it isn't. I'll bring the project to my IT teacher tomorrow and ask for advice but in theory it should work
Depends but mostly around 130PPM. When i remove the lines for the 100-150ppm it beeps constantly and when i put them back it beeps with the set periods. It doesn't beep when it's under 100ppm