#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 23
#define BUTTON_PIN 16
// 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 strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
// 初始化亮度
int brightness = 100;
// 上一次按钮状态
int lastButtonState = HIGH;
void adjustBrightness() {
int buttonState = digitalRead(BUTTON_PIN);
// 如果按钮被按下并且上一次未被按下,则调整亮度
if (buttonState == LOW && lastButtonState == HIGH) {
brightness -= 20;
if (brightness < 20) {
brightness = 100;
}
// 设置NeoPixel的亮度
strip.setBrightness(brightness);
strip.show();
}
// 更新上一次按钮状态
lastButtonState = buttonState;
}
// 递进渐变
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// 输入一个值0到255以获取颜色值
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
// 单色
void Color(uint32_t color) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
// 彩虹
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// 清空所有像素
void clear() {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 0);
}
strip.show();
}
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.begin();
strip.setBrightness(brightness);
strip.show(); // Initialize all pixels to 'off'
// 设置按钮引脚为输入
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
// 调整亮度函数
//adjustBrightness();
// 递进渐变效果
//colorWipe(strip.Color(255, 0, 0), 20); // 红色
//delay(500);
// 单色效果(修改Color函数的参数即可)
Color(strip.Color(0, 255, 0)); // 绿色
delay(500);
// 彩虹效果
//rainbow(20);
//delay(500);
// 清空所有像素
clear();
//delay(500);
}