/* NeoPixel LED Ring 彩虹特效
202208 - 13 NeoPixel LED Ring 彩虹特效
Created by Jason on 13 Aug 2022.
https://wokwi.com/projects/338857849104892500
*/
#include <Adafruit_NeoPixel.h>
#define DATA_PIN 13
#define NUMPIXELS 24 // NeoPixel ring size
#define pixDELAY 10 // Delay for pixel persistence
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel ring = Adafruit_NeoPixel(NUMPIXELS, DATA_PIN, NEO_GRB + NEO_KHZ800); // 建立物件 (24 = LED 數量, 13 = Pin)
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
void setup() {
ring.begin();
ring.setBrightness(255); // 設定亮度 0 - 255
ring.show(); // 將設定顯示出來
}
void loop() {
rainbowCycle(pixDELAY); // 彩虹滾動圈圈
}
// 彩虹滾動圈圈
void rainbowCycle(int wait) {
int i, j;
for (j = 0; j < 256 * 5; j++) {
for (i = 0; i < ring.numPixels(); i++) {
ring.setPixelColor(i, Wheel(((i * 256 / ring.numPixels()) + j) & 255));
}
ring.show();
delay(wait);
}
}
// 產生漸變顏色值
int32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return ring.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return ring.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return ring.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}