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
#include <DHT.h>                      // 온습도 센서 라이브러리 사용 
#include <Adafruit_NeoPixel.h>        // LED 링 라이브러리 사용
#include <U8glib.h>                   // OLED 디스플레이 라이브러리 사용
 
 
#define DHT_PIN         4             // 온습도 센서 연결 Pin 번호
#define DHT_TYPE        DHT22         // 온습도 센서 타입
#define LED_PIN         2             // LED Ring 연결 Pin 번호
#define CDS_PIN         A0            // 조도 센서 연결 Pin 번호
#define LVL_PIN         A1            // 수위(레벨) 센서 연결 Pin 번호
 
#define LED_PIXELs_SIZE   12          // LED Ring 픽셀 수
 
 
// 객체 선언
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);
 
int lux;                                      // 조도 값
float humidity, temperature;                  // 온습도 값
int waterLvl;                                 // 수위 값
uint32_t levelColor;                          // 수위(레벨) 상태 색상
int ledStep = 0;
int oledStage = 0;
int measureCnt = 0;                           // 센서 측정 카운트
 
void setup() {
  Serial.begin(115200);
  dht.begin();                                // 온습도 센서 활성화
  pixels.begin();                             // LED 링 모듈 활성화
  u8g.begin();                                // OLED 모듈 활성화
 
  measureCnt = 10;
 
}
 
void loop() {
  if (measureCnt == 10) {
    setLevelColor();
    measureSensors();
    showOLED();
    measureCnt = 0;
  }
  measureCnt++;
  showDotLed();
  delay(200);
}
 
 
void showOLED() {
  u8g.firstPage();
  do {
    u8g.setFont(u8g_font_unifont);
    u8g.undoScale();
    switch (oledStage) {
      case 0:
        u8g.drawStr(816"Water Level(%)");                    // 텍스트(Title) 출력
        u8g.setScale2x2();                                        // 폰트 스케일 증가
        u8g.drawStr(2426, String(waterLvl).c_str());
        break;
      case 1:
        u8g.drawStr(816"Temperature('C)");                    // 텍스트(Title) 출력
        u8g.setScale2x2();                                        // 폰트 스케일 증가
        u8g.drawStr(2426, String(temperature).c_str());
        break;
      case 2:
        u8g.drawStr(816"Humidity(%)");                    // 텍스트(Title) 출력
        u8g.setScale2x2();                                        // 폰트 스케일 증가
        u8g.drawStr(2426, String(humidity).c_str());
        break;
      default:
        u8g.drawStr(816"Light(Lux)");                    // 텍스트(Title) 출력
        u8g.setScale2x2();                                        // 폰트 스케일 증가
        u8g.drawStr(2426, String(lux).c_str());
    }
  } while ( u8g.nextPage() );
  oledStage = oledStage == 3 ? 0 : oledStage + 1;
}
 
void measureSensors() {
  humidity = dht.readHumidity();                  // 습도 측정
  temperature = dht.readTemperature();            // 온도 측정
  lux = analogRead(CDS_PIN);                      // 조도 측정
}
 
void setLevelColor() {
  int val = analogRead(LVL_PIN);
  waterLvl = map(val, 010230100);
 
  levelColor = waterLvl < 20 ? pixels.Color(25500) :
               (waterLvl < 80 ? pixels.Color(02550) : pixels.Color(00255));
}
 
void showDotLed() {
  for (int i = 0; i < pixels.numPixels(); i++) {
    if (i % 3 == ledStep)
      pixels.setPixelColor(i, levelColor);
    else
      pixels.setPixelColor(i, 0);
  }
  pixels.show();
  ledStep = ledStep == 2 ? 0 : ledStep + 1;
}
cs

 

+ Recent posts