本帖最后由 b9ss 于 2015-3-20 19:08 编辑
arduino真的很好玩 ,去年倒腾了下TBJS给的入门教程,大概知道了基本的模块怎么弄。最近倒腾了个3dprint,准备自己打印结构件玩四轴飞行打望器。这下发现知识不够了还得多实践,于是翻出上次买的各种物件,买了个夏普的灰尘传感器GP2Y10 东拼西凑出来个成品 也不知道准不准,发上来求指点。
纸板裸奔封装
测了几个地方的值 今天环保局给的pm2.5是31美领馆是42发现他们的pm10值差不多是2.5值乘个2,难道有相关关系?
阳台
客厅
卧室开了空气净化器
楼层比较高 去大街上测量的话应该会高不少
贴上代码 lcd会闪,可能自己1602没用对
- ///*======== lcd1602=====*/
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 10,9, 8, 7, 6);//设置接口
- //dust sensor
- int sampool_length=10;//样本数量
- int sampool[10] = {0,0,0,0,0,0,0,0,0,0};//样本容器
- int dustPin=1;//模拟取样针脚
- int sampleAnalog=0;//当前样本
- int sampleAvg=0;//样本均值
- int poolCount=0;//计数器 用于计算
- float particle=0;//颗粒数量
- float pm=0;//pm值
- int ledPower=2;//给led提供电位的数字输出针脚
- int delayTime=280;//单位Microsecond
- int delayTime2=40;
- float offTime=9680;
- float sampleVolt;
- void setup(){
- Serial.begin(9600);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Local PM2.5");
- lcd.begin(16, 2);
- pinMode(ledPower,OUTPUT);
- }
- void loop(){
- poolCount++;
- if(poolCount>=5){
- sampleAvg=getSampleAvg(sampool_length,sampool);
- PM();
- show1602();
- poolCount=0;
- }
-
- digitalWrite(ledPower,LOW);
- delayMicroseconds(delayTime);
- sampleAnalog=analogRead(dustPin);
- delayMicroseconds(delayTime2);
- digitalWrite(ledPower,HIGH);
- delayMicroseconds(offTime);
-
- delay(200);
- //Serial.print("Sample: ");
- //Serial.println(sampleAnalog);
- inQueue(sampool_length,sampool,sampleAnalog);
- }
- void PM(){
- sampleVolt=float(sampleAvg*5)/1024;
- Serial.print("Sample Avg: ");
- Serial.println(sampleAvg);
- Serial.print("Sample Voltage: ");
- Serial.println(sampleVolt);
-
-
- particle=(sampleVolt-0.0356)*120000;//(sampleVolt)*120000;
- Serial.print("Particle: ");
- Serial.println(particle);//实际的颗粒数
-
-
- Serial.print("PM2.5==");
- if(sampleAnalog>118)
- {
- pm=(sampleVolt*0.172-0.0999)*1000;
- Serial.println(pm);//实际的颗粒数
- }
- else
- {
- Serial.println("unknow");
- }
- }
- //显示到1602
- void show1602(){
- lcd.setCursor(0,0);
- lcd.print("Local PM2.5");//其实是PM值
- lcd.setCursor(5, 2) ;
- delay(1500);
- }
- //计算样本均值
- int getSampleAvg(int _length,int* _array){
- int i=0;
- int avg=0;
- int count=0;
- for(;i<_length;i++){
- if(_array[i]>0){
- avg+=_array[i];
- count++;
- }
- }
- if(count>0){
- avg=avg/count;
- }
- return avg;
- }
- //简陋的fifo 节约内存
- int inQueue(int _length,int* _array,int _value){
- int temp;
- int i;
- if(_length<1){
- return _value;
- }
- temp=_array[_length-1];
- _array[_length-1]=_value;
- i=_length-1;
- for(;i>=1;i--){
- _value=_array[i-1];
- _array[i-1]=temp;
- temp=_value;
- }
- return temp;
- }
复制代码 |