LED幻彩燈編程 |
發布時間:2024-05-29 11:50:37 |
Arduino 代碼 ```c++ #include #define LED_COUNT 10 #define LED_PIN 6 Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); const uint32_t colors[] = { strip.Color(255, 0, 0), // Red strip.Color( 0, 255, 0), // Green strip.Color( 0, 0, 255), // Blue strip.Color(255, 255, 0), // Yellow strip.Color( 0, 255, 255), // Cyan strip.Color(255, 0, 255), // Magenta strip.Color(255, 255, 255) // White }; unsigned long lastMillis = 0; int colorIndex = 0; void setup() { strip.begin(); strip.show(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - lastMillis > 500) { colorIndex = (colorIndex + 1) % (sizeof(colors) / sizeof(colors[0])); strip.fill(colors[colorIndex], 0, LED_COUNT); strip.show(); lastMillis = currentMillis; } } ``` 說明:
如何使用: 1. 導入 Adafruit_NeoPixel 庫。 2. 定義您的 LED 數量和引腳(jiao)。 3. 創建(jian)一個(ge) Adafruit_NeoPixel 對象來(lai)控制 LED。 4. 定義一(yi)個顏色數組(zu),其(qi)中包含(han)您希望(wang) LED 顯示(shi)的不(bu)同顏色。 5. 初始化(hua)串行(xing)監視器(可選(xuan))以(yi)打印調試信息。 6. 在主(zhu)循環(huan)中(zhong),使用時間來(lai)循環(huan)瀏覽顏色(se)并(bing)更新 LED。 此代(dai)碼將創(chuang)建幻彩燈(deng)效果,其中 LED 會緩(huan)慢(man)地循環顯示(shi)不同(tong)的顏色。 |