#include <Adafruit_NeoPixel.h> // 引入 Adafruit NeoPixel 函式庫
#define PIN_NEO_PIXEL 16 // 定義 ESP32 的 GPIO16 連接到 NeoPixel 的引腳
#define NUM_PIXELS 30 // 定義 NeoPixel LED 燈條上的 LED (像素) 數量
Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800); // 建立 NeoPixel 物件
Adafruit_NeoPixel ring = Adafruit_NeoPixel(16, 9); // 建立另一個 NeoPixel 物件 (16 = LED 數量, 9 = Pin)
int val = 0;
int speed = 0;
int colorIndex = 0; // 用於追蹤顏色索引
void setup() {
NeoPixel.begin(); // 初始化 NeoPixel 燈條物件 (必須)
pinMode(12, INPUT);
Serial.begin(115200);
ring.begin();
ring.setBrightness(255); // 設定亮度範圍 0 - 255
ring.show(); // 顯示設定的亮度
}
void loop() {
NeoPixel.clear(); // 將所有像素的顏色設為 '關閉'
// 逐個將像素設定為不同的顏色,並在每個像素之間加入延遲
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
NeoPixel.setPixelColor(pixel, getColor(colorIndex));
NeoPixel.show();
Serial.println(analogRead(12));
val = analogRead(12);
speed = map(val, 0, 4095, 10, 500);
delay(speed);
}
// 更新顏色索引,以便下一圈顯示不同的顏色
colorIndex = (colorIndex + 1) % 7;
// 等待一段時間,然後繼續下一圈
delay(1000);
}
// 取得不同的顏色
uint32_t getColor(int index) {
switch (index) {
case 0:
return NeoPixel.Color(255, 0, 0); // 紅色
case 1:
return NeoPixel.Color(255, 128, 0); // 橙色
case 2:
return NeoPixel.Color(255, 255, 0); // 黃色
case 3:
return NeoPixel.Color(0, 255, 0); // 綠色
case 4:
return NeoPixel.Color(0, 255, 255); // 青色
case 5:
return NeoPixel.Color(0, 0, 255); // 藍色
case 6:
return NeoPixel.Color(255, 0, 255); // 紫色
default:
return NeoPixel.Color(0, 0, 0); // 黑色
}
}