So, i have connected to my arduino uno, a carbon monoxide sensor, an lcd screen, an active buzer, an rgb led and a push button with a pull-down. in my code, when the sensor detects more than 10 ppm, as long as there is more than 10 ppm, it enters a loop which makes the buzer sound. However, when it enters this loop, it doesn't exit even if the value is less than the condition ... Here is my code:
float RS_gas;
float ratio;
float sensorValue;
float sensor_volt;
float R0 = 37384;
int repeat = 0;
int heattime = 0;
//pin on the Arduino
int red = 9;
int green = 10;
int blue = 11;
int heatOFF = 8;
int bp = 12;
//------------------
#include <LiquidCrystal.h>
const int rs = 2, en = 13, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.print("Carbon Monoxide");
lcd.setCursor(0, 2);
lcd.print("Detector");
delay(2500);
lcd.clear();
lcd.setCursor(1, 1);
Serial.begin(9600);
pinMode(heatOFF, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(bp, INPUT);
tone(3, 550, 100);
delay(200);
tone(3, 550, 100);
}
void loop()
{
while (heattime <= 6000 && digitalRead(12) == LOW)
{
lcd.clear();
lcd.print("Heating...");
digitalWrite(heatOFF, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
heattime += 1;
delay(10);
}
if (digitalRead(bp) == HIGH)
{
lcd.clear();
lcd.print("Bypass heating");
delay(1000);
lcd.clear();
}
heattime = 0;
digitalWrite(heatOFF, HIGH);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
lcd.clear();
lcd.print("Reading...");
delay(1500);
tone(3, 400, 100);
while (repeat <= 180)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0; //Replace R0 with the value found using the sketch above
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
while (ppm >= 10 && ppm < 25)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0;
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
digitalWrite(red, HIGH);
analogWrite(green, 100);
tone(3, 600, 100);
delay(300);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
delay(300);
}
while (ppm >= 25 && ppm < 50)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0;
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
digitalWrite(red, HIGH);
analogWrite(green, 30);
tone(3, 600, 100);
delay(200);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
delay(200);
}
while (ppm >= 50 && ppm < 90)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0;
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
digitalWrite(red, HIGH);
analogWrite(green, 30);
tone(3, 600, 100);
delay(100);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
delay(100);
}
while (ppm >= 90 && ppm < 1000)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0;
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
tone(3, 800, 100);
delay(100);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
delay(100);
}
while (ppm >= 1000)
{
lcd.clear();
sensorValue = analogRead(A0);
sensor_volt = sensorValue / 1024 * 5.0;
RS_gas = (5.0 - sensor_volt) / sensor_volt;
ratio = RS_gas / R0;
float x = 1538.46 * ratio;
float ppm = pow(x, -1.709);
lcd.print("PPM: ");
lcd.print(ppm);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
tone(3, 600, 100);
delay(100);
tone(3, 800, 100);
analogWrite(green, 85);
delay(100);
}
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
repeat += 1;
delay(500);
}
lcd.clear();
repeat = 0;
}
and here is the schematics :