3색 RGB LED는 한개의 단자안에서 R,G,B색의 비율을 조절해서 다양한 색을 표현 할 수 있는 LED입니다.
3색 RGB 단자는 R,G,B 핀과 1개의 공통 단자로 이루어져있습니다.
공통단자가 (-) 이면 Common Cathode, (+)이면 Common Anode라고 합니다.
단자의 외견만 봐서는 Cathode인지 Anode인지 판단 할 수가 없습니다. (구매하실 때 꼭 확인.....but 대부분은 Cathode, 우리도 Cathode)
그림에서 보듯이 가장 긴 핀이 공통단자입니다. 공통단자를 기준으로 2개 있는 쪽이 G,B 1개있는쪽이 R입니다.
실습 1 : 2초마다 빨강,초록,파랑색이 켜지도록 해보세요.
(저항을 공통단자쪽에 설치하면 1개로도 충분합니다.)
int red=13;
int green=12;
int blue=11;
void setup() {
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}
void loop() {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(2000);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
delay(2000);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
delay(2000);
}
문제1 : analogWrite()를 이용해서 여러가지 색을 만들어보세요.
(http://www.n2n.pe.kr/lev-1/color.htm) 참고 또는 구글에 rgb 표 검색
아두이노에서는 14개의 디지털 핀중 6개(3,5,6,9,10,11)를 PWM 신호로 사용할 수 있습니다.
위의 회로도에서 rgb핀만 바꿔서 만들어보세요.
(BeautifulSoup를 통해 rgb표값을 배열로 얻었습니다. 여러분도 BeautifulSoup를 통해 직접 배열을 얻어보세요.)
int redColors[]={000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,255,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255};
int greenColors[]={000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,153,204,255,000,051,102,153,204,255,000,051,102,102,153,204,255,000,051,102,153,204,255,000,051};
int blueColors[]={000,051,102,153,204,255,051,000,000,000,000,000,102,102,051,051,051,051,153,153,153,102,102,102,204,204,204,204,153,153,255,255,255,255,255,204,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,000,051,102,153,204,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,255,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,051,102,153,204,255,000,051,102,153,204,255,000,102,153,204,255,000,051,204,255,000,051,102,153,204,255,000,051,102,153,153,204,255,000,051,102};
int red=11;
int green=10;
int blue=9;
int redColors[]={000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,255,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255};
int greenColors[]={000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,153,204,255,000,051,102,153,204,255,000,051,102,102,153,204,255,000,051,102,153,204,255,000,051};
int blueColors[]={000,051,102,153,204,255,051,000,000,000,000,000,102,102,051,051,051,051,153,153,153,102,102,102,204,204,204,204,153,153,255,255,255,255,255,204,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,000,051,102,153,204,204,255,000,051,102,153,153,204,255,000,051,102,102,153,204,255,000,051,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,204,255,000,051,102,153,255,255,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,255,000,051,102,153,204,051,102,153,204,255,000,102,153,204,255,000,051,153,204,255,000,051,102,051,102,153,204,255,000,051,102,153,204,255,000,102,153,204,255,000,051,204,255,000,051,102,153,204,255,000,051,102,153,153,204,255,000,051,102};
void setup() {
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}
void loop() {
for(int i=0; i<sizeof(redColors)/sizeof(redColors[0]) ; i++){
analogWrite(red,redColors[i]);
analogWrite(green,greenColors[i]);
analogWrite(blue,blueColors[i]);
delay(500);
}
}
(이론상으론 모든색이 나와야하지만 3색rgb 내의 각 색상의 위치때문에 생각보다 많은 색이 보이지않습니다. 보는 위치에 따라 색이 다를 수 있으니 앞뒤옆면에서 살펴봅시다.)
실습 2 : 스위치를 누를 때마다 색이 r,g,b 색이 나오도록 해보세요
int btn=5;
int red=13;
int green=12;
int blue=11;
boolean lastBtn=LOW;
boolean currentBtn=LOW;
#define DEBOUNCING_DELAY 5
void setup() {
// put your setup code here, to run once:
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(btn,INPUT);
}
boolean buttonInputChattering(){
boolean current=digitalRead(btn);
if(current!=lastBtn){ //현재읽은게 전상황이랑 다르면.....
delay(DEBOUNCING_DELAY);
current=digitalRead(btn); //5ms 뒤 다시 읽음
}
return current; //다시 읽은 입력상태 return
}
void onlyOneLedOn(int led){
if(led==red){
digitalWrite(red,HIGH);
digitalWrite(green,LOW);
digitalWrite(blue,LOW);
}else if(led==green){
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
digitalWrite(blue,LOW);
}else if(led==blue){
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(blue,HIGH);
}else if(led==0){
digitalWrite(red,LOW);
digitalWrite(green,LOW);
digitalWrite(blue,LOW);
}
}
int count=0;
void loop() {
currentBtn=buttonInputChattering(); //현재 btn은 5ms 후의 읽은 값이 현재 버튼 상태
if(currentBtn==HIGH && lastBtn==LOW){ // 전버튼은 low, 현재버튼 HIGH ==> 버튼 누른것으로 간주
count++;
switch (count%4){
case 1 : onlyOneLedOn(red);break;
case 2 : onlyOneLedOn(green);break;
case 3 : onlyOneLedOn(blue);break;
default :onlyOneLedOn(0);break;
}
}
lastBtn=currentBtn;
}