Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define DATA_PIN 6
- #define NUM_LEDS 120
- #define MAX_BRIGHTNESS 255
- #define BOTTOM_RIGHT 21
- #define BOTTOM_LEFT 51
- #define TOP_LEFT 81
- #define TOP_RIGHT 111
- #define debugPrint 1
- CRGB leds[NUM_LEDS];
- void setup()
- {
- Serial.begin(9600);
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(MAX_BRIGHTNESS);
- }
- void loop()
- {
- rain();
- }
- void trace(String s)
- {
- if(debugPrint)
- {
- Serial.print(s);
- }
- }
- void traceln()
- {
- if(debugPrint)
- Serial.print("\n");
- }
- void traceln(String s)
- {
- if(debugPrint)
- Serial.print(s);
- Serial.print("\n");
- }
- bool rain_pool_left = false;
- bool rain_pool_filled = false;
- bool rain_pool_filling = false;
- byte rainHeaviness = 0;
- byte rain_pool_left_count = 0;
- byte rain_pool_right_count = 0;
- struct droplet {
- byte pos;
- byte spd; //0 fastest to 10 slowest
- bool inpool;
- short dir; //-1 = left, 1 = right
- };
- droplet rain_pool[NUM_LEDS];
- bool led_pool[NUM_LEDS];
- byte num_droplets = 0;
- void init_rain_pool()
- {
- for(int i = 0; i < NUM_LEDS;i++)
- {
- droplet drop = {0, 0, false, 0};
- rain_pool[i] = drop;
- led_pool[i] = false;
- }
- }
- void rain()
- {
- random16_add_entropy(random8());
- if(!rain_pool_filling) {
- rainHeaviness = random16(10); //creating "heaviness" of rainfall
- trace("rain heaviness: "); traceln((String)rainHeaviness);
- init_rain_pool();
- rain_pool_filling = true;
- }
- int rng = random16(10); //base rng for calculations
- if(rain_pool_filled){
- delay(5000);
- FastLED.clear();
- num_droplets = 0;
- rain_pool_filling = false;
- rain_pool_filled = false;
- rain_pool_left_count = 0;
- rain_pool_right_count = 0;
- }
- else{
- for(int i = 0; i < num_droplets; i++){
- if(!rain_pool[i].inpool)
- {
- EVERY_N_MILLISECONDS( (rain_pool[i].spd+1)*2) {
- int temp_position = (rain_pool[i].pos+(rain_pool[i].dir)) % NUM_LEDS;
- leds[rain_pool[i].pos] = CRGB::Black;
- leds[temp_position].blue = 255;
- rain_pool[i].pos = temp_position;
- if(temp_position == (BOTTOM_RIGHT + 15)){
- rain_pool[i].inpool = true;
- led_pool[temp_position] = true;
- }
- if(leds[temp_position+rain_pool[i].dir].blue == 255){
- if(led_pool[(temp_position+rain_pool[i].dir)%NUM_LEDS]){
- rain_pool[i].inpool = true;
- if(rain_pool_left){
- temp_position = BOTTOM_RIGHT + 15 + rain_pool_left_count;
- leds[temp_position].blue = 255;
- led_pool[temp_position] = true;
- rain_pool_left_count++;
- rain_pool_left = false;
- rain_pool[i].pos = temp_position;
- }
- else{
- temp_position = BOTTOM_RIGHT + 15 - rain_pool_right_count;
- if( temp_position < 0 ) temp_position += NUM_LEDS;
- leds[temp_position].blue = 255;
- led_pool[temp_position] = true;
- rain_pool_right_count++;
- rain_pool_left = true;
- rain_pool[i].pos = temp_position;
- }
- }
- }
- }
- }
- FastLED.show();
- }
- if(rain_pool_left_count+rain_pool_right_count > 90) rain_pool_filled = true;
- if(rainHeaviness >= rng) //heavier the rain, more likely the chance of rain appearing
- {
- EVERY_N_MILLISECONDS(map(rainHeaviness,0,10,1500,100)){
- if(num_droplets < NUM_LEDS){
- int drop_direction = 0;
- if((rng % 2) == 0) //even rng rain falls left, else falls right
- {
- drop_direction = -1;
- }
- else
- {
- drop_direction = 1;
- }
- droplet drop = {TOP_LEFT+15, ((random16(20)-rainHeaviness) % 1) + 1, false, drop_direction};
- leds[TOP_LEFT+15].blue = 255;
- rain_pool[num_droplets] = drop;
- num_droplets++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement