STUDY BLOG

온도센서로 RGB 모듈 제어 본문

Hardware/Raspberry Pi

온도센서로 RGB 모듈 제어

쥬루 2021. 4. 8. 09:53

온도 24 미만 -> 빨간불 on

그 외 -> 녹색불 on

 1 #include<iostream>
 2 #include<unistd.h>
 3 #include<wiringPi.h>
 4 #include<iomanip>
 5 using namespace std;
 6
 7 #define USING_DHT11      true   // The DHT11 uses only 8 bits
 8 #define DHT_GPIO         22      // Using GPIO 22 for this example
 9 #define LH_THRESHOLD     26      // Low=~14, High=~38 - pick avg.
10
11 #define LED_R 18
12 #define LED_G 17
13 #define LED_B 27
14
15 int main(){
16     wiringPiSetupGpio();
17     pinMode(LED_R, OUTPUT);
18     pinMode(LED_G, OUTPUT);
19     pinMode(LED_B, OUTPUT);
20
21
22  int humid = 0, temp = 0;
23    cout << "Starting the one-wire sensor program" << endl;
24    wiringPiSetupGpio();
25    piHiPri(99);
26 while(1){
27 TRYAGAIN:                        // If checksum fails (come back here)
28    unsigned char data[5] = {0,0,0,0,0};
29    pinMode(DHT_GPIO, OUTPUT);                 // gpio starts as output
30    digitalWrite(DHT_GPIO, LOW);               // pull the line low
31    usleep(18000);                             // wait for 18ms
32
33    digitalWrite(DHT_GPIO, HIGH);              // set the line high
34    pinMode(DHT_GPIO, INPUT);                  // now gpio is an input
35
36    // need to ignore the first and second high after going low
37    do { delayMicroseconds(1); } while(digitalRead(DHT_GPIO)==HIGH);
38    do { delayMicroseconds(1); } while(digitalRead(DHT_GPIO)==LOW);
39    do { delayMicroseconds(1); } while(digitalRead(DHT_GPIO)==HIGH);
40    // Remember the highs, ignore the lows -- a good philosophy!
41
42
43    for(int d=0; d<5; d++) {       // for each data byte
44       // read 8 bits
45       for(int i=0; i<8; i++) {    // for each bit of data
46          do { delayMicroseconds(1); } while(digitalRead(DHT_GPIO)==LOW);
47          int width = 0;           // measure width of each high
48          do {
49             width++;
50             delayMicroseconds(1);
51             if(width>1000) break; // missed a pulse -- data invalid!
52          } while(digitalRead(DHT_GPIO)==HIGH);    // time it!
53          // shift in the data, msb first if width > the threshold
54          data[d] = data[d] | ((width > LH_THRESHOLD) << (7-i));
55       }
56    }
57
58    if (USING_DHT11){
59       humid = data[0] * 10;            // one byte - no fractional part
60       temp = data[2] * 10;             // multiplying to keep code concise
61    }
62    else {                              // for DHT22 (AM2302/AM2301)
63       humid = (data[0]<<8 | data[1]);  // shift MSBs 8 bits left and OR LSBs
64       temp = (data[2]<<8 | data[3]);   // same again for temperature
65    }
66    unsigned char chk = 0;   // the checksum will overflow automatically
67
68     for(int i=0; i<4; i++){ chk+= data[i]; }
69    if(chk==data[4]){
70       cout << "The checksum is good" << endl;
71       cout << "The temperature is " << (float)temp/10 << "°C" << endl;
72       cout << "The humidity is " << (float)humid/10 << "%" << endl;
73
74     if((float)temp/10 > 24){
75         digitalWrite(LED_R,HIGH);
76         digitalWrite(LED_G,LOW);
77         digitalWrite(LED_B,LOW);
78          }
79     else {
80         digitalWrite(LED_R,LOW);
81         digitalWrite(LED_G,HIGH);
82         digitalWrite(LED_B,LOW);
83          }
84  delay(2000);
85 }
86     else{
87         usleep(2000000);
88         goto TRYAGAIN;
89     }
90
91
92 }
93 }

'Hardware > Raspberry Pi' 카테고리의 다른 글

초음파센서로 RGB 모듈 제어  (0) 2021.04.08
LED 번갈아 페이딩  (0) 2021.04.07
디바운싱을 이용한 LED 제어  (0) 2021.04.07