//FA20 hyunho #include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif #define NUM_LEDS 1 #define DATA_PIN 0 #define HUMIDIFIER 9 Adafruit_NeoPixel pixels(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 500 // Time (in milliseconds) to pause between pixels #include #include //#include #define LENG 31 //0x42 + 31 bytes equal to 32 bytes 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 int neor=255; int neog=255; int neob=255; static int maxdust = 50; static int mindust = 0; SoftwareSerial PMSerial(6, 7); // RX, TX void setup() { pinMode(HUMIDIFIER, OUTPUT); pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) PMSerial.begin(9600); PMSerial.setTimeout(1500); pixels.setPixelColor(0, pixels.Color(255, 255, 255)); pixels.show(); digitalWrite(HUMIDIFIER, HIGH); delay(3000); pixels.setPixelColor(0, pixels.Color(0, 0, 0)); pixels.show(); digitalWrite(HUMIDIFIER, LOW); } void loop() { //Serial.print("loop:"); //unsigned int uptime = millis(); //Serial.println(uptime); measuredust(); int firstr=255; int firstg=120; int firstb=0; //ledgo(); if (PM2_5Value < mindust) {PM2_5Value = mindust;} if (PM2_5Value > maxdust) {PM2_5Value = maxdust;} firstr= map(PM2_5Value, mindust, maxdust, 255, 250); firstg= map(PM2_5Value, mindust, maxdust, 255, 120); firstb= map(PM2_5Value, mindust, maxdust, 255, 0); if (PM2_5Value = maxdust) { digitalWrite(HUMIDIFIER, HIGH); } else { digitalWrite(HUMIDIFIER, LOW); } if(firstr < neor){ neor = neor - 1; } else { neor = neor + 1; } if(firstg < neog){ neog = neog - 1; } else { neog = neog + 1; } if(firstb < neob){ neob = neob - 1; } else { neob = neob + 1; } pixels.setPixelColor(0, pixels.Color(neor, neog, neob)); pixels.show(); delay(10); } void measuredust(){ static unsigned long OledTimer=millis(); if (millis() - OledTimer >=10000) { OledTimer=millis(); if(PMSerial.find(0x42)){ PMSerial.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 } } } } } 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; } int transmitPM01(unsigned char *thebuf) { int PM01Val; PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module return PM01Val; } //transmit PM Value to PC int transmitPM2_5(unsigned char *thebuf) { int PM2_5Val; PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module return PM2_5Val; } //transmit PM Value to PC int transmitPM10(unsigned char *thebuf) { int PM10Val; PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module return PM10Val; }