4315浏览
查看: 4315|回复: 2

[已解决] 使用的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

看不懂,等大神来解~~~~
回复

使用道具 举报

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解答,感谢
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail