trying to complie two sensors in one source code for the lcd display , and the value for the Co2 is not accurate.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // Or whatever your's is
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 2; //Connect led driver pins of dust sensor to Arduino D2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
#define pwmPin A1
float Co2= 0;
int prevVal = LOW;
long th, tl, h, l, ppm;
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(pwmPin, INPUT);
Serial.print("****************** keyestudio ******************\n");
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Raw Value:");
lcd.backlight ();
lcd.setCursor(0,1);
lcd.print("C02:");
pinMode(ledPower,OUTPUT);
}
void loop(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (5.0 / 1024.0);
dustDensity = 0.17 * calcVoltage - 0.1;
lcd.backlight();
lcd.setCursor(10,0);
lcd.print(voMeasured);
lcd.setCursor(13,2);
lcd.print(dustDensity);
delay(1000);
Co2 = analogRead(measurePin);
long tt = millis();
int myVal = digitalRead(pwmPin);
if (myVal == HIGH) {
if (myVal != prevVal) {
h = tt;
tl = h - l;
prevVal = myVal;
}
} else {
if (myVal != prevVal) {
l = tt;
th = l - h;
prevVal = myVal;
ppm = 5000 * (th - 2) / (th + tl - 4);
Serial.println("PPM = " + String(ppm));
}
}
lcd.backlight();
lcd.setCursor(5,1);
lcd.print(Co2);
delay(1000);
}//************************************************************