Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<FastLED.h>
- #define LED_PIN 5
- #define BRIGHTNESS 255
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- const uint8_t LED_X = 8;
- const uint8_t LED_Y = 4;
- const bool kMatrixSerpentineLayout = false;
- const uint16_t XSteps = 100;
- const uint16_t YSteps = 300;
- const uint16_t SNOW_X = LED_X * XSteps;
- const uint16_t SNOW_Y = LED_Y * YSteps;
- #define NUM_LEDS (LED_X * LED_Y)
- #define MAX_DIMENSION ((LED_X>LED_Y) ? LED_X : LED_Y)
- // The leds
- CRGB leds[LED_X * LED_Y];
- int seedCounter = 0;
- int seedCheck = 10;
- // The 16 bit version of our coordinates
- static uint16_t x;
- static uint16_t y;
- static uint16_t z;
- uint16_t speed = 2; // speed is set dynamically once we've started up
- const uint8_t maxflakes = 5;
- float snow[LED_X][LED_Y];
- short Wind = 0;
- class Flake {
- public:
- float PosX;
- float PosY;
- short Speed;
- short Size;
- bool isActive;
- uint8_t MoveCounter = 0;
- Flake() {
- isActive = false;
- }
- void ResetFlake() {
- isActive = true;
- PosY = 0;
- PosX = (rand() % LED_X) * XSteps;
- Speed = (rand() % 7 + 4);
- Size = (rand() % 100) + 50;
- }
- void UpdatePosition() {
- PosY += Speed;
- if (PosY > SNOW_Y - 1) {
- isActive = false;
- }
- }
- };
- Flake Flakes[maxflakes];
- void setup() {
- delay(4000);
- Serial.begin(9600);
- Serial.println("Setup");
- for (int a = 0; a < maxflakes; a++) {
- Flakes[a] = Flake();
- }
- Serial.println((String)"Waiting for LEDs");
- LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
- LEDS.setBrightness(BRIGHTNESS);
- Serial.println("done");
- }
- void loop() {
- //seedFlakes();
- for (int a = 0; a < maxflakes; a++) {
- if (Flakes[a].isActive == true) {
- //Serial.println((String)"Moving flake " + a);
- Flakes[a].UpdatePosition();
- }
- }
- seedCounter++;
- if (seedCounter > seedCheck)
- {
- seedCounter = 0;
- seedFlakes();
- seedCheck = (rand() % 15) + 1;
- }
- applyFlakes();
- LEDS.show();
- // delay(10);
- delay(20);
- //EVERY_N_SECONDS(30/maxflakes) {seedFlakes();}
- // EVERY_N_SECONDS(1) {
- //
- //// Serial.println((String)"Wind = " + Wind);
- //// for (int a = 0; a < maxflakes; a++) {
- //// Serial.println((String)"Flake " + a + "X=" + Flakes[a].PosX);
- //// Serial.println((String)"Flake " + a + "Y=" + Flakes[a].PosY);
- //// }
- // String thisArray;
- // for (int g = 0; g < LED_X; g++) {
- // for (int h = 0; h < LED_Y; h++) {
- // thisArray += (String)snow[g][h]+",";
- // }
- // thisArray += "\n";
- // }
- // Serial.println(thisArray);
- // }
- }
- void seedFlakes() {
- //Serial.println((String)"In seedFlakes");
- // int aCount = 0;
- // int fCount = 0;
- // for (int i = 0; i < maxflakes; i++) {
- // if (Flakes[i].isActive == true) {
- // aCount+=1;
- // if (Flakes[i].PosY < SNOW_Y / 2) {fCount+=1;}
- // }
- // }
- // if (fCount < maxflakes/2) {
- for (int i = 0; i < maxflakes; i++) {
- if (Flakes[i].isActive == false) {
- // uint8_t check = rand() % 10000;
- // if (check <= i) {
- // //Serial.println("New flake");
- Flakes[i].ResetFlake();
- if (rand() % 3 == 0) {
- delay(rand() % 75);
- seedFlakes();
- }
- // }
- break;
- }
- }
- // }
- }
- void applyFlakes() {
- //Serial.println((String)"In applyFlakes");
- for (int a = 0; a < LED_X; a++) {
- for (int b = 0; b < LED_Y; b++) {
- snow[a][b] = 0;
- }
- }
- for (int a = 0; a < maxflakes; a++) {
- if (Flakes[a].isActive == true) {
- //Serial.println((String)a+" is active");
- double AXF, AYF;
- float AdjX, AdjY, NextYB, ThisB;
- AdjX = LED_X * (Flakes[a].PosX / SNOW_X);
- AdjY = LED_Y * (Flakes[a].PosY / SNOW_Y);
- float AXR = modf(AdjX, &AXF);
- float AYR = modf(AdjY, &AYF);
- ThisB = Flakes[a].Size * (1 - AXR) * (1 - AYR);
- NextYB = Flakes[a].Size * (1 - AXR) * (AYR);
- int AXFI = AXF;
- int AYFI = AYF;
- snow[AXFI][AYFI] += ThisB;
- if (AYF != LED_Y - 1) {
- snow[AXFI][AYFI + 1] += NextYB;
- }
- }
- }
- for (int g = 0; g < LED_X; g++) {
- for (int h = 0; h < LED_Y; h++) {
- // Serial.print((String)snow[g][h] + ",");
- //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]))));
- //leds[XY(g, h)] = CHSV(0, 0, (snow[g][h] > 255 ? 255 : (snow[g][h] < 24 ? 24 : snow[g][h])));
- leds[XY(g, h)] = CHSV(0, 0, (snow[g][h] > 255 ? 255 : snow[g][h]));
- }
- //Serial.println();
- }
- }
- //
- // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
- //
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if ( kMatrixSerpentineLayout == false) {
- i = (x * LED_Y) + y;
- }
- if ( kMatrixSerpentineLayout == true) {
- if ( y & 0x01) {
- // Odd rows run backwards
- uint8_t reverseX = (LED_X - 1) - x;
- i = (y * LED_X) + reverseX;
- } else {
- // Even rows run forwards
- i = (y * LED_X) + x;
- }
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement