|
新手代碼寫得比較亂,也沒大標注,設備:arduino uno,acg激光傳感,dht11,3.2寸tft顯示屏。所用到的庫見附件。
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_TFTLCD.h> // Hardware-specific library
- #include <dht11.h>
- dht11 DHT11;
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- #define LCD_CS A3 // Chip Select goes to Analog 3
- #define LCD_CD A2 // Command/Data goes to Analog 2
- #define LCD_WR A1 // LCD Write goes to Analog 1
- #define LCD_RD A0 // LCD Read goes to Analog 0
- #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
- #define DHT11PIN 10 //tmp pin
- uint8_t d[28];
- uint8_t c;
- bool logData = false;
- int i=0;
- int j=0;
- int k=0;
- int l=0;
- int m=0;
- int pm25=0;
- int opm25=0;
- int pm10=0;
- int opm10=0;
- int hum=0;
- int tem=0;
- double Aqi=0;
- Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
- // If using the shield, all control and data lines are fixed, and
- // a simpler declaration can optionally be used:
- // Adafruit_TFTLCD tft;
- //Adafruit_GFX_Button buttons[15];
- void setup() {
- Serial.begin(9600);
- pinMode(11,OUTPUT);
- pinMode(12,OUTPUT);
- digitalWrite(12,HIGH);
- tft.reset();
- uint16_t identifier = tft.readID();
- if(identifier == 0x9325) {
- Serial.println(F("Found ILI9325 LCD driver"));}
- else{Serial.print(F("Unknown LCD driver chip: "));
- return;}
- tft.begin(identifier);
- tft.setRotation(1);
- testtext();
- }
- void loop() {
-
- while(Serial.available()){
- int sum=0;
- c=Serial.read();
- if(c==50){c=Serial.read();
- if(c==61){c=Serial.read();
- if(c==0){c=Serial.read();
- if(c=28){c=Serial.read();
- logData=true;}}}}
- if(logData)
- {d[i]=c; //Serial.print(d[i]); Serial.print(",");Serial.println(i);
- i=i+1;}
- if(i==28){if(j==0){opm25=0;opm10=0;}
- sum=d[0]+d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9]+d[10]+d[11]+d[12]+d[13]+d[14]+d[15]+d[16]+d[17]+d[18]+d[19]+d[20]+d[21]+d[22]+d[23]+d[24]+d[25];
- if(sum==d[26]*256+d[27]-139){j=j+1;opm25=opm25+d[2]*256+d[3];opm10=opm10+d[4]*256+d[5];}
- sum=0;logData = false;i=0;
- //Serial.print(opm25); Serial.print(",");Serial.print(opm10);Serial.print(",");Serial.println(j);
- }
- }
- if(l==0)
- {
- dText();
- pm25=opm25/j;pm10=opm10/j;j=0;l=6;
- if(pm25<0)pm25=8;if(pm10<0)pm10=8;
- DHT11.read(DHT11PIN); //int chk =DHT11.read(DHT11PIN)
- hum=DHT11.humidity;tem=DHT11.temperature;
- uText();
- if(pm25>60)k=k+1;
- if(k>5){digitalWrite(11,HIGH);k=k+1;}
- if(k>360){digitalWrite(11,LOW);k=0;}
- if(pm25<70)m=m+1;
- if(m>=50){digitalWrite(12,LOW);m=m+1;}
- if(m>=400){digitalWrite(12,HIGH);m=0;}
- }
- //Serial.print((float)DHT11.humidity,2);Serial.print(",");Serial.println((float)DHT11.temperature, 2);
- //Serial.print(pm25);Serial.print(",");Serial.println(pm10);
- //Serial.print(k);;Serial.print(",");Serial.println(m);//Serial.println(getAQI(umg3));
- l=l-1;delay(800);
- }
- unsigned long testtext()
- {
- tft.fillScreen(BLACK);
- unsigned long start = micros();
- tft.setTextSize(2);tft.println("Tmp:");tft.setCursor(128,50);tft.println("oC");
- tft.setCursor(157,2);tft.print("Pm25:");tft.setCursor(280,35);tft.println("ug");tft.setCursor(280,50);tft.println("/m3");
- tft.setCursor(0,70); tft.println("Hum:");tft.setCursor(140,120);tft.println("%");
- tft.setCursor(157,70); tft.println("Aqi:");tft.setCursor(280,105);//tft.println("ug");tft.setCursor(280,120);tft.println("/m3");
- tft.setCursor(0,54);tft.println("__________________________");
- tft.setCursor(150,0);tft.println("|");tft.setCursor(150,12);tft.println("|");tft.setCursor(150,24);tft.println("|");
- tft.setCursor(150,36);tft.println("|");tft.setCursor(150,48);tft.println("|");tft.setCursor(150,60);tft.println("|");
- tft.setCursor(150,72);tft.println("|");tft.setCursor(150,84);tft.println("|");tft.setCursor(150,96);tft.println("|");
- tft.setCursor(150,108);tft.println("|");tft.setCursor(150,120);tft.println("|");
- tft.setCursor(0,150); tft.setTextColor(GREEN);tft.setTextSize(5);tft.print("Wolf");
- tft.setCursor(155,140);tft.setTextSize(1);tft.setTextColor(RED);tft.print("*");
- tft.setCursor(152,144);tft.print("**");
- tft.setCursor(149,148);tft.print("***");
- tft.setCursor(152,152);tft.print("**");
- tft.setCursor(155,156);tft.print("*");
- tft.setCursor(152,160);tft.setTextSize(2);tft.print("|");
- tft.setCursor(200,150);tft.setTextColor(GREEN);tft.setTextSize(5);tft.println("Hui");
- tft.setTextSize(1);
- tft.println();
- tft.println("Life is troubling my body,but I dealing it like my");
- tft.println("first love.God helps those who help themselves.");
- tft.println("in the life with my blurglecruncheon,");
- tft.println("see if I can or not!");
- return micros() - start;
- }
- unsigned long uText() {
- uint16_t cpm25 = GREEN;uint16_t cpm10 = GREEN;uint16_t colotm = GREEN;uint16_t colohu = GREEN;double q25=getAQI(pm25);double q10=getAQI(pm10);
- if(pm25<=35)cpm25=WHITE;if(pm25>35&&pm25<=60)cpm25=YELLOW;if(pm25>60&&pm25<=150)cpm25=RED; if(pm25>150&&pm25<250)cpm25=MAGENTA;if(pm25>=250)cpm25=CYAN;
- if(Aqi<=50)cpm10=BLUE;if(Aqi>50&&Aqi<=100)cpm10=YELLOW;if(Aqi>100&&Aqi<=150)cpm10=RED; if(Aqi>150&&Aqi<200)cpm10=MAGENTA;if(Aqi>=200)cpm10=CYAN;
- if(hum<40)colohu=RED; if(hum>40&&hum<70)colohu=GREEN;if(hum>=70)colohu=BLUE;
- if(tem<=18)colotm=BLUE; if(tem>18&&tem<25)colotm=GREEN;if(tem>=25)colotm=RED;
- if(q25>q10)Aqi=q25;else Aqi=q10;
- tft.setTextSize(4);
- tft.setCursor(200,30); tft.setTextColor(cpm25); tft.println(pm25);
- tft.setCursor(170,100); tft.setTextColor(cpm10);tft.println(Aqi);
- tft.setCursor(50,30); tft.setTextColor(colotm); tft.println(tem);
- tft.setCursor(50,100); tft.setTextColor(colohu); tft.println(hum);
- }
-
- unsigned long dText()
- {
- tft.setCursor(50, 30); tft.setTextColor(BLACK);
- tft.setTextSize(4); tft.println(tem);
- tft.setCursor(50, 100); tft.println(hum);
- tft.setCursor(200, 30);tft.println(pm25);
- tft.setCursor(170, 100);tft.println(Aqi);
-
- }
- double getAQI(double pm25) {
- double aqiL = 0;double aqiH = 0;double bpL = 0;double bpH = 0;double aqi = 0;//根據pm2.5和aqi對應關系分別計算aqi
- if (pm25 >= 0 && pm25 <= 35) {
- aqiL = 0;aqiH = 50;bpL = 0;bpH = 35;
- } else if (pm25 > 35 && pm25 <= 75) {
- aqiL = 50;aqiH = 100;bpL = 35;bpH = 75;
- } else if (pm25 > 75 && pm25 <= 115) {
- aqiL = 100; aqiH = 150;bpL = 75; bpH = 115;
- } else if (pm25 > 115 && pm25 <= 150) {
- aqiL = 150;aqiH = 200;bpL = 115; bpH = 150;
- } else if (pm25 > 150 && pm25 <= 250) {
- aqiL = 200;aqiH = 300;bpL = 150;bpH = 250;
- } else if (pm25 > 250 && pm25 <= 350) {
- aqiL = 300;aqiH = 400; bpL = 250; bpH = 350;
- } else if (pm25 > 350) {
- aqiL = 400; aqiH = 500; bpL = 350;bpH = 500;
- }
- //公式aqi = (aqiH - aqiL) / (bpH - bpL) * (desity - bpL) + aqiL;
- aqi = (aqiH - aqiL) / (bpH - bpL) * (pm25 - bpL) + aqiL;
- return aqi;
- }
復制代碼
|
|