Skip to content

Instantly share code, notes, and snippets.

@joranmulderij
Last active May 5, 2021 12:08
Show Gist options
  • Save joranmulderij/a25928ae83b45ed5a621e339b179ce54 to your computer and use it in GitHub Desktop.
Save joranmulderij/a25928ae83b45ed5a621e339b179ce54 to your computer and use it in GitHub Desktop.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include "PMS.h"
#define TFT_SCLK 4
#define TFT_SID 5
#define TFT_RST 6 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 7
#define TFT_CS 8
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_SID, TFT_SCLK, TFT_RST);
PMS pms(Serial);
PMS::DATA data;
#define BLACK 0x0000
#define WHITE 0xFFFF
/*#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0 */
#define PM1COLOR 0xFB3F
#define PM25COLOR 0x059E
#define PM10COLOR 0x0398
#define GREEN 0x0700
#define YELLOW 0x07FF
#define ORANGE 0x03FF
#define RED 0x001F
#define PURPLE 0x4812
#define MAROON 0x200F
float p = 3.1415926;
void setup(void) {
Serial.begin(9600);
// Use this initializer if using a 1.8" TFT screen:
tft.initR(INITR_GREENTAB);
tft.setRotation(1);
tft.fillScreen(BLACK);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.print("Made by\n\nJoran\nMulderij");
delay(5000);
tft.setCursor(0, 0);
tft.setTextColor(BLACK);
tft.print("Made by\n\nJoran\nMulderij");
//tft.fillRect(5, 5, 118, 150, GREEN);
//drawBase1();
//drawBase25();
//drawBase10();
}
int graphIndex = 0;
int lastColor10 = 0;
int lastColor25 = 0;
int lastColor1 = 0;
void loop() {
if (pms.read(data))
{
/*Serial1.print("PM 1.0 (ug/m3): ");
Serial1.println(data.PM_AE_UG_1_0);
Serial1.print("PM 2.5 (ug/m3): ");
Serial1.println(data.PM_AE_UG_2_5);
Serial1.print("PM 10.0 (ug/m3): ");
Serial1.println(data.PM_AE_UG_10_0);
Serial1.println();*/
int pm1a = pm25ToAQI(data.PM_AE_UG_1_0);
int pm25a = pm25ToAQI(data.PM_AE_UG_2_5);
int pm10a = pm10ToAQI(data.PM_AE_UG_10_0);
if(lastColor10 != AQIToColor(pm10a)){
lastColor10 = AQIToColor(pm10a);
drawBase10();
}
if(lastColor25 != AQIToColor(pm25a)){
lastColor25 = AQIToColor(pm25a);
drawBase25();
}
if(lastColor1 != AQIToColor(pm1a)){
lastColor1 = AQIToColor(pm1a);
drawBase1();
}
tft.setTextSize(2);
tft.setCursor(10, 20);
tft.setTextColor(BLACK, lastColor1);
tft.print(pm1a);
if(pm1a < 100) tft.print(" ");
if(pm1a < 10) tft.print(" ");
tft.setCursor(60, 20);
tft.setTextColor(BLACK, lastColor25);
tft.print(pm25a);
if(pm25a < 100) tft.print(" ");
if(pm25a < 10) tft.print(" ");
tft.setCursor(110, 20);
tft.setTextColor(BLACK, lastColor10);
tft.print(pm10a);
if(pm10a < 100) tft.print(" ");
if(pm10a < 10) tft.print(" ");
tft.setTextSize(1);
tft.setCursor(10, 40);
tft.setTextColor(BLACK, lastColor1);
int pm1 = data.PM_AE_UG_1_0;
tft.print(pm1);
if(pm1 < 100) tft.print(" ");
if(pm1 < 10) tft.print(" ");
tft.setCursor(60, 40);
tft.setTextColor(BLACK, lastColor25);
int pm25 = data.PM_AE_UG_2_5;
tft.print(pm25);
if(pm25 < 100) tft.print(" ");
if(pm25 < 10) tft.print(" ");
tft.setCursor(110, 40);
tft.setTextColor(BLACK, lastColor10);
int pm10 = data.PM_AE_UG_10_0;
tft.print(pm10);
if(pm10 < 100) tft.print(" ");
if(pm10 < 10) tft.print(" ");
tft.fillRect(graphIndex, 68, 5, 60, BLACK);
if(data.PM_AE_UG_10_0 > 300){
tft.drawLine(graphIndex, 127, graphIndex, 68, PM10COLOR);
} else {
tft.drawLine(graphIndex, 127, graphIndex, 127 - (data.PM_AE_UG_10_0 / 5), PM10COLOR);
}
if(data.PM_AE_UG_2_5 > 300){
tft.drawLine(graphIndex, 127, graphIndex, 68, PM25COLOR);
} else {
tft.drawLine(graphIndex, 127, graphIndex, 127 - (data.PM_AE_UG_2_5 / 5), PM25COLOR);
}
if(data.PM_AE_UG_1_0 > 300 ){
tft.drawLine(graphIndex, 127, graphIndex, 68, PM1COLOR);
} else {
tft.drawLine(graphIndex, 127, graphIndex, 127 - (data.PM_AE_UG_1_0 / 5), PM1COLOR);
}
graphIndex++;
while(Serial.available()){
Serial.read();
}
if(graphIndex >= 160){
graphIndex = 0;
}
}
}
void drawBase10(){
tft.fillRect(105, 0, 50, 50, lastColor10);
tft.setTextColor(BLACK);
tft.setTextSize(1);
tft.setCursor(110, 5);
tft.print("PM 10");
}
void drawBase25(){
tft.fillRect(55, 0, 50, 50, lastColor25);
tft.setTextColor(BLACK);
tft.setTextSize(1);
tft.setCursor(60, 5);
tft.print("PM 2.5");
}
void drawBase1(){
tft.fillRect(5, 0, 50, 50, lastColor1);
tft.setTextColor(BLACK);
tft.setTextSize(1);
tft.setCursor(10, 5);
tft.print("PM 1");
}
int AQIToColor(int aqi){
if(aqi <= 50){
return GREEN;
} else if(aqi <= 100){
return YELLOW;
} else if(aqi <= 150){
return ORANGE;
} else if(aqi <= 200){
return RED;
} else if(aqi <= 300){
return PURPLE;
} else if(aqi <= 500){
return MAROON;
} else {
return WHITE;
}
}
int pm25ToAQI(int pm){
float Chigh, Clow, Ihigh, Ilow;
if(pm <= 12) Clow = 0, Chigh = 12, Ilow = 0, Ihigh = 50;
else if(pm <= 35) Clow = 13, Chigh = 35, Ilow = 51, Ihigh = 100;
else if(pm <= 55) Clow = 36, Chigh = 55, Ilow = 101, Ihigh = 150;
else if(pm <= 150) Clow = 56, Chigh = 150, Ilow = 151, Ihigh = 200;
else if(pm <= 250) Clow = 151, Chigh = 250, Ilow = 201, Ihigh = 300;
else if(pm <= 350) Clow = 251, Chigh = 350, Ilow = 301, Ihigh = 400;
else if(pm <= 500) Clow = 351, Chigh = 500, Ilow = 401, Ihigh = 500;
else return 500;
return (Ihigh - Ilow)/(Chigh - Clow)*(pm - Clow) + Ilow;
}
int pm10ToAQI(int pm){
float Chigh, Clow, Ihigh, Ilow;
if(pm <= 54) Clow = 0, Chigh = 54, Ilow = 0, Ihigh = 50;
else if(pm <= 154) Clow = 55, Chigh = 154, Ilow = 51, Ihigh = 100;
else if(pm <= 254) Clow = 155, Chigh = 254, Ilow = 101, Ihigh = 150;
else if(pm <= 354) Clow = 255, Chigh = 354, Ilow = 151, Ihigh = 200;
else if(pm <= 424) Clow = 355, Chigh = 424, Ilow = 201, Ihigh = 300;
else if(pm <= 504) Clow = 425, Chigh = 504, Ilow = 301, Ihigh = 400;
else if(pm <= 604) Clow = 505, Chigh = 604, Ilow = 401, Ihigh = 500;
else return 500;
return (Ihigh - Ilow)/(Chigh - Clow)*(pm - Clow) + Ilow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment