exercise
더 쉽게 더 화려하고 멋진 빛을 만들어 봅시다
material
WS2812 기반 LED 스틱( or 스트립)
circuit
![](http://mimlab.net/wp-content/uploads/2022/01/7.더쉽고더멋진빛만들기-1024x148.png)
code
#include <FastLED.h>
#define NUM_LEDS 8 //LED 갯수
#define DATA_PIN 3 //Data Pin
#define CLOCK_PIN 13
CRGB leds[NUM_LEDS];
int counter = 0;
int h = 0;
int range = 0;
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
//1개 단순 켜고 끄기
//leds[0] = CRGB::Red;
leds[0] = CRGB(255, 0, 0);
//leds[0].setRGB(255, 0, 0);
FastLED.show();
delay(500);
//leds[0] = CRGB::Black;
leds[0] = CRGB(0, 0, 0);
//leds[0].setRGB(0, 0, 0);
FastLED.show();
delay(500);
//순서대로 켜기 반복
for(int i=0; i<NUM_LEDS; i++) {
leds[i].setRGB(126, 0, 126);
FastLED.show();
delay(50);
leds[i].setRGB(0, 0, 0);
}
//순서대로 켜기 반복 + 서서히 꺼지게 하기
counter++;
if(counter == NUM_LEDS) counter=0;
leds[counter].setRGB(255, 0, 0);
FastLED.show();
for(int i=0; i<NUM_LEDS; i++) {
leds[i].fadeToBlackBy(192); //ratio to 255
FastLED.show();
}
delay(50);
//색상(hue) 변화시켜서 무지개 만들기
counter++;
if(counter == NUM_LEDS) counter=0;
h += 10;
leds[counter] = CHSV(h, 255, 255);
FastLED.show();
delay(50);
//내장 함수로 무지개 만들기
fill_rainbow(leds, NUM_LEDS, range, 25);
FastLED.show();
delay(10);
range += 5;
if(range > 255) range = 0;
}
further exercise
[고급] 외부전원을 연결하여 긴 LED 스트립에 무지개 애니메이션 만들어 봅시다