프라모델

프라모델 LED 작업을 위한 준비 - HC-06 블투투스 모듈을 통한 LED Strip 제어하기

여우래비 2020. 11. 14. 19:39
반응형

 

자, 이제 이번 예제만 마치면, 블루투스를 통해 LED Strip 제어를 나름 뽀대나게 할 수 있다.

아래의 작동 영상을 먼저 보자.

기본 동작은 아래와 같다.

 

ㅇ 모든 동작은 블루투스 앱의 "1" 만을 사용하여 토글

ㅇ 처음 "1" 선택시 적색이 서서히 켜졌다가 서서히 꺼짐을 반복

ㅇ 다음 "1" 선택시 녹색이 서서히 켜졌다가 서서히 꺼짐을 반복

ㅇ 다음 "1" 선택시 청색이 서서히 켜졌다가 서서히 꺼짐을 반복

ㅇ 다음 "1" 선택시, 1번 LED 부터 (위 그림의 맨 오른쪽) 색상을 변경하며 순차적으로 서서히 켜졌다가 서서히 꺼짐을 반복

ㅇ 다음 "1" 선택시, 모든 LED 가 적색으로 켜졌다가 서서히 꺼짐

 

회로를 아래와 같이 꾸미고, 소스코드를 아두이노로 업로드 해보자.

(LED Strip 은 LED 5개로 자른 것을 사용했으나, 편의상 8개 짜리로 회로를 꾸몄다.)

 

아래는 소스 코드이다.

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define dataPin 6  // 아두이노 6번핀을 Data 용으로 사용
int currentMode = -1;
SoftwareSerial serialBT(23); // TX, RX
Adafruit_NeoPixel ledstripTest = Adafruit_NeoPixel(5, dataPin, NEO_GRB + NEO_KHZ800);
 
void setup() {
  ledstripTest.begin();
  shutAllLedStrip();
  
  // 아두이노의 시리얼모니터에 표시
  Serial.begin(9600);
  Serial.println("Test Start");
  // 휴대폰 블루투스 앱에 표시
  serialBT.begin(9600);
  serialBT.println("Test Start");
}
 
void loop() {
  if(detectBT()) {
    Serial.println(currentMode);
    if(currentMode == 0) {
      shutAllLedStrip();
      Serial.println("Red Breathing mode");
      serialBT.println("Red Breathing mode");
      breathRed();
    }
    else if(currentMode == 1) {
      shutAllLedStrip();
      Serial.println("Green Breathing mode");
      serialBT.println("Green Breathing mode");
      breathGreen();
    }
    else if(currentMode == 2) {
      shutAllLedStrip();
      Serial.println("Blue Breathing mode");
      serialBT.println("Blue Breathing mode");
      breathBlue();
    }
    else if(currentMode == 3) {
      shutAllLedStrip();
      Serial.println("Sequencing Breathing mode");
      serialBT.println("Sequencing Breathing mode");
      breathSequence();
    }
    else if(currentMode == 4) {
      shutAllLedStrip();
      Serial.println("LED off");
      serialBT.println("LED off");
      shutDown();
      currentMode = 0;
    }
  }
}
 
void shutAllLedStrip() {
  for(int i=0; i<5; i++) {
    ledstripTest.setPixelColor(i, ledstripTest.Color(000));
  }
    ledstripTest.show();
}
 
bool detectBT() {
  // HC-06 의 신호를 계속 읽고 아두이노 시리얼모니터로 읽은 신호 전송
  if (serialBT.available()) {
    char data = serialBT.read();
    if(data == '1') {
      if(currentMode < 0) currentMode = 0;
      return true;
    }
    else if(data == '2') {
      shutAllLedStrip();
      currentMode = -1;
    }
  }
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available()) {
      serialBT.write(Serial.read());
  }
   return false;
}
 
void breathRed() {
  //int blue=0;
  while (1){
    for (int i=0; i<=125; i++) {
      ledstripTest.setPixelColor(0, i, 00);
      ledstripTest.setPixelColor(1, i, 00);
      ledstripTest.setPixelColor(2, i, 00);
      ledstripTest.setPixelColor(3, i, 00);
      ledstripTest.setPixelColor(4, i, 00);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
    for (int i=125; i>=0; i--) {
      ledstripTest.setPixelColor(0, i, 00);
      ledstripTest.setPixelColor(1, i, 00);
      ledstripTest.setPixelColor(2, i, 00);
      ledstripTest.setPixelColor(3, i, 00);
      ledstripTest.setPixelColor(4, i, 00);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
  }
    currentMode = 1;
}
 
void breathGreen() {
  while (1){
    for (int i=0; i<=125; i++) {
      ledstripTest.setPixelColor(00, i, 0);
      ledstripTest.setPixelColor(10, i, 0);
      ledstripTest.setPixelColor(20, i, 0);
      ledstripTest.setPixelColor(30, i, 0);
      ledstripTest.setPixelColor(40, i, 0);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
    for (int i=125; i>=0; i--) {
      ledstripTest.setPixelColor(00, i, 0);
      ledstripTest.setPixelColor(10, i, 0);
      ledstripTest.setPixelColor(20, i, 0);
      ledstripTest.setPixelColor(30, i, 0);
      ledstripTest.setPixelColor(40, i, 0);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
  }
    currentMode = 2;
}
 
void breathBlue() {
  while (1){
    for (int i=0; i<=125; i++) {
      ledstripTest.setPixelColor(000, i);
      ledstripTest.setPixelColor(100, i);
      ledstripTest.setPixelColor(200, i);
      ledstripTest.setPixelColor(300, i);
      ledstripTest.setPixelColor(400, i);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
    for (int i=125; i>=0; i--) {
      ledstripTest.setPixelColor(0, i, 00);
      ledstripTest.setPixelColor(000, i);
      ledstripTest.setPixelColor(100, i);
      ledstripTest.setPixelColor(200, i);
      ledstripTest.setPixelColor(300, i);
      ledstripTest.setPixelColor(400, i);
      delay (2);
      ledstripTest.show();
    }
    if (serialBT.available()) break;
  }
    currentMode = 3;
}
 
void breathSequence() {
  while (1){
    for (int i=0; i<=25; i++) {
      ledstripTest.setPixelColor(000, i*5);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(10, i*50);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(2, i*500);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(30, i*50);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(400, i*5);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      //ledstripTest.show();
      if (serialBT.available()) break;
    }
    if (serialBT.available()) break;
    for (int i=25; i>=0; i--) {
      ledstripTest.setPixelColor(000, i*5);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(10, i*50);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(2, i*500);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(30, i*50);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      ledstripTest.setPixelColor(400, i*5);
      ledstripTest.show();
      delay (20);
      shutAllLedStrip();
      if (serialBT.available()) break;
    }
    if (serialBT.available()) break;
  }
    currentMode = 4;
}
 
void shutDown() {
    for (int i=125; i>=0; i--) {
      ledstripTest.setPixelColor(0, i, 00);
      ledstripTest.setPixelColor(1, i, 00);
      ledstripTest.setPixelColor(2, i, 00);
      ledstripTest.setPixelColor(3, i, 00);
      ledstripTest.setPixelColor(4, i, 00);  
      delay (10);
      ledstripTest.show();
    }
}
cs

 

이제, 본인의 상상대로 코딩하면 된다.

 

이번 포스트 끝.

반응형