i am using a MQ-135 gas sensor and 20x4 LCD screen and it shows different values on the serial monitor it shows 756 PPM and on the LCD screen it is stuck below 320PPM the text BAD is also stuck on the screen and doesn't update on the lowering of PPM value of CO2
this is my code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define sensor A0
int gas, co2lvl;
#define updateDisplay();
void setup()
{
pinMode(sensor,INPUT);
Serial.begin(9600);
lcd.begin(20,4);
lcd.clear();
}
void loop()
{
co2lvl=gas-255;
co2lvl=map(co2lvl, 0,1024,400,5000);
Serial.print(co2lvl);
Serial.print("ppm");
Serial.println();
delay(200);
if(co2lvl>=699){
updateDisplay();
lcd.setCursor(0,0);
lcd.print("CO2 IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
lcd.setCursor(6,1);
lcd.print("EXCELLENT");
}
else if(co2lvl<=700){
updateDisplay();
lcd.setCursor(0,0);
lcd.print("CO2 PRESENT IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
lcd.setCursor(4,1);
lcd.print("GOOD");}
else if(co2lvl<=900){
updateDisplay();
lcd.setCursor(0,0);
lcd.print("CO2 PRESENT IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
lcd.setCursor(5,1);
lcd.print("FAIR");}
else if(co2lvl<=1100){
updateDisplay();
lcd.setCursor(0,0);
lcd.print("CO2 PRESENT IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
lcd.setCursor(5,1);
lcd.print("MEDIOCRE");
}
else(co2lvl<=1600);{
updateDisplay();
lcd.setCursor(0,0);
lcd.print("CO2 PRESENT IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
lcd.setCursor(5,1);
lcd.print("BAD");}
}
1 Answer 1
You made a mistake in the line:
else(co2lvl<=1600);
Because of the semicolon (;) it will not do anything for that condition. But instead the following lines will be executed always.
The following is a slight refactoring making your code more clear, removing also duplicated code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define sensor A0
int gas, co2lvl;
void setup()
{
pinMode(sensor,INPUT);
Serial.begin(9600);
lcd.begin(20,4);
lcd.clear();
}
void loop()
{
co2lvl=gas-255;
co2lvl=map(co2lvl, 0,1024,400,5000);
Serial.print(co2lvl);
Serial.print("ppm");
Serial.println();
delay(200);
lcd.setCursor(0,0);
lcd.print("CO2 PRESENT IN THE AIR");
lcd.setCursor(0,1);
lcd.print(analogRead(co2lvl));
if (co2lvl <= 300) { show(6, "EXCELLENT"); }
else if (co2lvl <= 700) { show(4, "GOOD" ); }
else if (co2lvl <= 900) { show(5, "FAIR" ); }
else if (co2lvl <= 1100) { show(5, "MEDIOCRE" ); }
else if (co2lvl <= 1600) { show(5, "BAD" ); }
}
void show(int position, char* text)
{
lcd.setCursor(position, 1);
lcd.print(text);
}
-
You are welcome... if it helped you please upvote the answer, and if the answer is correct to solve your solution, accept the answer (by clicking the checkmark next to the answer). Good luck with your project.Michel Keijzers– Michel Keijzers12/09/2020 13:59:13Commented Dec 9, 2020 at 13:59
CO2 level in the air
to get better fit on the display ...CO2 level
is probably enough