- Laser 미세먼지 센서 -
//******************************
//*Abstract: Read value of PM1,PM2.5 and PM10 of air quality
//*Product Link: http://www.dfrobot.com.cn/goods-1113.html
//
//*Version:V3.1
//*Author:Zuyang @ HUST
//*Modified by Cain for Arduino Hardware Serial port compatibility
//*Date:March.25.2016
//******************************
#include <Arduino.h>
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
#include <Servo.h>
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
Servo servo1; // pm 1.0 미세먼지 지시
Servo servo2; // pm 2.5 미세먼지 지시
Servo servo3; // pm 10 미세먼지 지시
void setup()
{
Serial.begin(9600); //使用串口0
Serial.setTimeout(1500); //设置超时时间为1500毫秒(大于传感器传送数据周期1秒)
servo1.attach(2); // pin 2 pm 1.0 servo pin
servo2.attach(3); // pin 3 pm 2.5 servo pin
servo3.attach(4); // pin 4 pm 10 servo pin
servo1.write(0); // 미세먼지 pm 1.0 0 에서 시작
servo2.write(0); // 미세먼지 pm 2.5 0 에서 시작
servo3.write(0); // 미세먼지 pm 10 0 에서 시작
delay(10);
}
void loop()
{
if(Serial.find(0x42)){ //检测到0x42时,开始读取
Serial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(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(PM01Value);
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();
/*
int angle1 = map(PM01Value,0,500,0,160);
servo1.write(angle1);
delay(1000);
int angle2 = map(PM2_5Value,0,500,0,160);
servo2.write(angle2);
delay(3000);
int angle3 = map(PM10Value,0,500,0,160);
servo3.write(angle3);
delay(20);
servo1.write(0);
delay(500);
servo2.write(0);
delay(500);
Serial.println(angle1);
Serial.println(angle2);
Serial.print(angle3);
delay(2000);
*/
}
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}