#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define BLUE 9 // RGB LED Module
#define GREEN 10
#define RED 11
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD 주소값 확인
int DustPin = 8; //먼지 센서
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
float ugm3 = 0;
int redValue = 0; //RGB LED
int greenValue = 0;
int blueValue = 0;
void setup() {
Serial.begin(9600);
pinMode(DustPin ,INPUT); // 먼지센서
starttime = millis();
lcd.init(); // LCD
lcd.backlight();
lcd.setCursor(0,0); //
lcd.print(" Dust Density ");
pinMode(RED, OUTPUT); // RGB LED
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
}
void loop() {
redValue = 0;
greenValue = 0;
blueValue = 0;
duration = pulseIn(DustPin , LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
ugm3= concentration * 100 /13000;
Serial.print("ugm3 = " ); //미세먼지 시리얼모니터
Serial.print(ugm3);
Serial.println("ug/m3" );
lcd.setCursor(0,1); //미세먼지 LCD 출력
lcd.print(" ");
lcd.print(ugm3);
lcd.print(" ug/m3 ");
if(ugm3<30) //각 수치에 따른 LED 색상
{
greenValue = 20;
blueValue = 255;
redValue = 0;
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
}
else if(ugm3<80)
{
greenValue = 255;
blueValue = 20;
redValue = 0;
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
}
else if(ugm3<150)
{
redValue = 250;
greenValue = 60;
blueValue = 0;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
}
else if(ugm3>150)
{
redValue = 220;
greenValue = 0;
blueValue = 0;
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
}
lowpulseoccupancy = 0;
starttime = millis();
}
}