deepidea 发表于 2016-3-27 19:53:04

使用的gp2y1010f,。如果不接1602在端口监视器能正确看到数值...

使用的gp2y1010f,全是DF的套件。如果不接LCD在端口监视器能正确看到数值,但是一接上1602就不行了。一直是-0.1.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int measurePin = 0;// 连接模拟口0
int ledPower = 2;    // 连接数字口2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
LiquidCrystal_I2C lcd(0x20,16,2); //设置LCD的地址为0x20,每行16个字符,共2行
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
lcd.init();
lcd.backlight();
}

void loop(){
digitalWrite(ledPower,LOW);       //开启内部LED
delayMicroseconds(samplingTime);// 开启LED后的280us的等待时间
voMeasured = analogRead(measurePin);   // 读取模拟值
delayMicroseconds(deltaTime);      //40us等待时间
digitalWrite(ledPower,HIGH);         // 关闭LED
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.home();
lcd.print("DustDen:"); //输出粉尘数值
lcd.print(dustDensity); // 输出单位: 毫克/立方米
delay(1000);
}

凌风清羽 发表于 2016-3-27 20:31:40

看不懂,等大神来解~~~~{:5_171:}

deepidea 发表于 2016-3-28 15:13:25

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2); //设置LCD的地址为0x20,每行16个字符,共2行
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
   lcd.init();
lcd.backlight();
}

void loop(){
digitalWrite(2,LOW); // power on the LED
delayMicroseconds(samplingTime);

voMeasured = analogRead(1); // read the dust value

delayMicroseconds(deltaTime);
digitalWrite(2,HIGH); // turn the LED off
delayMicroseconds(sleepTime);

// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (5.0 / 1024.0);

// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = 0.17 * calcVoltage - 0.1;

Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);

Serial.print(" - Voltage: ");
Serial.print(calcVoltage);

Serial.print(" - Dust Density: ");
Serial.println(dustDensity); // unit: mg/m3
lcd.home();
lcd.print("DustDen:"); //输出粉尘数值
lcd.print(dustDensity); // 输出单位: 毫克/立方米
delay(1000);
}
以上是正确代码,DF技术Cain解答,感谢
页: [1]
查看完整版本: 使用的gp2y1010f,。如果不接1602在端口监视器能正确看到数值...