Advertisement
Seechay

FastLED Rainfall Simulator

Aug 15th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define DATA_PIN 6
  4.  
  5. #define NUM_LEDS 120
  6. #define MAX_BRIGHTNESS 255
  7. #define BOTTOM_RIGHT 21
  8. #define BOTTOM_LEFT 51
  9. #define TOP_LEFT 81
  10. #define TOP_RIGHT 111
  11. #define debugPrint 1
  12.  
  13. CRGB leds[NUM_LEDS];
  14.  
  15. void setup()
  16. {  
  17.   Serial.begin(9600);  
  18.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  19.   FastLED.setBrightness(MAX_BRIGHTNESS);
  20. }
  21.  
  22. void loop()                    
  23. {
  24.  
  25.  rain();
  26.  
  27. }
  28. void trace(String s)
  29. {
  30.   if(debugPrint)
  31.   {
  32.     Serial.print(s);
  33.   }
  34. }
  35.  
  36. void traceln()
  37. {
  38.   if(debugPrint)
  39.   Serial.print("\n");
  40. }
  41.  
  42. void traceln(String s)
  43. {
  44.   if(debugPrint)
  45.   Serial.print(s);
  46.   Serial.print("\n");
  47. }
  48.  
  49.  
  50.  
  51. bool rain_pool_left = false;
  52. bool rain_pool_filled = false;
  53. bool rain_pool_filling = false;
  54. byte rainHeaviness = 0;
  55. byte rain_pool_left_count = 0;
  56. byte rain_pool_right_count = 0;
  57.  
  58. struct droplet {
  59.   byte pos;
  60.   byte spd; //0 fastest to 10 slowest
  61.   bool inpool;
  62.   short dir; //-1 = left, 1 = right
  63. };
  64.  
  65. droplet rain_pool[NUM_LEDS];
  66. bool led_pool[NUM_LEDS];
  67. byte num_droplets = 0;
  68.  
  69. void init_rain_pool()
  70. {
  71.   for(int i = 0; i < NUM_LEDS;i++)
  72.   {
  73.     droplet drop = {0, 0, false, 0};
  74.     rain_pool[i] = drop;
  75.     led_pool[i] = false;
  76.   }
  77. }
  78.  
  79. void rain()
  80. {
  81.   random16_add_entropy(random8());
  82.   if(!rain_pool_filling) {
  83.     rainHeaviness = random16(10); //creating "heaviness" of rainfall
  84.     trace("rain heaviness: "); traceln((String)rainHeaviness);
  85.     init_rain_pool();
  86.     rain_pool_filling = true;
  87.   }
  88.   int rng = random16(10); //base rng for calculations
  89.   if(rain_pool_filled){
  90.     delay(5000);
  91.     FastLED.clear();
  92.     num_droplets = 0;
  93.     rain_pool_filling = false;
  94.     rain_pool_filled = false;
  95.     rain_pool_left_count = 0;
  96.     rain_pool_right_count = 0;
  97.   }
  98.   else{
  99.     for(int i = 0; i < num_droplets; i++){
  100.       if(!rain_pool[i].inpool)
  101.       {
  102.         EVERY_N_MILLISECONDS( (rain_pool[i].spd+1)*2) {
  103.           int temp_position = (rain_pool[i].pos+(rain_pool[i].dir)) % NUM_LEDS;          
  104.           leds[rain_pool[i].pos] = CRGB::Black;
  105.           leds[temp_position].blue = 255;
  106.           rain_pool[i].pos = temp_position;
  107.           if(temp_position == (BOTTOM_RIGHT + 15)){
  108.             rain_pool[i].inpool = true;
  109.             led_pool[temp_position] = true;
  110.           }
  111.           if(leds[temp_position+rain_pool[i].dir].blue == 255){
  112.             if(led_pool[(temp_position+rain_pool[i].dir)%NUM_LEDS]){
  113.                rain_pool[i].inpool = true;                
  114.                if(rain_pool_left){
  115.                  temp_position = BOTTOM_RIGHT + 15 + rain_pool_left_count;
  116.                  leds[temp_position].blue = 255;
  117.                  led_pool[temp_position] = true;
  118.                  rain_pool_left_count++;
  119.                  rain_pool_left = false;
  120.                  rain_pool[i].pos = temp_position;
  121.                }
  122.                else{
  123.                  temp_position = BOTTOM_RIGHT + 15 - rain_pool_right_count;
  124.                  if( temp_position < 0 ) temp_position += NUM_LEDS;
  125.                  leds[temp_position].blue = 255;
  126.                  led_pool[temp_position] = true;
  127.                  rain_pool_right_count++;
  128.                  rain_pool_left = true;
  129.                  rain_pool[i].pos = temp_position;
  130.                }
  131.               }
  132.             }
  133.         }
  134.         }    
  135.     FastLED.show();    
  136.     }
  137.     if(rain_pool_left_count+rain_pool_right_count > 90) rain_pool_filled = true;
  138.     if(rainHeaviness >= rng) //heavier the rain, more likely the chance of rain appearing
  139.     {
  140.       EVERY_N_MILLISECONDS(map(rainHeaviness,0,10,1500,100)){
  141.         if(num_droplets < NUM_LEDS){
  142.           int drop_direction = 0;
  143.           if((rng % 2) == 0) //even rng rain falls left, else falls right
  144.           {
  145.             drop_direction = -1;
  146.           }
  147.           else
  148.           {
  149.             drop_direction = 1;
  150.           }
  151.           droplet drop = {TOP_LEFT+15, ((random16(20)-rainHeaviness) % 1) + 1, false, drop_direction};
  152.           leds[TOP_LEFT+15].blue = 255;
  153.           rain_pool[num_droplets] = drop;
  154.           num_droplets++;
  155.         }
  156.       }
  157.     }
  158.   }    
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement