Przeglądaj źródła

Merge of copy at mom

jediemil 4 lat temu
rodzic
commit
aba236df07

+ 16 - 5
LedThing.ino/AnimationSetups.ino

@@ -1,6 +1,6 @@
 void setup_rainbow() {
   normal_setup();
-  animation_var_1 = 4;
+  animation_var_1 = 5.12;
 }
 
 void normal_setup() {
@@ -10,14 +10,25 @@ void normal_setup() {
   animation_var_1 = 0;
 }
 
-void setup_random1() {
-  for (int i = 0; i < NUM_LEDS; i++) {
-    for (int i = 0; i < 8; i++) {
-      animation_table_1[i][i] = 0;
+void clean_animation_table_1() {
+  for (int i = 0; i < 10; i++) {
+    for (int y = 0; y < 8; y++) {
+      animation_table_1[i][y] = 0;
     }
     //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
   }
+}
+
+void setup_random1() {
+  clean_animation_table_1();
   normal_setup();
   animation_var_1 = 10; //How many sections
   animation_step_time = 100;
 }
+
+void setup_random2() {
+  clean_animation_table_1();
+  normal_setup();
+  animation_var_1 = 5;
+  animation_step_time = 50;
+}

+ 96 - 39
LedThing.ino/Animations.ino

@@ -21,25 +21,25 @@ void animation_running() {
 void animation_chasing() {
   //Serial.println("Animation 2 step");
   clear_strip();
-  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);
+  leds[animation_i] = CRGB(100, 255, 200);
+  leds[(animation_i + 10) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[(animation_i + 20) % (NUMBER_OF_LEDS)] = CRGB(100, 255, 200);
+  leds[((NUMBER_OF_LEDS) - animation_i) - 1] = 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 %= NUMBER_OF_LEDS/3;
+  animation_i %= NUMBER_OF_LEDS;
 }
 
 void animation_off() {
@@ -49,7 +49,7 @@ void animation_off() {
 
 void animation_random1() {
   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];
@@ -63,9 +63,9 @@ void animation_random1() {
     uint8_t g = (goalGreen - startGreen) * currentStep / howManyCycles + startGreen;
     uint8_t b = (goalBlue - startBlue) * currentStep / howManyCycles + startBlue;
 
-//    for (int led = 0; led < 10; led++) {
-//      leds[led + i * 10] = 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;
@@ -104,7 +104,7 @@ void animation_random1() {
       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);
+      leds[currentStep + i * difference] = CRGB(r, g, b);
     }
   }
   FastLED.show();
@@ -112,26 +112,83 @@ void animation_random1() {
 
 void animation_rgb_light() {
   FastLED.clear();
-  for (int i = 0; i < NUM_LEDS/6; i++) {
+  for (int i = 0; i < NUM_LEDS / 6; i++) {
     leds[(i + animation_i) % NUM_LEDS] = CRGB(255, 0, 0);
-    leds[(i + animation_i + NUM_LEDS/3) % NUM_LEDS] = CRGB(0, 255, 0);
-    leds[(i + animation_i + (NUM_LEDS/3)*2) % NUM_LEDS] = CRGB(0, 0, 255);
+    leds[(i + animation_i + NUM_LEDS / 3) % NUM_LEDS] = CRGB(0, 255, 0);
+    leds[(i + animation_i + (NUM_LEDS / 3) * 2) % NUM_LEDS] = CRGB(0, 0, 255);
   }
-//  for (int i = 1; i < NUM_LEDS/6; i++) { // Made an animation that already exists, but more complicated (animation_rainbow)
-//    uint8_t r = (0 - 255) * i / 25 + 255;
-//    uint8_t g = (255) * i / 25;
-//    uint8_t b = 0;
-//    leds[(i + animation_i + 25) % NUM_LEDS] = CRGB(r, g, b);
-//    r = 0;
-//    g = (0 - 255) * i / 25 + 255;
-//    b = (255) * i / 25;
-//    leds[(i + animation_i + 25 + 50) % NUM_LEDS] = CRGB(r, g, b);
-//    r = (255 - 0) * i / 25 + 0;
-//    g = 0;
-//    b = (0 - 255) * i / 25 + 255;
-//    leds[(i + animation_i + 25 + 100) % NUM_LEDS] = CRGB(r, g, b);
-//  }
+  //  for (int i = 1; i < NUM_LEDS/6; i++) { // Made an animation that already exists, but more complicated (animation_rainbow)
+  //    uint8_t r = (0 - 255) * i / 25 + 255;
+  //    uint8_t g = (255) * i / 25;
+  //    uint8_t b = 0;
+  //    leds[(i + animation_i + 25) % NUM_LEDS] = CRGB(r, g, b);
+  //    r = 0;
+  //    g = (0 - 255) * i / 25 + 255;
+  //    b = (255) * i / 25;
+  //    leds[(i + animation_i + 25 + 50) % NUM_LEDS] = CRGB(r, g, b);
+  //    r = (255 - 0) * i / 25 + 0;
+  //    g = 0;
+  //    b = (0 - 255) * i / 25 + 255;
+  //    leds[(i + animation_i + 25 + 100) % NUM_LEDS] = CRGB(r, g, b);
+  //  }
   FastLED.show();
   animation_i++;
   animation_i %= NUM_LEDS;
 }
+
+void animation_random2() {
+  if (random(0, animation_var_1) == 0) {
+    //Serial.println("Stage 1");
+    for (int i = 0; i < 10; i++) {
+      //Serial.println("Stage 2");
+      //Serial.println(animation_table_1[i][1]);
+      if (animation_table_1[i][1] == 0) {
+        //Serial.println("Stage 3");
+        int randomDot = random(0, NUMBER_OF_LEDS);
+        //Serial.println(randomDot);
+        if (!leds[randomDot]) {
+          //Serial.println("Stage 4");
+          animation_table_1[i][0] = random(0, 255);
+          animation_table_1[i][1] = 255;
+          animation_table_1[i][2] = randomDot;
+          animation_table_1[i][3] = 1;
+          //Serial.println(animation_table_1[i][0]);
+          leds[randomDot] = CHSV(animation_table_1[i][0], 255, 126);
+          break;
+        }
+      }
+    }
+  }
+
+  for (int i = 0; i < 10; i++) {
+    if (animation_table_1[i][3] == 1) {
+      leds[animation_table_1[i][2]] = CHSV(animation_table_1[i][0], 255, 126);
+      animation_table_1[i][3] = 2;
+    } else if (animation_table_1[i][3] == 2) {
+      leds[animation_table_1[i][2]] = CHSV(animation_table_1[i][0], 255, 255);
+      animation_table_1[i][3] = 0;
+    } else {
+      animation_table_1[i][1] = max(animation_table_1[i][1] - 10, 0);
+      leds[animation_table_1[i][2]] = CHSV(animation_table_1[i][0], 255, animation_table_1[i][1]);
+    }
+  }
+
+
+  //  if (animation_i <= animation_var_1) {
+  //    int randomDot = random(0, NUMBER_OF_LEDS);
+  //    if (!leds[randomDot]) {
+  //      leds[randomDot] = CHSV(random(0, 255), 255, 255);
+  //      animation_i += 1;
+  //    }
+  //  } else {
+  //    for (int i = 0; i < NUM_LEDS; i++) {
+  //      if (leds[i]) {
+  //        if (random(1, animation_var_1) == 1) {
+  //          leds[i] = CRGB(0, 0, 0);
+  //          animation_i -= 1;
+  //        }
+  //      }
+  //    }
+  //  }
+  FastLED.show();
+}

+ 102 - 52
LedThing.ino/LedThing.ino.ino

@@ -2,13 +2,13 @@
 #include <SPI.h>
 #include <WiFiNINA.h>
 
-#define NUM_LEDS 150 
+#define NUM_LEDS 150
 //50
 #define DATA_PIN 9
-int NUMBER_OF_ANIMATIONS = 6;
+int NUMBER_OF_ANIMATIONS = 7;
 int NUMBER_OF_LEDS = 150;
-char ssid[] = "Pannkakshuset";
-char pass[] = "lavalampa";
+char ssid[] = "Telia-E39815";
+char pass[] = "B3C606D8E7";
 boolean doing_animation = true;
 
 int current_animation = 0;
@@ -17,7 +17,7 @@ int loops_since_http = 0;
 int animation_step_time = 0;
 int animation_i = 0;
 int animation_var_1 = 0;
-uint16_t animation_table_1[10][8] = {};
+int animation_table_1[10][8] = {};
 
 long last_animation_millis = millis();
 long latest_animation_change = millis();
@@ -29,15 +29,17 @@ extern void animation_chasing();
 extern void animation_off();
 extern void animation_random1();
 extern void animation_rgb_light();
+extern void animation_random2();
 
-void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1, animation_rgb_light};
+void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1, animation_rgb_light, animation_random2};
 
 extern void setup_rainbow(); // forward declaration
 extern void setup_random1();
 extern void normal_setup();
 extern void setup_chosen(int, int, CRGB, boolean, int, int);
+extern void setup_random2();
 
-void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1, normal_setup};
+void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1, normal_setup, setup_random2};
 
 CRGB leds[NUM_LEDS];
 
@@ -53,23 +55,27 @@ void setup() {
   while (!Serial) {
     ; // wait for serial port to connect. Needed for native USB port only
   }
+  Serial.println("Starting");
 
   FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //TM1804 BRG
   FastLED.setBrightness(255);
   FastLED.clear();
   FastLED.show();
+
+  Serial.println("Connected LEDs");
   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);
-    }
-  }  
-  
+  //  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) {
@@ -86,31 +92,33 @@ void setup() {
       delay(10000);
     }
   }
+  Serial.println("Connected to WiFi");
 
-  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);
-    }
-  }  
-  
+  //  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);
+  //    }
+  //  }
+
+  Serial.println(WiFi.localIP());
   server.begin();
 
   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);
-    }
-  }
+  //  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]();
@@ -134,6 +142,13 @@ void loop() {
   } else {
     loops_since_http = 0;
     //Serial.println("Waiting for animation");
+    status=WiFi.status();
+    if (status == WL_DISCONNECTED || status == WL_CONNECTION_LOST) {
+      while (status != WL_CONNECTED) {
+        status = WiFi.begin(ssid, pass);
+        delay(1000);
+      }
+    }
     process_HTTP();
     delay(1);
   }
@@ -162,8 +177,7 @@ void process_HTTP() {
     int anim = 0;
     Serial.println("new client");
     // an http request ends with a blank line
-    boolean foundSlash = false;
-    boolean foundHashtag = false;
+    int readingState = 0;
     int leds1 = 0;
     int leds2 = 0;
     uint8_t newR = 0;
@@ -173,12 +187,14 @@ void process_HTTP() {
     int cycles = 20;
     int cycleLength = 100;
     int currentLoop = 0;
+    int setting = 0;
+    int value = 0;
     String fullArguments = "";
     while (client.connected()) {
       if (client.available()) {
         char c = client.read();
         //Serial.write(c);
-        if (foundHashtag) { //http://192.168.112.41/z0:49:0:255:0:0:20:1000;
+        if (readingState == 2) { //http://192.168.112.41/z0:49:0:255:0:0:20:1000;
           if (c == ';') {
             client.println("HTTP/1.1 200 OK");
             client.println("Content-Type: text/plain");
@@ -209,10 +225,35 @@ void process_HTTP() {
           } else {
             fullArguments = fullArguments + c;
           }
-        } else if (foundSlash) {
-          if (c == 'z') {
-            foundHashtag = true;
-            foundSlash = false;
+        } else if (readingState == 3)  {
+          Serial.println("Inside readingState == 3");
+          if (c == ';') {
+            client.println("HTTP/1.1 200 OK");
+            client.println("Content-Type: text/plain");
+            client.println("Connection: close");  // the connection will be closed after completion of the response
+            client.println();
+            client.println("OK");
+            break;
+          } else if (c == ':') {
+            if (currentLoop == 0) {
+              setting = fullArguments.toInt();
+            } else if (currentLoop == 1) {
+              value = fullArguments.toInt();
+              Serial.println(value);
+            }
+            fullArguments = "";
+            currentLoop++;
+            Serial.println(currentLoop);
+          } else {
+            fullArguments = fullArguments + c;
+            Serial.println(fullArguments);
+          }
+        } else if (readingState != 0) {
+          if (c == 'y') {
+            readingState = 3;
+            continue;
+          } if (c == 'z') {
+            readingState = 2;
             continue;
           } else if (c == '(') {
             anim = current_animation - 1;
@@ -237,7 +278,7 @@ void process_HTTP() {
         }
 
         if (c == '/') {
-          foundSlash = true;
+          readingState = 1;
         }
       }
     }
@@ -247,19 +288,28 @@ void process_HTTP() {
     // close the connection:
     client.stop();
     //Serial.println("client disconnected");
-    if (foundSlash) {
+    if (readingState == 1) {
       doing_animation = true;
       setup_table[anim]();
       current_animation = anim;
       latest_animation_change = millis();
-    } else {
+
+    } else if (readingState == 3) {
+      Serial.println(setting);
+      Serial.println(value);
+      if (setting == 1) {
+        animation_step_time = value;
+      } else if (setting == 2) {
+        animation_var_1 = value;
+      }
+    } else if (readingState == 2) {
       doing_animation = false;
-//      Serial.println(leds1);
-//      Serial.println(leds2);
-//      Serial.println(newR);
-//      Serial.println(newG);
-//      Serial.println(newB);
-//      Serial.println(doFade);
+      //      Serial.println(leds1);
+      //      Serial.println(leds2);
+      //      Serial.println(newR);
+      //      Serial.println(newG);
+      //      Serial.println(newB);
+      //      Serial.println(doFade);
       setup_chosen(leds1, leds2, CRGB(newR, newG, newB), (boolean) doFade, cycles, cycleLength);
     }
   }