Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<FastLED.h>
  2.  
  3. #define LED_PIN   5
  4. #define BRIGHTNESS  255
  5. #define LED_TYPE  WS2812B
  6. #define COLOR_ORDER GRB
  7.  
  8. const uint8_t LED_X  = 8;
  9. const uint8_t LED_Y = 4;
  10. const bool  kMatrixSerpentineLayout = false;
  11.  
  12. const uint16_t XSteps = 100;
  13. const uint16_t YSteps = 300;
  14.  
  15. const uint16_t SNOW_X = LED_X * XSteps;
  16. const uint16_t SNOW_Y = LED_Y * YSteps;
  17.  
  18.  
  19. #define NUM_LEDS (LED_X * LED_Y)
  20. #define MAX_DIMENSION ((LED_X>LED_Y) ? LED_X : LED_Y)
  21.  
  22. // The leds
  23. CRGB leds[LED_X * LED_Y];
  24.  
  25. int seedCounter = 0;
  26. int seedCheck = 10;
  27.  
  28. // The 16 bit version of our coordinates
  29. static uint16_t x;
  30. static uint16_t y;
  31. static uint16_t z;
  32.  
  33. uint16_t speed = 2; // speed is set dynamically once we've started up
  34. const uint8_t maxflakes = 5;
  35.  
  36. float snow[LED_X][LED_Y];
  37. short Wind = 0;
  38.  
  39.  
  40. class Flake {
  41.   public:
  42.     float PosX;
  43.     float PosY;
  44.     short Speed;
  45.     short Size;
  46.     bool isActive;
  47.  
  48.     uint8_t MoveCounter = 0;
  49.  
  50.  
  51.     Flake() {
  52.       isActive = false;
  53.     }
  54.  
  55.     void ResetFlake() {
  56.       isActive = true;
  57.       PosY = 0;
  58.       PosX = (rand() % LED_X) * XSteps;
  59.       Speed = (rand() % 7 + 4);
  60.       Size = (rand() % 100) + 50;
  61.     }
  62.  
  63.  
  64.     void UpdatePosition() {
  65.       PosY += Speed;
  66.       if (PosY > SNOW_Y - 1) {
  67.         isActive = false;
  68.       }
  69.  
  70.     }
  71.  
  72.  
  73.  
  74. };
  75.  
  76. Flake Flakes[maxflakes];
  77.  
  78.  
  79.  
  80. void setup() {
  81.   delay(4000);
  82.   Serial.begin(9600);
  83.   Serial.println("Setup");
  84.   for (int a = 0; a < maxflakes; a++) {
  85.     Flakes[a] = Flake();
  86.   }
  87.   Serial.println((String)"Waiting for LEDs");
  88.  
  89.   LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  90.   LEDS.setBrightness(BRIGHTNESS);
  91.  
  92.  
  93.   Serial.println("done");
  94. }
  95.  
  96.  
  97. void loop() {
  98.   //seedFlakes();
  99.   for (int a = 0; a < maxflakes; a++) {
  100.     if (Flakes[a].isActive == true) {
  101.       //Serial.println((String)"Moving flake " + a);
  102.       Flakes[a].UpdatePosition();
  103.     }
  104.  
  105.  
  106.   }
  107.  
  108.   seedCounter++;
  109.   if (seedCounter > seedCheck)
  110.   {
  111.     seedCounter = 0;
  112.     seedFlakes();
  113.     seedCheck = (rand() % 15) + 1;
  114.   }
  115.  
  116.   applyFlakes();
  117.  
  118.  
  119.   LEDS.show();
  120.   // delay(10);
  121.   delay(20);
  122.   //EVERY_N_SECONDS(30/maxflakes) {seedFlakes();}
  123.  
  124.   //  EVERY_N_SECONDS(1) {
  125.   //
  126.   ////  Serial.println((String)"Wind = " + Wind);
  127.   ////  for (int a = 0; a < maxflakes; a++) {
  128.   ////    Serial.println((String)"Flake " + a + "X=" + Flakes[a].PosX);
  129.   ////    Serial.println((String)"Flake " + a + "Y=" + Flakes[a].PosY);
  130.   ////  }
  131.   //  String thisArray;
  132.   //  for (int g = 0; g < LED_X; g++) {
  133.   //  for (int h = 0; h < LED_Y; h++) {
  134.   //    thisArray += (String)snow[g][h]+",";
  135.   //  }
  136.   //  thisArray += "\n";
  137.   //  }
  138.   //  Serial.println(thisArray);
  139.   //  }
  140.  
  141. }
  142.  
  143. void seedFlakes() {
  144.   //Serial.println((String)"In seedFlakes");
  145.   //  int aCount = 0;
  146.   //  int fCount = 0;
  147.   //  for (int i = 0; i < maxflakes; i++) {
  148.   //  if (Flakes[i].isActive == true) {
  149.   //    aCount+=1;
  150.   //    if (Flakes[i].PosY < SNOW_Y / 2) {fCount+=1;}
  151.   //  }
  152.   //  }
  153.   //  if (fCount < maxflakes/2) {
  154.  
  155.   for (int i = 0; i < maxflakes; i++) {
  156.     if (Flakes[i].isActive == false) {
  157.       //      uint8_t check = rand() % 10000;
  158.       //      if (check <= i) {
  159.       //        //Serial.println("New flake");
  160.       Flakes[i].ResetFlake();
  161.       if (rand() % 3 == 0) {
  162.         delay(rand() % 75);
  163.         seedFlakes();
  164.       }
  165.       //      }
  166.       break;
  167.     }
  168.   }
  169.   //  }
  170. }
  171.  
  172.  
  173. void applyFlakes() {
  174.   //Serial.println((String)"In applyFlakes");
  175.   for (int a = 0; a < LED_X; a++) {
  176.     for (int b = 0; b < LED_Y; b++) {
  177.       snow[a][b] = 0;
  178.     }
  179.   }
  180.  
  181.   for (int a = 0; a < maxflakes; a++) {
  182.     if (Flakes[a].isActive == true) {
  183.       //Serial.println((String)a+" is active");
  184.       double AXF, AYF;
  185.       float AdjX, AdjY, NextYB, ThisB;
  186.       AdjX = LED_X * (Flakes[a].PosX / SNOW_X);
  187.       AdjY = LED_Y * (Flakes[a].PosY / SNOW_Y);
  188.  
  189.       float AXR = modf(AdjX, &AXF);
  190.       float AYR = modf(AdjY, &AYF);
  191.       ThisB = Flakes[a].Size * (1 - AXR) * (1 - AYR);
  192.       NextYB = Flakes[a].Size * (1 - AXR) * (AYR);
  193.  
  194.       int AXFI = AXF;
  195.       int AYFI = AYF;
  196.       snow[AXFI][AYFI] += ThisB;
  197.  
  198.       if (AYF != LED_Y - 1) {
  199.         snow[AXFI][AYFI + 1] += NextYB;
  200.       }
  201.     }
  202.   }
  203.  
  204.   for (int g = 0; g < LED_X; g++) {
  205.     for (int h = 0; h < LED_Y; h++) {
  206.       //    Serial.print((String)snow[g][h] + ",");
  207.       //leds[XY(g, h)] = CHSV(0, 0, (snow[g][h] == 0 ? 0 : (snow[g][h] > 255 ? 255 : (snow[g][h] < 24 ? 24 : snow[g][h]))));
  208.       //leds[XY(g, h)] = CHSV(0, 0, (snow[g][h] > 255 ? 255 : (snow[g][h] < 24 ? 24 : snow[g][h])));
  209.       leds[XY(g, h)] = CHSV(0, 0, (snow[g][h] > 255 ? 255 : snow[g][h]));
  210.     }
  211.     //Serial.println();
  212.   }
  213. }
  214.  
  215.  
  216.  
  217. //
  218. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  219. //
  220. uint16_t XY( uint8_t x, uint8_t y)
  221. {
  222.   uint16_t i;
  223.   if ( kMatrixSerpentineLayout == false) {
  224.     i = (x * LED_Y) + y;
  225.   }
  226.   if ( kMatrixSerpentineLayout == true) {
  227.     if ( y & 0x01) {
  228.       // Odd rows run backwards
  229.       uint8_t reverseX = (LED_X - 1) - x;
  230.       i = (y * LED_X) + reverseX;
  231.     } else {
  232.       // Even rows run forwards
  233.       i = (y * LED_X) + x;
  234.     }
  235.   }
  236.   return i;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement