主页 > 硬件 > 读取空气检测器程序

读取空气检测器程序

2014年5月29日 读取空气检测器程序无评论 阅读: 10,750 次
读取数据,直接输出到串口:
int dustPin=0;
float dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(dustPin, INPUT);
}
void loop(){
// ledPower is any digital pin on the arduino connected to Pin 3 on the sensor
digitalWrite(ledPower,LOW);
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);
delayMicroseconds(offTime);
delay(1000);
if (dustVal>36.455)
Serial.println((float(dustVal/1024)-0.0356)*120000*0.035);
}
如果arduino 和树莓派通讯进行信息上传,程序如下:

#include <OneWire.h>
#include <Wire.h>

int dustPin=0;
int dustVal=0;
String dust=”";
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
void setup(){
Wire.begin(5);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
}

void loop(){
getDust();
}
void requestEvent()
{
char charBuf[dust.length()+1];
dust.toCharArray(charBuf, dust.length()+1);
Wire.write(charBuf);
}
void receiveEvent(int howMany)
{
while (1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.print(x); // print the integer
}
void getDust(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin);
if(dustVal<10){
dust=”000″+String(dustVal);
}else if(dustVal<100){
dust=”00″+String(dustVal);
}else if(dustVal<1000){
dust=”0″+String(dustVal);
}else{
dust=String(dustVal);
}
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);

delay(3000);
Serial.println(dust);

}

需要先下载OneWire

发表评论

新用户的评论需审核后才会显示;

电子邮件地址不会被公开;
必填项已用*标注