BASIC4MCU | 질문게시판 | 답변 : 아두이노 미세먼지
페이지 정보
작성자 master 작성일2019-07-11 14:54 조회6,682회 댓글2건본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2019-07-11 오후 2:50:01// by Ok-Hyun Park//#include <Wire.h>#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27,16,2);//#include <DHT11.h>int pin=A1;DHT11 dht11(pin);//int dust_sensor=A0;int rgb_red=5,rgb_green=6,rgb_blue=9;int buzzerPin=13;//int sensor_led=12sampling=280,waiting=40;float dust_value=0,dustDensityug=0,stop_time=9680;double five_dust[5]={0},recent_dust=0,total_dust=0;//byte humi[8]={ // 물컵모양 출력0b00000,0b10001,0b10001,0b10001,0b11111,0b11111,0b11111,0b11111,};byte temp[8]={ // 온도계 모양 출력0b00100,0b01010,0b01010,0b01010,0b01010,0b10001,0b11111,0b01110,};byte char_temp[8]={ // 온도 단위 출력0b10000,0b00110,0b01001,0b01000,0b01000,0b01000,0b01001,0b00110,};void setup(){lcd.init(); lcd.backlight();lcd.createChar(1,temp); // 온도계 모양lcd.createChar(2,humi); // 물컵 모양lcd.createChar(3,char_temp); // 온도 단위pinMode(sensor_led,OUTPUT); // 미세먼지 적외선 ledpinMode(4,OUTPUT);pinMode(buzzerPin,OUTPUT);pinMode(rgb_red,OUTPUT); pinMode(rgb_green,OUTPUT); pinMode(rgb_blue,OUTPUT);Serial.begin(9600);}//void loop(){int i,count=0;float humi,temp;//digitalWrite(sensor_led,LOW); delayMicroseconds(sampling); // LED 켜기dust_value=analogRead(dust_sensor); delayMicroseconds(waiting); // 센서 값 읽어오기digitalWrite(sensor_led,HIGH); delayMicroseconds(stop_time); // LED 끄기recent_dust=(0.17*(dust_value*(5.0/1024))-0.1)*1000; // 미세먼지 값 계산five_dust[4]=recent_dust; // 새로운 미세먼지 값 입력total_dust=five_dust[4]; // 5개의 미세먼지 값을 저장할 변수//for(int i=0; i<4; i++){total_dust+=five_dust[i];five_dust[i]=five_dust[i+1]; // 0~4번째까지 미세먼지 값 저장을 위해 4번째 배열 비워주기}//if(five_dust[0]!=0){dustDensityug=total_dust/5;Serial.print("Dust Density[ug/m^3]: "); Serial.println(dustDensityug);}//if((i=dht11.read(humi,temp))==0){Serial.print("humidity: "); Serial.print(humi);Serial.print("\t temperature: "); Serial.println(temp);}else{ Serial.print("Error: "); Serial.print(i); }//Serial.println();lcd.setCursor( 0,0); lcd.write(byte(1));lcd.setCursor( 2,0); lcd.print((int)temp); // 온도 출력lcd.setCursor( 5,0); lcd.write(byte(3));lcd.setCursor( 8,0); lcd.write(byte(2)); // 물컵 출력lcd.setCursor(10,0); lcd.print(humi); // 습도 출력lcd.setCursor(14,0); lcd.print("%"); // %출력lcd.setCursor( 0,1); lcd.print("F.Dust"); // fine dust 글자 출력lcd.setCursor( 7,1); lcd.print(dustDensityug); // 미세먼지 출력lcd.setCursor(11,1); lcd.print("ug/m3");//if (dustDensityug<= 30.0)light( 0, 0,255); // 좋음 일때 파란색else if(dustDensityug<= 80.0)light( 0,255, 0); // 보통 일때 녹색else if(dustDensityug<=150.0)light(255, 80, 1); // 나쁨 일때 노란색else light(255, 0, 0); // 매우 나쁨 일때 빨간색//if(dustDensityug>150.0)tone(buzzerPin,523,500);else noTone(buzzerPin);delay(1150);}//void light(int a,int b,int c){analogWrite(rgb_red,a); analogWrite(rgb_green,b); analogWrite(rgb_blue,c);}
댓글 2
조회수 6,682핑구맹구님의 댓글
핑구맹구 작성일
마스터님 ㅠㅠㅠㅠㅠㅠ진짜 감사합니다 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ내일 발푠데 됩니다 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
진짜 천재십ㅂ니다 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
마스터님 미세먼지 농도가 30아래이면 파란색 led가 나와야하는데 초록색 / 노란색 / 빨간색 밖에 안나옵니다.
파란색이 왜안나오는 걸까요?
/*
#include <Wire.h> // i2C 통신을 위한 라이브러리
#include <LiquidCrystal_I2C.h> // LCD 1602 I2C용 라이브러리
#include <DHT11.h> // 온습도 센서 사용을 위한 라이브러리
int dust_sensor = A0; // 미세먼지 핀 설정
int pin = A1; // 온습도 센서 핀 설정
int rgb_red = 5; // rgb 핀 빨간색 핀
int rgb_green = 6; // rgb핀 녹색색 핀
int rgb_blue = 9; // rgb핀 파란색 핀
int buzzerPin = 13;
float dust_value = 0; // 센서에서 입력받은 미세먼지 값
float dustDensityug = 0; // ug/m^3 값을 계산
int sensor_led = 12; // 미세먼지 센서 안에 있는 적외선 led 핀 번호
int sampling = 280; // 적외선 led를 키고, 센서값을 읽어들여 미세먼지를 측정하는 샘플링 시간
int waiting = 40;
float stop_time = 9680; // 센서를 구동하지 않는 시간
double five_dust[5] = {0};
double recent_dust = 0, total_dust = 0;
DHT11 dht11(pin); // 온습도 센서사용을 위한 객체 생성
LiquidCrystal_I2C lcd(0x27,16,2); // 접근주소: 0x3F or 0x27 1602 Display
byte humi[8] = { // 물컵모양 출력
0b00000,
0b10001,
0b10001,
0b10001,
0b11111,
0b11111,
0b11111,
0b11111,
};
byte temp[8] = { // 온도계 모양 출력
0b00100,
0b01010,
0b01010,
0b01010,
0b01010,
0b10001,
0b11111,
0b01110,
};
byte char_temp[8] = { // 온도 단위 출력
0b10000,
0b00110,
0b01001,
0b01000,
0b01000,
0b01000,
0b01001,
0b00110,
};
void setup(){
lcd.init(); // LCD 초기화
lcd.backlight(); // 백라이트 켜기
lcd.createChar(1, temp); // 온도계모양 출력
lcd.createChar(2, humi); // 물컵 모양 출력
lcd.createChar(3, char_temp); // 온도 단위 출력
pinMode(sensor_led,OUTPUT); // 미세먼지 적외선 led를 출력으로 설정
pinMode(4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(rgb_red, OUTPUT); // 3색 LED 모듈 출력으로 설정, 붉은색
pinMode(rgb_green, OUTPUT); // 녹색
pinMode(rgb_blue, OUTPUT); // 파란색
Serial.begin(9600); // 시리얼 모니터 시작, 속도는 9600
}
void loop(){
digitalWrite(sensor_led, LOW); // LED 켜기
delayMicroseconds(sampling); // 샘플링해주는 시간.
int count=0;
dust_value = analogRead(dust_sensor); // 센서 값 읽어오기
delayMicroseconds(waiting); // 너무 많은 데이터 입력을 피해주기 위해 잠시 멈춰주는 시간.
digitalWrite(sensor_led, HIGH); // LED 끄기
delayMicroseconds(stop_time); // LED 끄고 대기
recent_dust = (0.17 * (dust_value * (5.0 / 1024)) - 0.1) * 1000; // 미세먼지 값 계산
five_dust[4] = recent_dust; // 새로운 미세먼지 값 입력
total_dust = five_dust[4]; // 5개의 미세먼지 값을 저장할 변수
for(int i=0; i<4; i++)
{
total_dust += five_dust[i];
five_dust[i] = five_dust[i+1]; // 0~4번째까지 미세먼지 값 저장을 위해 4번째 배열 비워주기
}
if(five_dust[0] != 0)
{
dustDensityug = total_dust / 5;
Serial.print("Dust Density [ug/m^3]: "); // 시리얼 모니터에 미세먼지 값 출력
Serial.println(dustDensityug);
}
int i;
float humi, temp; //온도, 습도 값을 위한 변수.
if((i = dht11.read(humi, temp)) == 0) { // 습도, 온도 값을 읽어오면 시리
Serial.print("humidity:"); // ‘시리얼 플로터’ 사용위해 이부분 주석 필요
Serial.print(humi); // 습도값 출력
Serial.print("\t temperature:"); // ‘시리얼 플로터’ 사용위해 이부분 주석 필요
Serial.println(temp); // 온도값 출력
}
else{ // 온습도 값을 읽어오지 못했을 때
Serial.print("Error:");
Serial.print(i);
}
Serial.println();
lcd.setCursor(0,0); // 1번째, 1라인
lcd.write(byte(1)); // 온도계 출력
lcd.setCursor(2,0); // 3번째, 1라인
lcd.print((int)temp); // 온도 출력
lcd.setCursor(5,0); // 6번째 1라인
lcd.write(byte(3)); // 온도 단위 출력
lcd.setCursor(8,0); // 9번째, 1라인
lcd.write(byte(2)); // 물컵 출력
lcd.setCursor(10,0); // 11번째, 1라인
lcd.print(humi); // 습도 출력
lcd.setCursor(14,0); // 15번째, 1라인
lcd.print("%"); // % 출력
lcd.setCursor(0,1); // 1번째, 2라인
lcd.print("F.Dust"); // fine dust 글자 출력
lcd.setCursor(7,1); // 6번째, 2라인
lcd.print(dustDensityug); // 미세먼지 출력
lcd.setCursor(11,1);
lcd.print("ug/m3");
if(dustDensityug <= 30.0) // 대기 중 미세먼지가 좋음 일때 파란색 출력
light(0, 0, 225);
else if(30.0 < dustDensityug && dustDensityug <= 80.0) // 대기 중 미세먼지가 보통 일때 녹색 출력
light(0, 255, 0);
else if (80.0 < dustDensityug && dustDensityug <= 150.0) // 대기 중 미세먼지가 나쁨 일때 노란색 출력
light(255, 80, 1);
else light(255, 0, 0);
if(dustDensityug>150.0) tone(buzzerPin,523, 500);
else noTone(buzzerPin);
delay(1150);
delay(900);
}
void light(int a, int b, int c){
analogWrite(rgb_red, a);
analogWrite(rgb_green, b);
analogWrite(rgb_blue, c);
}
코드입니다
master님의 댓글
master 작성일
1. 파란색 LED가 고장
2. 연결을 제대로 하지 못했다.
3. rgb_blue=9; 핀을 변경해본다.
다른색의 LED와 바꿔보면 1,2번은 쉽게 판단을 할 수 있을겁니다.