PM检测
最近都是大神各种发帖,搞得我这种小白都不好意思了,发个low点的,也希望大神指点指点。:lol:lol:lol设计目标:检测PM2.5,并将数据结果打印到LCD12864屏幕和PC串口上。同时当PM2.5大于40ug/m3时开始闪烁LED灯珠报警。
设备清单:
[*]Arduino Mega 2560 控制点此购买
[*]Mega sensor shield v2.4 点此购买
[*]LCD12864 Shield V1.0 点此购买
[*]Digital Plranha Green Led Module V2
[*]PM2.5激光粉尘传感器 点此购买
接线说明:
PM2.5传感器,Arduino的TX/RX接传感器的RX/TX(注意TX和RX要交叉接线,TX–RX,RX–TX),LED接到数字口20上。
完整代码
#include <U8glib.h>
#include <Arduino.h>
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
unsigned char buf;
//创建lcd对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);
//声明LED灯珠使用的是数字口20
int led = 20;
//初始化变量,用于存储PM1.0, PM2.0, PM10的读书
int PM0_1Value=0;
int PM2_5Value=0;
int PM10Value=0;
//检查送串口获取到的读书
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf;
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf<<8)+thebuf))//check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM0_1(unsigned char *thebuf)
{
int PM0_1Val;
PM0_1Val=((thebuf<<8) + thebuf); //count PM1.0 value of the air detector module
return PM0_1Val;
}
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf<<8) + thebuf);//count PM2.5 value of the air detector module
return PM2_5Val;
}
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf<<8) + thebuf); //count PM10 value of the air detector module
return PM10Val;
}
void drawTitle(){
u8g.setFont(u8g_font_unifont);
u8g.drawStr(0, 18, "PM1.0: ");
u8g.drawStr(0, 36, "PM2.5: ");
u8g.drawStr(0, 54, "PM1 0: ");
u8g.drawStr(80, 18, "ug/m3");
u8g.drawStr(80, 36, "ug/m3");
u8g.drawStr(80, 54, "ug/m3");
}
void lightAlarm(){
digitalWrite(led, HIGH); //Turn on led
delay(100);
digitalWrite(led, LOW); //Turn off led
delay(100);
}
void drawData(){
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(45,18);
u8g.print(PM0_1Value);
u8g.setPrintPos(45,36);
u8g.print(PM2_5Value);
u8g.setPrintPos(45,54);
u8g.print(PM10Value);
}
void drawScreen(){
drawTitle();
drawData();
}
void setup() {
// put your setup code here, to run once:
u8g.setRot180();// rotate screen
Serial.begin(9600);
pinMode(led, OUTPUT); //Set Pin20 as output
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.find(0x42)){ //检测到0x42时,开始读取
Serial.readBytes(buf,LENG);
if(buf == 0x4d){
if(checkValue(buf,LENG)){
PM0_1Value=transmitPM0_1(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM0_1Value);
Serial.println("ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println("ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println("ug/m3");
Serial.println();
}
u8g.firstPage();
drawScreen();
do {
drawScreen();
} while( u8g.nextPage() );
delay(500);
if(PM2_5Value>40){
lightAlarm();
}
delay(500);
}
感谢分享,学习了 顶起来 代码写的好工整~ luna 发表于 2016-12-22 17:47
代码写的好工整~
嘿嘿,说的我都不好意思了 dsweiliang 发表于 2016-12-22 04:04
感谢分享,学习了
一起学咯,这里好多好多可以学的。 实用,好课题 好高端!!!!!
页:
[1]