I'm using an Arduino Uno and an MQ-135 to detect ethanol and send readings on an LCD screen.
Everything works fine, until the reading goes above 100 and then dips back below 100 : the LCD adds a power of 10 to the readings from that point on for all values with 2 digits, for example : 90 on the serial monitor becomes 900. Readings above are correct (for example, 110 on the serial monitor becomes 110). The error only applies to the 2 digit values.
Here is my code :
#include <LiquidCrystal.h>
const int rs= 12 , en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int buz=8;
int led=13;
int aqsensor=A0;
int threshold = 150;
void setup() {
// put your setup code here, to run once:
pinMode (buz,OUTPUT);
pinMode (led,OUTPUT);
Serial.begin(9600);
lcd.clear();
lcd.begin(16,2);
}
void loop()
{
// put your main code here, to run repeatedly:
int AQ= analogRead(aqsensor);
Serial.print("Air Quality: ");
Serial.println(AQ);
lcd.setCursor(0,0);
lcd.print("Air Quality: ");
lcd.print(AQ, DEC);
if (AQ > threshold)
{
digitalWrite(buz,HIGH);
digitalWrite(led,HIGH);
tone(buz,1000,200);
lcd.setCursor(1,1);
lcd.print("AQ Level High");
}
else
{
digitalWrite(led,LOW);
digitalWrite(buz,LOW);
lcd.setCursor(1,1);
lcd.print("AQ Level Good");
}
delay(500);
}
Here is the LCD screen I am using. A datasheet is included in the page :
In my experience this is the easiest way to tidy up the code and add the code tags
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
The reason is that the display does not automatically erase the old text, but prints the new one on top. Once you move from three-digit numbers to two-digit numbers, one zero remains on the screen.
To prevent this, you can output two-digit numbers with a trailing space - that is, not "90", but "90 "
lcd.clear() will indeed clear the third digit along with everything else on the screen. This tends to cause horrendous flicker and is best avoided if possible