// --------------------------------------------------------------------------------
/*

13 NeoPixel LED Ring 彩虹特效

[學習重點]

1. 更精簡的彩虹色計算法

[挑戰]

- 自由發揮


Created by Jason on 13 Aug 2022.

*/
// --------------------------------------------------------------------------------

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel ring = Adafruit_NeoPixel(32, 9); // 建立物件 (32 = LED 數量, 9 = Pin)

void setup() {
  ring.begin();
  ring.setBrightness(255); // 設定亮度 0 - 255
  ring.show(); // 將設定顯示出來
}

void loop() {
  rainbowCycle(10); // 彩虹滾動圈圈
}

// 彩虹滾動圈圈
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);
}