#include <FastLED.h>
#define LED_PIN1 6
#define LED_PIN2 7
#define NUM_LEDS 31
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
int red_button = 2;
int blue_button = 3;
int red_button_count = 0;
int blue_button_count = 0;
int red_button_state = 0;
int blue_button_state = 0;
int last_red_button_state = HIGH;
int last_blue_button_state = HIGH;
unsigned long last_debounce_time_1 = 0; //上一次消抖时间1
unsigned long last_debounce_time_2 = 0; //上一次消抖时间2
unsigned long debounce_delay_1 = 20; //消抖延时1
unsigned long debounce_delay_2 = 20; //消抖延时2
int leds_index = 0;
int leds_increase_num = 5;
int red_index = 2;
int blue_index = 160;
void setup()
{
Serial.begin(9600);
FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds1, NUM_LEDS);
FastLED.addLeds<WS2812B, LED_PIN2, GRB>(leds2, NUM_LEDS);
pinMode(red_button, INPUT_PULLUP);
pinMode(blue_button, INPUT_PULLUP);
FastLED.clear();
FastLED.show();
}
void loop()
{
button_scan();
if(red_button_count + blue_button_count == 6)
{
delay(1000);
FastLED.clear();
FastLED.show();
if(red_button_count == 6 && blue_button_count == 0)
{
b1_red();
}
else if(red_button_count == 0 && blue_button_count == 6)
{
b1_blue();
}
else
{
red_button_count = 0;
blue_button_count = 0;
leds_index = 0;
FastLED.clear();
FastLED.show();
}
}
}
void button_scan()
{
int reading_1 = digitalRead(red_button);
int reading_2 = digitalRead(blue_button);
if (reading_1 != last_red_button_state)
{
last_debounce_time_1 = millis();
}
if (reading_2 != last_blue_button_state)
{
last_debounce_time_2 = millis();
}
if ((millis() - last_debounce_time_1) > debounce_delay_1)
{
if (reading_1 != red_button_state)
{
red_button_state = reading_1;
if (red_button_state == LOW)
{
red_button_count++;
Serial.print("red_button_count: ");
Serial.println(red_button_count);
fill_palette(leds1 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
leds_index += leds_increase_num;
FastLED.show();
}
}
}
if ((millis() - last_debounce_time_2) > debounce_delay_2)
{
if (reading_2 != blue_button_state)
{
blue_button_state = reading_2;
if (blue_button_state == LOW)
{
blue_button_count++;
Serial.print("blue_button_count: ");
Serial.println(blue_button_count);
fill_palette(leds1 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
leds_index += leds_increase_num;
FastLED.show();
}
}
}
last_red_button_state = reading_1;
last_blue_button_state = reading_2;
}
void b1_red()
{
leds_index = 0;
for(int i = 0 ; i < 6 ; i++)
{
fill_palette(leds1 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
FastLED.show();
delay(500);
fill_palette(leds1,30,red_index,0,RainbowColors_p, 0,LINEARBLEND);
fill_palette(leds2,30,red_index,0,RainbowColors_p, 0,LINEARBLEND);
FastLED.show();
delay(50);
leds_index += leds_increase_num;
}
leds_index = 0;
FastLED.clear();
FastLED.show();
for(int i = 0; i < 6; i++)
{
fill_palette(leds1 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,red_index,0,RainbowColors_p, 255,LINEARBLEND);
FastLED.show();
delay(500);
leds_index += leds_increase_num;
}
delay(1000);
FastLED.clear();
FastLED.show();
leds_index = 0;
red_button_count = 0;
blue_button_count = 0;
}
void b1_blue()
{
leds_index = 0;
for(int i = 0 ; i < 6 ; i++)
{
fill_palette(leds1 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
FastLED.show();
delay(500);
fill_palette(leds1,30,blue_index,0,RainbowColors_p, 0,LINEARBLEND);
fill_palette(leds2,30,blue_index,0,RainbowColors_p, 0,LINEARBLEND);
FastLED.show();
delay(50);
leds_index += leds_increase_num;
}
leds_index = 0;
FastLED.clear();
FastLED.show();
for(int i = 0; i < 6; i++)
{
fill_palette(leds1 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
fill_palette(leds2 + leds_index,5,blue_index,0,RainbowColors_p, 255,LINEARBLEND);
FastLED.show();
delay(500);
leds_index += leds_increase_num;
}
delay(1000);
FastLED.clear();
FastLED.show();
leds_index = 0;
red_button_count = 0;
blue_button_count = 0;
}