Prechádzať zdrojové kódy

For 150 leds and WS2812, new animaton, and fixes

jediemil 5 rokov pred
rodič
commit
29a9f5683b
1 zmenil súbory, kde vykonal 101 pridanie a 121 odobranie
  1. 101 121
      LedThing.ino/LedThing.ino.ino

+ 101 - 121
LedThing.ino/LedThing.ino.ino

@@ -2,9 +2,11 @@
 #include <SPI.h>
 #include <WiFiNINA.h>
 
-#define NUM_LEDS 50
+#define NUM_LEDS 150 
+//50
 #define DATA_PIN 9
-int NUMBER_OF_ANIMATIONS = 5;
+int NUMBER_OF_ANIMATIONS = 6;
+int NUMBER_OF_LEDS = 150;
 char ssid[] = "Pannkakshuset";
 char pass[] = "lavalampa";
 boolean doing_animation = true;
@@ -15,7 +17,7 @@ int loops_since_http = 0;
 int animation_step_time = 0;
 int animation_i = 0;
 int animation_var_1 = 0;
-uint8_t animation_table_1[10][8] = {};
+uint16_t animation_table_1[10][8] = {};
 
 long last_animation_millis = millis();
 long latest_animation_change = millis();
@@ -26,15 +28,16 @@ extern void animation_running(); // forward declaration
 extern void animation_chasing();
 extern void animation_off();
 extern void animation_random1();
+extern void animation_rgb_light();
 
-void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1};
+void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1, animation_rgb_light};
 
 extern void setup_rainbow(); // forward declaration
 extern void setup_random1();
 extern void normal_setup();
 extern void setup_chosen(int, int, CRGB, boolean, int, int);
 
-void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1};
+void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1, normal_setup};
 
 CRGB leds[NUM_LEDS];
 
@@ -50,6 +53,23 @@ void setup() {
   while (!Serial) {
     ; // wait for serial port to connect. Needed for native USB port only
   }
+
+  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //TM1804 BRG
+  FastLED.setBrightness(255);
+  FastLED.clear();
+  FastLED.show();
+  delay(1000);
+
+  for (int i = 15; i >= 10; i--) { //Remove this when small spce left
+    uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
+    for (int currentLed = 5; currentLed >= 0; currentLed--) {
+      leds[currentLed + i*5] = CRGB(color, 255-color, 0);
+      leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
+      FastLED.show();
+      delay(5*i/3 + random(0, 25*i + 1) + 1);
+    }
+  }  
+  
   //pinMode(DATA_PIN, OUTPUT);
   boolean first_check = true;
   while (status != WL_CONNECTED) {
@@ -66,14 +86,33 @@ void setup() {
       delay(10000);
     }
   }
+
+  for (int i = 10; i >= 5; i--) { //Remove this when small spce left
+    uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
+    for (int currentLed = 5; currentLed >= 0; currentLed--) {
+      leds[currentLed + i*5] = CRGB(color, 255-color, 0);
+      leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
+      FastLED.show();
+      delay(5*i/3 + random(0, 25*i + 1) + 1);
+    }
+  }  
+  
   server.begin();
 
-  delay(3000);
-  FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
-  FastLED.setBrightness(255);
-  FastLED.clear();
-  FastLED.show();
   delay(1000);
+  Serial.println("Ready");
+
+  for (int i = 5; i >= 0; i--) { //insert this when small space -> for (int i = (NUM_LEDS / 5) / 2; i >= 0; i--) {
+    uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
+    for (int currentLed = 5; currentLed >= 0; currentLed--) {
+      leds[currentLed + i*5] = CRGB(color, 255-color, 0);
+      leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
+      FastLED.show();
+      delay(5*i/3 + random(0, 25*i + 1) + 1);
+    }
+  }
+  delay(1000);
+  current_animation = 0;
   setup_table[current_animation]();
   Serial.println("Started");
 }
@@ -245,7 +284,7 @@ void animation_running() {
   leds[animation_i] = CRGB(100, 255, 200);
   FastLED.show();
   animation_i++;
-  if (animation_i >= 50) {
+  if (animation_i >= NUMBER_OF_LEDS) {
     animation_i = 0;
     clear_strip();
   }
@@ -254,15 +293,25 @@ void animation_running() {
 void animation_chasing() {
   //Serial.println("Animation 2 step");
   clear_strip();
-  leds[animation_i] = CRGB(100, 255, 200);
-  leds[(animation_i + 10) % 50] = CRGB(100, 255, 200);
-  leds[(animation_i + 20) % 50] = CRGB(100, 255, 200);
-  leds[(50 - animation_i) - 1] = CRGB(0, 255, 255);
+  leds[animation_i*3] = CRGB(100, 255, 200);
+  leds[(animation_i*3 + 10*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[(animation_i*3 + 20*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[((NUMBER_OF_LEDS) - animation_i*3) - 1*3] = CRGB(0, 255, 255);
+
+  leds[animation_i*3+1] = CRGB(100, 255, 200);
+  leds[(animation_i*3+1 + 10*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[(animation_i*3+1 + 20*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[((NUMBER_OF_LEDS) - animation_i*3+1) - 1*3] = CRGB(0, 255, 255);
+
+  leds[animation_i*3+2] = CRGB(100, 255, 200);
+  leds[(animation_i*3+2 + 10*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[(animation_i*3+2 + 20*3) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[((NUMBER_OF_LEDS) - animation_i*3+2) - 1*3] = CRGB(0, 255, 255);
   //  leds[(50 - animation_i - 10)] = CRGB(0, 255, 255);
   //  leds[(50 - animation_i)] = CRGB(0, 255, 255);
   FastLED.show();
   animation_i++;
-  animation_i %= 50;
+  animation_i %= NUMBER_OF_LEDS/3;
 }
 
 void animation_off() {
@@ -271,7 +320,9 @@ void animation_off() {
 }
 
 void animation_random1() {
-  for (int i = 0; i < NUM_LEDS / 5; i++) {
+  int difference = NUM_LEDS / animation_var_1;
+  
+  for (int i = 0; i < animation_var_1; i++) {
     int howManyCycles = animation_table_1[i][7];
     int currentStep = animation_table_1[i][6];
     uint8_t startRed = animation_table_1[i][0];
@@ -284,9 +335,10 @@ void animation_random1() {
     uint8_t g = (goalGreen - startGreen) * currentStep / howManyCycles + startGreen;
     uint8_t b = (goalBlue - startBlue) * currentStep / howManyCycles + startBlue;
 
-    for (int led = 0; led < 5; led++) {
-      leds[led + i * 5] = CRGB(r, b, g);
-    }
+//    for (int led = 0; led < 10; led++) {
+//      leds[led + i * 10] = CRGB(r, b, g);
+//    }
+    leds[i * difference] = CRGB(r, b, g);
 
     animation_table_1[i][6] += 1;
 
@@ -300,15 +352,11 @@ void animation_random1() {
       CRGB newColor = CRGB(0, 0, 0);
       newColor.setHSV(newH, newS, 255);
 
-      Serial.println(newColor.r);
-      Serial.println(newColor.g);
-      Serial.println(newColor.b);
-
       animation_table_1[i][3] = newColor.r;
       animation_table_1[i][4] = newColor.g;
       animation_table_1[i][5] = newColor.b;
 
-      uint8_t steps = (uint8_t) random(10, 50);
+      uint8_t steps = (uint8_t) random(50, 255);
       animation_table_1[i][0] = r;
       animation_table_1[i][1] = g;
       animation_table_1[i][2] = b;
@@ -316,104 +364,36 @@ void animation_random1() {
       animation_table_1[i][6] = 0;
 
     }
-    //    uint8_t r = leds[i*5].r + animation_table_1[i][0];
-    //    uint8_t g = leds[i*5].g + animation_table_1[i][1];
-    //    uint8_t b = leds[i*5].b + animation_table_1[i][2];
-    //    if (i == 0) {
-    //      Serial.println(r);
-    //      Serial.println(g);
-    //      Serial.println(b);
-    //    }
-    //    boolean rDone = false;
-    //    boolean gDone = false;
-    //    boolean bDone = false;
-    //    if (animation_table_1[i][0] > 0 && r >= animation_table_1[i][3]) {
-    //      r = animation_table_1[i][3];
-    //      rDone = true;
-    //    }
-    //    if (animation_table_1[i][1] > 0 && g >= animation_table_1[i][4]) {
-    //      g = animation_table_1[i][4];
-    //      gDone = true;
-    //    }
-    //    if (animation_table_1[i][2] > 0 && b >= animation_table_1[i][5]) {
-    //      b = animation_table_1[i][5];
-    //      bDone = true;
-    //    }
-    //    if (animation_table_1[i][0] <= 0 && r <= animation_table_1[i][3]) {
-    //      r = animation_table_1[i][3];
-    //      rDone = true;
-    //    }
-    //    if (animation_table_1[i][1] <= 0 && g <= animation_table_1[i][4]) {
-    //      g = animation_table_1[i][4];
-    //      gDone = true;
-    //    }
-    //    if (animation_table_1[i][2] <= 0 && b <= animation_table_1[i][5]) {
-    //      b = animation_table_1[i][5];
-    //      bDone = true;
-    //    }
-    //    if (i == 0) {
-    //    Serial.println(rDone);
-    //    Serial.println(gDone);
-    //    Serial.println(bDone);
-    //    }
-    //
-    //    for (int led = 0; led < 5; led++) {
-    //      leds[led + i*5] = CRGB(r, b, g);
-    //    }
-    //
-    //    if (rDone && gDone && bDone) {
-    //      if (i == 0) {
-    //        Serial.println("New color");
-    //      } else {
-    //        Serial.println("New color other i");
-    //      }
-    //      uint8_t newR = (uint8_t) random(0, 255);
-    //      uint8_t newG = (uint8_t) random(0, 255);
-    //      uint8_t newB = (uint8_t) random(0, 255);
-    //      animation_table_1[i][3] = newR;
-    //      animation_table_1[i][4] = newG;
-    //      animation_table_1[i][5] = newB;
-    //
-    //      uint8_t steps = (uint8_t) random(10, 50);
-    //      uint8_t newRStep = (uint8_t) (newR - r)/steps;
-    //      uint8_t newGStep = (uint8_t) (newG - g)/steps;
-    //      uint8_t newBStep = (uint8_t) (newB - b)/steps;
-    //      if (newRStep < 1) {
-    //        newRStep = 1;
-    //      }
-    //      if (newGStep < 1) {
-    //        newGStep = 1;
-    //      }
-    //      if (newBStep < 1) {
-    //        newBStep = 1;
-    //      }
-    //      animation_table_1[i][0] = newRStep;
-    //      animation_table_1[i][1] = newGStep;
-    //      animation_table_1[i][2] = newBStep;
-    //      if (i == 0) {
-    //      Serial.println(newR);
-    //      Serial.println(newG);
-    //      Serial.println(newB);
-    //      }
-    //    }
   }
-  for (int i = 0; i < NUM_LEDS / 5; i++) {
-    uint8_t startRed = leds[i * 5].r;
-    uint8_t startGreen = leds[i * 5].g;
-    uint8_t startBlue = leds[i * 5].b;
-    uint8_t goalRed = leds[((i + 1) * 5) % NUM_LEDS].r;
-    uint8_t goalGreen = leds[((i + 1) * 5) % NUM_LEDS].g;
-    uint8_t goalBlue = leds[((i + 1) * 5) % NUM_LEDS].b;
-    for (int currentStep = 1; currentStep < 5; currentStep++) {
-      uint8_t r = (goalRed - startRed) * currentStep / 5 + startRed;
-      uint8_t g = (goalGreen - startGreen) * currentStep / 5 + startGreen;
-      uint8_t b = (goalBlue - startBlue) * currentStep / 5 + startBlue;
-      leds[i * 5 + currentStep] = CRGB(r, g, b);
+  for (int i = 0; i < animation_var_1; i++) {
+    uint8_t startRed = leds[i * difference].r;
+    uint8_t startGreen = leds[i * difference].g;
+    uint8_t startBlue = leds[i * difference].b;
+    uint8_t goalRed = leds[((i + 1) * difference) % NUM_LEDS].r;
+    uint8_t goalGreen = leds[((i + 1) * difference) % NUM_LEDS].g;
+    uint8_t goalBlue = leds[((i + 1) * difference) % NUM_LEDS].b;
+    for (int currentStep = 1; currentStep < difference; currentStep++) {
+      uint8_t r = (goalRed - startRed) * currentStep / difference + startRed;
+      uint8_t g = (goalGreen - startGreen) * currentStep / difference + startGreen;
+      uint8_t b = (goalBlue - startBlue) * currentStep / difference + startBlue;
+      leds[currentStep + i*difference] = CRGB(r, g, b);
     }
   }
   FastLED.show();
 }
 
+void animation_rgb_light() {
+  FastLED.clear();
+  for (int i = 0; i < NUM_LEDS/3; i++) {
+    leds[i] = CRGB(255, 0, 0);
+    leds[(i + NUM_LEDS/3) % NUM_LEDS] = CRGB(0, 255, 0);
+    leds[(i + (NUM_LEDS/3)*2) % NUM_LEDS] = CRGB(0, 0, 255);
+  }
+  FastLED.show();
+  animation_i++;
+  animation_i %= NUM_LEDS/3;
+}
+
 void animation_fade_help() {
   int leds1 = animation_table_1[0][0];
   int leds2 = animation_table_1[0][1];
@@ -429,7 +409,7 @@ void animation_fade_help() {
   uint8_t g = (finnishG - startG) * animation_i / cycles + startG;
   uint8_t b = (finnishB - startB) * animation_i / cycles + startB;
 
-  for (int i = leds1; i < leds2; i++) {
+  for (int i = leds1; i <= leds2; i++) {
     leds[i] = CRGB(r, g, b);
   }
   animation_i++;
@@ -444,7 +424,7 @@ void animation_fade_help() {
 
 void setup_rainbow() {
   normal_setup();
-  animation_var_1 = 10;
+  animation_var_1 = 4;
 }
 
 void normal_setup() {
@@ -459,11 +439,11 @@ void setup_random1() {
     for (int i = 0; i < 8; i++) {
       animation_table_1[i][i] = 0;
     }
-    //= {0, 0, 0, 0, 0, 0}; //Step size R, Step size G, Step size B, fR, fG, fB
     //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
   }
   normal_setup();
-  animation_step_time = 500;
+  animation_var_1 = 10; //How many sections
+  animation_step_time = 100;
 }
 
 void setup_chosen(int lamps1, int lamps2, CRGB newColor, boolean doFade = false, int cycles = 20, int cycleLength = 100) {