#include "FastLED.h"
#define NUM_LEDS 216
#define DATA_PIN 6
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
uint8_t max_bright = 120;
int leds_count1 = 8;
int leds_count2 = 16;
int leds_count3 = 24;
int leds_count4 = 35;
int leds_count5 = 45;
CRGBPalette16 myColorPalette = CRGBPalette16(
CRGB::CRGB(255, 255, 0), CRGB::Black );
int brightness_delay = 2;
int shock_sensor_pin = 2;
int light_sensor_pin = 3;
int shock_sensor_value = 0;
int light_sensor_value = 0;
int work_state = 0;
int ripple_leds_brightness_steps = 5; // 涟漪效果的亮度变化步长,越大越快,越小越慢,可调节
int all_leds_brightness_steps = 5; // 闪烁效果的亮度变化步长,越大越快,越小越慢,可调节
void setup()
{
Serial.begin(9600);
pinMode(shock_sensor_pin, INPUT);
pinMode(light_sensor_pin, INPUT);
LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(max_bright);
FastLED.clear();
FastLED.show();
}
void loop()
{
if(work_state == 0)
{
shock_sensor_value = digitalRead(shock_sensor_pin);
if(shock_sensor_value == HIGH)
{
all_leds_on();
work_state = 1;
}
}
else if(work_state == 1)
{
light_sensor_value = digitalRead(light_sensor_pin);
if(light_sensor_value == HIGH)
{
double start_time = millis();
ripple_leds();
double end_time = millis();
Serial.println((end_time - start_time) / 1000.0);
work_state = 2;
while(1){}
}
}
}
void ripple_leds()
{
delay(1000);
leds2_off();
delay(300);
leds2_on();
delay(300);
leds1_off();
delay(300);
leds1_on();
leds2_off();
delay(300);
leds2_on();
leds3_off();
delay(300);
leds3_on();
leds4_off();
delay(300);
leds4_on();
leds5_off();
delay(300);
leds5_on();
delay(300);
leds5_off();
delay(300);
leds5_on();
leds4_off();
delay(300);
leds4_on();
leds3_off();
delay(300);
leds3_on();
leds2_off();
delay(300);
leds2_on();
leds1_off();
delay(300);
leds1_on();
delay(300);
leds1_off();
delay(300);
leds1_on();
leds2_off();
delay(300);
leds2_on();
leds3_off();
delay(300);
leds3_on();
leds4_off();
delay(300);
leds4_on();
leds5_off();
delay(300);
leds5_on();
delay(300);
for(int i = 0; i < 5; i++)
{
all_leds_off();
delay(1550);
all_leds_on();
delay(2200);
}
all_leds_off();
}
void all_leds_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=all_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds, NUM_LEDS, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void all_leds_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=all_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds, NUM_LEDS, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds1_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds, leds_count1, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds1_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds, leds_count1, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds2_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1, leds_count2, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds2_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1, leds_count2, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds3_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2, leds_count3, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds3_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2, leds_count3, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds4_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2 + leds_count3, leds_count4, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds4_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2 + leds_count3, leds_count4, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds5_on()
{
for(int brightness = 0; brightness < max_bright; brightness+=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2 + leds_count3 + leds_count4, leds_count5, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}
void leds5_off()
{
for(int brightness = max_bright; brightness >= 0; brightness-=ripple_leds_brightness_steps)
{
brightness = constrain(brightness, 0, max_bright);
fill_palette (leds + leds_count1 + leds_count2 + leds_count3 + leds_count4, leds_count5, 0, 0, myColorPalette, brightness, LINEARBLEND );
FastLED.show();
//delay(brightness_delay);
}
}