写一段Arduino代码,实现用按键开关依次切换ws2812灯带的九种模式,九种模式均为白光,区别为照度和闪烁频率
时间: 2024-03-23 21:36:00 浏览: 45
基于阿里云、小程序、Arduino的WS2812灯控系统.zip
5星 · 资源好评率100%
好的,下面是实现你要求的Arduino代码,使用了Adafruit公司的neopixel库:
```Arduino
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_LEDS 30
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int mode = 0;
void setup() {
strip.begin();
strip.show(); // Set all pixel colors to 'off'
pinMode(2, INPUT_PULLUP);
}
void loop() {
if (digitalRead(2) == LOW) {
mode++;
if (mode > 8) {
mode = 0;
}
delay(100);
}
switch (mode) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 255, 255, 255);
}
strip.show();
delay(500);
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 127, 127, 127);
}
strip.show();
delay(500);
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 63, 63, 63);
}
strip.show();
delay(500);
break;
case 3:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 31, 31, 31);
}
strip.show();
delay(500);
break;
case 4:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 15, 15, 15);
}
strip.show();
delay(500);
break;
case 5:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 7, 7, 7);
}
strip.show();
delay(500);
break;
case 6:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 3, 3, 3);
}
strip.show();
delay(500);
break;
case 7:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 1, 1, 1);
}
strip.show();
delay(500);
break;
case 8:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 255, 255, 255);
}
strip.show();
delay(50);
strip.setBrightness(50);
strip.show();
delay(50);
strip.setBrightness(255);
strip.show();
delay(50);
break;
}
}
```
这段代码使用按键开关切换九种模式,每种模式都是白光,区别在于照度和闪烁频率。当按下按键时,模式会依次切换,直到最后一个模式后重新回到第一个模式。
阅读全文