是不是时间上有问题~
[mw_shl_code=cpp,true]
#include <LiquidCrystal.h>
#include <dht.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
dht DHT;
#define DHT11_PIN 6
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup()
{
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(8,INPUT);
starttime = millis();
}
void loop() {
int chk = DHT.read11(DHT11_PIN);
// set the cursor to column 0, line 1
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print( DHT.temperature,1 );
lcd.print ("C");
lcd.setCursor(9, 0);
lcd.print("H:");
lcd.print(DHT.humidity,1);
lcd.print("%");
//
lcd.setCursor(0, 1);
lcd.print("PM2.5:");
lcd.setCursor(13, 1);
lcd.print ("pcs");
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
//Serial.print(lowpulseoccupancy);
//Serial.print(",");
// Serial.print(ratio);
// Serial.print(",");
lcd.setCursor(6, 1);
lcd.print(concentration);
Serial.print("h1:");
Serial.print(DHT.humidity,1);
Serial.print(";");
Serial.print("t1:");
Serial.print(DHT.temperature,1);
Serial.print(";");
Serial.print("c1:");
Serial.print(concentration);
Serial.println(";");
lowpulseoccupancy = 0;
starttime = millis();
}
delay(1000);
}
[/mw_shl_code]
|