mingxu976 发表于 2017-7-1 22:21:11

sds011粉尘模块if生效求教

程序如下,红字标识部分为判断,
但是奇怪的是三种都输出了,默认刚烧录完Pm2.5应该是0,小屏幕上也是显示Pm2.5为0.判断没作用,求支招

#include "U8glib.h"


unsigned int Pm25 = 0;
unsigned int Pm10 = 0;
//Com: D0..D7: 8,9,10,11,4,5,6,7 cs1=14, cs2=15,di=17,rw=16,reset = 16
U8GLIB_SSD1306_128X64 u8g(13, 12, 11, 10);// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9


void draw() {
// 设置字体
u8g.setFont(u8g_font_7x14);
// 设置文字及其显示位置
u8g.drawStr( 0, 10, "By MS17E2 Martinxu");
u8g.drawStr( 0, 30, "Pm2.5:");
u8g.setPrintPos(47,30);          //设置位置
u8g.print(Pm25);//打印内容
if (Pm<=35)
   {u8g.drawStr( 70, 30, "good");}
if (35<Pm<=75)
   {u8g.drawStr( 70, 30, "moderate");}
if (75<Pm)
   {u8g.drawStr( 70, 30, "bad");}
   u8g.drawStr( 0, 40, "Pm10:");
   u8g.setPrintPos(43,40);          //设置位置
u8g.print(Pm10);      //打印内容
}


void ProcessSerialData()
{
uint8_t mData = 0;
uint8_t i = 0;
uint8_t mPkt = {0};
uint8_t mCheck = 0;
while (Serial.available() > 0)
{
    // from www. inovafitness. com
    // packet format: AA C0 PM25_Low PM25_High PM10_Low PM10_High 0 0 CRC AB
   mData = Serial.read();   delay(2);//wait until packet is received
    if(mData == 0xAA)//head1 ok
   {
      mPkt =mData;
      mData = Serial.read();
      if(mData == 0xc0)//head2 ok
      {
          mPkt =mData;
          mCheck = 0;
          for(i=0;i < 6;i++)//data recv and crc calc
          {
             mPkt = Serial.read();
             delay(2);
             mCheck += mPkt;
          }
          mPkt = Serial.read();
          delay(1);
          mPkt = Serial.read();
          if(mCheck == mPkt)//crc ok
          {
            Serial.flush();

            Pm25 = (uint16_t)mPkt | (uint16_t)(mPkt<<8);
            Pm10 = (uint16_t)mPkt | (uint16_t)(mPkt<<8);
            if(Pm25 > 9999)
             Pm25 = 9999;
            if(Pm10 > 9999)
             Pm10 = 9999;            
            //get one good packet
             return;
          }
      }
   }
}
}




void setup() {

    Serial.begin(9600);
    Pm25=0;
    Pm10=0;
   
}
// 主循环
void loop() {
ProcessSerialData();
    // u8glib图片循环结构:
u8g.firstPage();
do {
    draw();
}
while( u8g.nextPage() );


delay(1000);

}

Paderboy 发表于 2017-7-2 08:41:42

if (Pm<=35)
   {u8g.drawStr( 70, 30, "good");}
else if (Pm>35&&Pm<=75)
   {u8g.drawStr( 70, 30, "moderate");}
else if (Pm>75)
   {u8g.drawStr( 70, 30, "bad");}

mingxu976 发表于 2017-7-3 21:38:52

Paderboy 发表于 2017-7-2 08:41
if (Pm35&&Pm75)
   {u8g.drawStr( 70, 30, "bad");}

可以了,但有个问题,pm25为0,即使我不用else也只是会拖慢速度而已啊,其他的比如,大于35小于75,或大于75的应该是不会失效才对的啊
页: [1]
查看完整版本: sds011粉尘模块if生效求教