RGB LED에서 Red-> Yellow->Green-> Cyan->Blue -> Magenta 순으로 나타내고 싶다.
/*7.4 control RGB cookbook code 1*/ const int redPin = 3; const int greenPin = 5; const int bluePin = 6; const boolean invert = true; int color = 0; int R, G, B; void setup() { } void loop() { int brightness = 255; hueToRGB( color, brightness); analogWrite(redPin, R); analogWrite(greenPin, G); analogWrite(bluePin, B); color ++; if(color > 255) color = 0; delay(10); } void hueToRGB(int hue, int brightness) { unsigned int scaledHue = (hue*6); unsigned int segment = scaledHue / 256; unsigned int segmentOffset = scaledHue - (segment*256); unsigned int complement = 0; unsigned int prev = (brightness * (255 - segmentOffset)) / 256; unsigned int next = (brightness * segmentOffset) / 256; if(invert) { brightness = 255-brightness; complement = 255; prev = 255-prev; next = 255-next; } switch(segment){ case 0: R = brightness; G = next; B = complement; break; case 1: R = prev; G = brightness; B = complement; break; case 2: R = complement; G = brightness; B = next; break; case 3: R =complement; G = prev; B = brightness; break; case 4: R = next; G = complement; B = brightness; break; case 5: R = brightness; G = complement; B = prev; break; } } /*2 my RGB code*/ int color = 0; const int redPin= 3, greenPin = 5, bluePin = 6; int R, G, B; const boolean invert = true; void setup() { Serial.begin(9600); } void loop() { //dicide each brightness of RGB brightOfRGB( color); analogWrite(redPin, R); analogWrite(greenPin, G); analogWrite(bluePin, B); color ++; if(color > 255) color = 0; delay(10); } void brightOfRGB(int color) { //dicide the color long domain = color*6; long segment = domain/256; long segmentOffset = domain-segment*256; //value of each kind of brightness long brightness=150; long prev=(150*(255-segmentOffset))/255; long next = (150*segmentOffset)/255; int complement =0; //because of common anode if(invert) {brightness = 255 - brightness; complement = 255 - complement; next = 255 - next; prev = 255 - prev;} //output each brightness from color switch(segment) { //RED case 0: R = brightness; G = next; B = complement; break; //YELLOW case 1 : R = prev; G = brightness; B = complement; break; //GREEN case 2: R = complement; G = brightness; B = next; break; //CYAN case 3: R = complement; G = prev; B = brightness; break; //BLUE case 4: R = next; G = complement; B = brightness; break; //MAGENTA case 5: R = brightness; G = complement; B = prev; break; } /*3 analogcode reference*/ const int redPin= 3, greenPin = 5, bluePin = 6; int R=255, G=0, B=0; int r=0, g=0, b=0; void setup() { Serial.begin(9600); } void loop() { if(R ==255) {r= -1; g=1; b=0;} if(G==255) {r=0; g=-1; b=1;} if(B==255) {r=1; g=0; b=-1;} R +=r; G +=g; B+=b; Serial.println(R); analogWrite(redPin, 255-R); analogWrite(greenPin, 255-G); analogWrite(bluePin, 255-B); delay(1); }
[출처] [아날로그 출력] - RGB|작성자 DEW
'아두이노 > 출력' 카테고리의 다른 글
LED 11개를 이용해서, 빛이 중심에서 갈라졌다 모였다 하기 (0) | 2016.02.08 |
---|---|
LED 6를 사용해서, 빛이 좌우로 움직이게 하기 (0) | 2016.02.08 |
아날로그 출력 밝기 이동시키기 (0) | 2016.02.08 |
기본 1개짜리 아날로그 출력 (0) | 2016.02.08 |
기본 출력 digitalWrite() (0) | 2016.02.08 |