极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10525|回复: 3

这几天雾霾是热门话题 做了个看pm值的东东。很简陋 贴上来求指点

[复制链接]
发表于 2015-3-20 19:02:31 | 显示全部楼层 |阅读模式
本帖最后由 b9ss 于 2015-3-20 19:08 编辑

arduino真的很好玩 ,去年倒腾了下TBJS给的入门教程,大概知道了基本的模块怎么弄。最近倒腾了个3dprint,准备自己打印结构件玩四轴飞行打望器。这下发现知识不够了还得多实践,于是翻出上次买的各种物件,买了个夏普的灰尘传感器GP2Y10 东拼西凑出来个成品 也不知道准不准,发上来求指点。
纸板裸奔封装

测了几个地方的值 今天环保局给的pm2.5是31美领馆是42发现他们的pm10值差不多是2.5值乘个2,难道有相关关系?
阳台

客厅

卧室开了空气净化器

楼层比较高 去大街上测量的话应该会高不少


贴上代码   lcd会闪,可能自己1602没用对

  1. ///*======== lcd1602=====*/
  2. #include <LiquidCrystal.h>
  3. LiquidCrystal lcd(12, 11, 10,9, 8, 7, 6);//设置接口

  4. //dust sensor
  5. int sampool_length=10;//样本数量
  6. int sampool[10] = {0,0,0,0,0,0,0,0,0,0};//样本容器

  7. int dustPin=1;//模拟取样针脚
  8. int sampleAnalog=0;//当前样本
  9. int sampleAvg=0;//样本均值
  10. int poolCount=0;//计数器 用于计算

  11. float particle=0;//颗粒数量
  12. float pm=0;//pm值

  13. int ledPower=2;//给led提供电位的数字输出针脚
  14. int delayTime=280;//单位Microsecond
  15. int delayTime2=40;
  16. float offTime=9680;

  17. float sampleVolt;
  18. void setup(){
  19.     Serial.begin(9600);
  20.     lcd.clear();
  21.     lcd.setCursor(0,0);
  22.     lcd.print("Local PM2.5");
  23.     lcd.begin(16, 2);
  24.     pinMode(ledPower,OUTPUT);
  25. }

  26. void loop(){
  27.   poolCount++;
  28.   if(poolCount>=5){
  29.     sampleAvg=getSampleAvg(sampool_length,sampool);
  30.     PM();
  31.     show1602();
  32.     poolCount=0;
  33.   }
  34.   
  35.   digitalWrite(ledPower,LOW);
  36.   delayMicroseconds(delayTime);
  37.   sampleAnalog=analogRead(dustPin);
  38.   delayMicroseconds(delayTime2);
  39.   digitalWrite(ledPower,HIGH);
  40.   delayMicroseconds(offTime);
  41.   
  42.   delay(200);
  43.   //Serial.print("Sample: ");
  44.   //Serial.println(sampleAnalog);  
  45.   inQueue(sampool_length,sampool,sampleAnalog);
  46. }
  47. void PM(){
  48.   sampleVolt=float(sampleAvg*5)/1024;
  49.    Serial.print("Sample Avg: ");
  50.   Serial.println(sampleAvg);
  51.   Serial.print("Sample Voltage: ");
  52.   Serial.println(sampleVolt);
  53.   
  54.   
  55.   particle=(sampleVolt-0.0356)*120000;//(sampleVolt)*120000;
  56.   Serial.print("Particle: ");
  57.   Serial.println(particle);//实际的颗粒数

  58.   
  59.   Serial.print("PM2.5==");
  60.   if(sampleAnalog>118)
  61.   {
  62.     pm=(sampleVolt*0.172-0.0999)*1000;
  63.     Serial.println(pm);//实际的颗粒数
  64.   }
  65.   else
  66.   {
  67.     Serial.println("unknow");
  68.   }
  69. }
  70. //显示到1602
  71. void show1602(){
  72.   lcd.setCursor(0,0);
  73.     lcd.print("Local PM2.5");//其实是PM值

  74.     lcd.setCursor(5, 2) ;

  75.     delay(1500);

  76. }

  77. //计算样本均值
  78. int getSampleAvg(int _length,int* _array){
  79.   int i=0;
  80.   int avg=0;
  81.   int count=0;
  82.   for(;i<_length;i++){
  83.     if(_array[i]>0){
  84.       avg+=_array[i];
  85.       count++;
  86.     }
  87.   }
  88.   if(count>0){
  89.     avg=avg/count;
  90.   }
  91.   return avg;
  92. }

  93. //简陋的fifo 节约内存
  94. int inQueue(int _length,int* _array,int _value){
  95.   int temp;
  96.   int i;
  97.   if(_length<1){
  98.     return _value;
  99.   }
  100.   temp=_array[_length-1];
  101.   _array[_length-1]=_value;
  102.   i=_length-1;
  103.   for(;i>=1;i--){
  104.     _value=_array[i-1];
  105.     _array[i-1]=temp;
  106.     temp=_value;
  107.   }
  108.   return temp;
  109. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-3-21 13:17:49 | 显示全部楼层
通俗易懂,好贴!
回复 支持 反对

使用道具 举报

发表于 2015-3-21 23:27:52 | 显示全部楼层
撸主3d打印机怎么搞的?来个教程呗
回复 支持 反对

使用道具 举报

发表于 2015-3-23 15:32:29 | 显示全部楼层
顶贴,收藏,

谢谢楼主的分享,有空也来试一这么实有且有意义的 应用!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 17:24 , Processed in 0.062108 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表