1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <DHT.h>                      // 온습도 센서 라이브러리 사용 
#include <Adafruit_NeoPixel.h>        // LED 링 라이브러리 사용
#include <U8glib.h>                   // OLED 디스플레이 라이브러리 사용
 
#define DHT_PIN         4             // 온습도 센서 연결 Pin 번호
#define DHT_TYPE        DHT22         // 온습도 센서 타입
#define CDS_PIN         A0            // 조도 센서 연결 Pin 번호
#define LED_PIN         2             // LED Ring 연결 Pin 번호
#define BTN_PIN         6             // BTN 연결 Pin 번호
#define BTN_LED_PIN     5             // BTN LED연결 Pin 번호
 
#define LED_PIXELs_SIZE   12          // LED Ring 픽셀 수
 
 
#define ORANGE_START_POS  10          // 온습도 수치에 따라 주황/초록/파랑 계열 색상 범위(Position 기준 수치) 지정
#define ORANGE_END_POS    40
#define GREEN_START_POS   45
#define GREEN_END_POS     95
#define BLUE_START_POS    105
#define BLUE_END_POS      160
 
// 객체 선언
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(LED_PIXELs_SIZE, LED_PIN, NEO_GRB + NEO_KHZ800);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
 
// OLED Bitmap 아이콘(32*32) 배열
const unsigned char humiIcon [] PROGMEM = {
  0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00,
  0x000x000x000x000x000x010x800x000x000x030xc00x000x000x060x600x00,
  0x000x0c0x300x000x000x080x180x000x000x180x080x000x000x300x0c0x00,
  0x000x240x060x000x000x600x020x000x000xc00x030x000x000x900x010x00,
  0x000x800x010x800x010x800x000x800x010x800x000x800x010x800x000x80,
  0x010x800x000x800x010x800x010x800x000x800x010x800x000xc00x030x00,
  0x000x600x060x000x000x3c0x3c0x000x000x1f0xf80x000x000x070xe00x00,
  0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00
};
const unsigned char tempIcon [] PROGMEM = {
  0x000x000x000x000x000x000x000x000x000x030x800x000x000x070xe00x00,
  0x000x0f0xe00x000x000x0c0x300x000x000x080x300x000x000x080x300x00,
  0x000x080x300x000x000x090xb00x000x000x0b0xb00x000x000x0b0xb00x00,
  0x000x0b0xb00x000x000x0b0xb00x000x000x0b0xb00x000x000x1b0xb80x00,
  0x000x3b0x9c0x000x000x730x8e0x000x000x670xe60x000x000xef0xe60x00,
  0x000xcf0xf30x000x000xdf0xf30x000x000xdf0xf30x000x000xcf0xf30x00,
  0x000xef0xe60x000x000x670xc60x000x000x700x0c0x000x000x3c0x3c0x00,
  0x000x1f0xf00x000x000x070xc00x000x000x000x000x000x000x000x000x00
};
 
float tempLowValue = 15;              // 온도 상태 기준 수치
float tempHighValue = 25;
float humiLowValue = 20;              // 습도 상태 기준 수치
float humiHighValue = 70;
 
int lux;                                      // LED 세기 값
float humidity, temperature;                  // 온습도 값
long loopInterval = 100;                      // Loop 간격
long loopTime;                                // Loop 측정 시간
int sensingDelay = 20;                        // 센싱 간격(횟수)
int sensingCnt = 0;                           // 센싱 동작 카운트
int colorPos, startPos, endPos;               // LED Color Position 값
bool isPosIncrement = true;                   // Color Position 증가 상태값
bool enable = false;                          // On/Off 상태 값
 
void setup() {
  Serial.begin(115200);
  dht.begin();                                // 온습도 센서 활성화
  pixels.begin();                             // LED 링 모듈 활성화
  u8g.begin();                                // OLED 모듈 활성화
 
  pinMode(BTN_PIN, INPUT);                    // 버튼 모듈 Pin 모드 설정
  pinMode(BTN_LED_PIN, OUTPUT);
  
  clearOLED();                                
  clearLed();
    
  Serial.println("## Mood Lighting Setup ##");
}
 
void loop() {
  btnEvent();                                               // 버튼 모듈 클릭 이벤트 함수 호출
 
  if (enable) {                                             // LED등 상태 활성화,
    if (millis() - loopTime > loopInterval) {               // 설정된 Loop 시간 초과,
      if (sensingCnt == 0 || sensingCnt > sensingDelay) {   // 초기 센싱 상태 또는 센싱 동작 카운트가 기준 횟수 초과 시, 
        measureSensor();                                        // 온습도 수치 측정
        showOLED();                                             // 온습도 값 & 아이콘 OLED 출력
        setColorRange();                                        // LED 출력 Color Position 범위 설정
        sensingCnt = 0;                                         // 센싱 동작 카운트 초기화
      }
      sensingCnt++;                                         // 센싱 동작 카운트 증가
      showLed();                                            // LED 등 출력
      loopTime = millis();                                  // Loop 측정 시간 초기화
    }
  }
  delay(50);
}
 
// 버튼 클릭 이벤트 함수
void btnEvent() {
  if (digitalRead(BTN_PIN)) {                   // 버튼 클릭 시,
    while (digitalRead(BTN_PIN)) {
      delay(100);
    }
    enable = !enable;                           // 동작 활성화 상태 전환
    if (enable) {                                   // 동작 활성화 시,
      colorPos = 0;                                     // Color Position 초기화
      isPosIncrement = true;                            // Color Position 증가 설정
    } else {                                        // 동작 비활성화 시, 
      clearOLED();                                      // OLED 출력 종료
      clearLed();                                       // LED 출력 종료
    }
    digitalWrite(BTN_LED_PIN, enable);          // 동작 상태에 따라 버튼 LED 상태 설정 
  }
}
 
// OLED 종료 함수 (기존 출력 정보 지우기)
void clearOLED() {
  u8g.firstPage();
  do {
    // Null
  } while ( u8g.nextPage() );
}
 
// OLED 출력 함수 (온습도 정보 출력)
void showOLED() {
  u8g.firstPage();
  do {
    u8g.drawBitmapP( 120432, humiIcon);             // 비트맵 아이콘 출력
    u8g.drawBitmapP( 1232432, tempIcon);
    u8g.setFont(u8g_font_profont22r);                     // 폰트 설정
    u8g.drawStr(5222, String(humidity).c_str());        // 온습도 수치 출력
    u8g.drawStr(5254, String(temperature).c_str());
  } while ( u8g.nextPage() );
}
 
// 센서 측정 함수
void measureSensor() {
  humidity = dht.readHumidity();                  // 습도 측정 
  temperature = dht.readTemperature();            // 온도 측정 
 
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("DHT sensor error!");
    return;
  }
 
  float cdsValue = analogRead(CDS_PIN);           // 조도 센서값 측정
  lux = map(cdsValue, 0102310020);          // 조도 수치에 따른 LED 밝기 설정
}
 
// LED Color Position 범위 설정 함수
void setColorRange() {
  if (temperature < tempLowValue || humidity > humiHighValue) {     
    // 측정된 온도 수치가 LOW 온도 보다 낮거나 습도 수치가 HIGH 습도 보다 높으면 파랑 계열 색상 범위 설정
    startPos = BLUE_START_POS;
    endPos = BLUE_END_POS;
  } else if (temperature > tempHighValue || humidity < humiLowValue) {
    // 측정된 온도 수치가 HIGH 온도 보다 높거나 습도 수치가 LOW 습도 보다 낮으면 주황 계열 색상 범위 설정
    startPos = ORANGE_START_POS;
    endPos = ORANGE_END_POS;
  } else {
    // 이외의 수치는 초록 계열 색상 범위 설정
    startPos = GREEN_START_POS;
    endPos = GREEN_END_POS;
  }
}
 
 
// 무드등 출력 비활성화 (모든 LED 픽셀을 검정색으로 출력)
void clearLed() {
  for (int i = 0; i < LED_PIXELs_SIZE; i ++) {
    pixels.setPixelColor(i,  pixels.Color(000));
  }
  pixels.show();
}
 
// 무드등 LED 출력
void showLed() {
  isPosIncrement ? colorPos++ : colorPos--;               // Color Position 증가 여부에 따라 현재값 +/- 
  
  for (int i = 0; i < pixels.numPixels(); i++) {          // 현재 Color Position에 대한 RGB 색상 출력
    pixels.setPixelColor(i, getRGBColor(colorPos));
  }
  
  if (colorPos >= endPos) {                               // 현재 Color Position이 종료 범위에 도달하면 
    isPosIncrement = false;                                   // isPosIncrement를 False로 설정하여 Color Position 감소 
  } else if (colorPos <= startPos) {                      // 시작 범위에 도달하면 
    isPosIncrement = true;                                    // isPosIncrement를 True로 설정하여 Color Position 증가
  }
 
  pixels.setBrightness(lux);                              // LED 밝기 설정
  pixels.show();                                          // 지정된 LED 색상 출력
}
 
// Color Position에 따른 RGB 색상 반환 함수
uint32_t getRGBColor(byte pos) {
  pos = 255 - pos;
  if (pos < 85) {
    return pixels.Color(255 - pos * 30, pos * 3);
  }
  if (pos < 170) {
    pos -= 85;
    return pixels.Color(0, pos * 3255 - pos * 3);
  }
  pos -= 170;
  return pixels.Color(pos * 3255 - pos * 30);
}
cs

 

+ Recent posts