RGB LED는 빨강, 초록, 파랑 빛의 삼원색을 조절하여 다양한 색을 출력하는 LED이다.

 

RGB LED를 용해 다양한 색을 표현해 본다.

 

※ 명령어

  • analogWrite(핀번호, 값) : 정해진 핀에 아날로그 출력을 한다. '값' 에는 0~255의 값을 넣는다.
const int RedLed = 3;
const int GreenLed = 5;
const int BlueLed = 6;
void setup() {
  ledOutput(255,0,0);  //R
  delay(1000);
  ledOutput(0,255,0);  //G
  delay(1000);
  ledOutput(0,0,255);  //B
  delay(1000);

}

void loop() {
  for(int i=0;i<256;i++){
    ledOutput(255,i,0); //빨강만 켠 상태에서 녹색 밝기 조절
    delay(10);
  }

  for(int i=0;i<256;i++){
    ledOutput(0,255,i); //녹색만 켠 상태에서 파랑 밝기 조절
    delay(10);
  }

  for(int i=0;i<256;i++){
    ledOutput(i,i,255); //파랑만 켠 상태에서 빨강 밝기 조절
    delay(10);
  }

  for(int i=0;i<256;i++){
    ledOutput(i,255,255); //녹색,파랑만 켠 상태에서 빨강 밝기 조절
    delay(10);
  }

  for(int i=0;i<256;i++){
    ledOutput(255,i,255); //빨강,파랑만 켠 상태에서 녹색 밝기 조절
    delay(10);
  }

  for(int i=0;i<256;i++){
    ledOutput(255,255,i); //빨강,파랑만 켠 상태에서 파랑 밝기 조절
    delay(10);
  }
}

void ledOutput(int Red, int Green, int Blue) {  // LED 켜는 함수
  analogWrite(RedLed,Red);
  analogWrite(GreenLed,Green);
  analogWrite(BlueLed,Blue);
}

 

 

+ Recent posts