1

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

enter image description here

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");}
}
Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Dec 9, 2020 at 11:17
1
  • use CO2 level in the air to get better fit on the display ... CO2 level is probably enough Commented Dec 9, 2020 at 18:09

1 Answer 1

0

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);
}
answered Dec 9, 2020 at 11:36
1
  • 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. Commented Dec 9, 2020 at 13:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.