R,G,B 에 220옴을 연결하고 그라운드를 -(Cathod) 를 연결할 예정
int Red = 11; int Green = 5; int Blue = 3;
//11번 핀은 레드, 5번 핀은 그린, 3번 핀은 블루
void setup()
{
//핀 기능 : 핀을 입력으로 쓸지 출력으로 쓸지
// 3,5,11번 핀을 출력으로 쓰겠음
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
analogWrite(Red, 255);
// Red(11번 핀)에 255를 보낸다.
analogWrite(Green, 0);
analogWrite(Blue, 0);
delay(1500);
// 1.5초간 시간 지연
analogWrite(Red, 0);
analogWrite(Green, 255);
analogWrite(Blue, 0);
delay(1500);
analogWrite(Red, 0);
analogWrite(Green, 0);
analogWrite(Blue, 255);
delay(1500);
// 1.5초간 시간 지연
}
int Red = 11; int Green = 5; int Blue = 3;
int Rmag = 0; int Gmag = 0; int Bmag = 0;
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
for(Rmag = 0; Rmag < 256 ; Rmag =Rmag+63) {
for(Gmag = 0; Gmag < 256 ; Gmag =Gmag+63) {
for(Gmag = 0; Gmag < 256 ; Gmag =Gmag+63) {
analogWrite(Red, Rmag);
analogWrite(Green, Gmag);
analogWrite(Blue, Bmag);
}
}
}
}
int Red 11; int Green = 5 ; int Blue = 3;
void setcolor(int r, int g, int b, int time)
{
analogWrite(Red, r)
analogWrite(Green, g)
analogWrite(Blue, b)
delay(time);
}
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
setcolor(255,0,0,1000);
setcolor(0,255,0,1000);
setcolor(0,0,255,1000);
setcolor(255,255,0,1000);
setcolor(255,0,255,1000);
setcolor(0,255,255,1000);
}
int Red 11; int Green = 5 ; int Blue = 3;
void setcolor(int r, int g, int b, int time)
{
analogWrite(Red, r)
analogWrite(Green, g)
analogWrite(Blue, b)
delay(time);
}
void setup()
{
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
for(int i = 9; i < 100 ; i ++) {
setcolor(random(200,255),random(0,50),random(0,50), random(0,1000);
setcolor(random(0,50),random(200,255),random(0,50), random(0,1000);
setcolor(random(0,50),random(0,50),random(200,255), random(0,1000);
}
}
직렬 통신을 활용한 RGB LED
직렬통신 : 데이터를 주고 받을 때 1개씩 전달함
병렬통신 : 데이터를 주고 받을 때 여러 개를 한 번에 전달함
아두이노에서 다루는 데이터가 많지 않아 직렬통신 이용할 예정
Serial.begin(9600) ; // Serial.begin(통신속도 buad rate)
1.boaud rate : 심볼 수 기준
2.bit per secont : 비트 수를 기준으로 (32bit, 64bit)
Serial.print(데이터값); // 아두이노 -> 컴퓨터
Serial.println(데이터값) // 띄어쓰기 추가.
Serial.available(); // 아두이노에 전송된 데이터의 바이트 길이
Serial.available() == 0; // 수신된 데이터 없음
Serial.available() > 0; // 수신된 데이터 있음
while(Seril.available() > 0 ) {
//전송된 데이터가 있는 경우
데이터 처리 1;
데이터 처리 2;
}
r = Serial.parseInt();
g = Serial.parseInt();
b = Serial.parseInt();
수신된 데이터를 읽어오느 오는 함수
if(Serial.read() == '\n') {
func1();
func2();
}
int r = 0; int g = 0; int b = 0;
int Red = 11; int Green = 5; int Blue = 3;
void setcolor(int r, int g, int b)
{
analogWrite(Red, r);
analogWrite(Green, g);
analogWrite(Blue, b)
}
Ex
int r = 0 , int g = 0, int b = 0;
int Red = 11;, int Green = 5;, int Blue = 3;
void setcolor(int r, int g, int b)
{
analogWrite(Red, r);
analogWrite(Green, g);
anlaogWrite(Blue, b);
}
void setup()
{
Serial.begin(9600); // 직렬 통신 시작
PinMode(3, OUTPUT);
PinMode(5, OUTPUT);
PinMode(11, OUTPUT);
}
void loop()
{
while(Serial.avialable() > 0)
{
r = Serial.parseInt();
g = Serial.parseInt();
b = Serial.parseInt();
if(Serial.read() == '\n')
{
setcolor(r,g,b);
Serial.print("Red : ");
Serial.print(r);
Serial.print(", Green : ");
Serial.print(g);
Serial.print(", Blue : ");
Serial.println(b);
}
}
}