#include "FastLED.h" #include #include #define NUM_LEDS 150 //50 #define DATA_PIN 9 int NUMBER_OF_ANIMATIONS = 6; int NUMBER_OF_LEDS = 150; char ssid[] = "Pannkakshuset"; char pass[] = "lavalampa"; boolean doing_animation = true; int current_animation = 0; 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] = {}; long last_animation_millis = millis(); long latest_animation_change = millis(); //typedef enum {STATE_A = 0, STATE_B = 1} State_type; extern void animation_rainbow(); // forward declaration 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, 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, normal_setup}; CRGB leds[NUM_LEDS]; int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { setup_random1(); // put your setup code here, to run once: //Serial.begin(9600); Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } FastLED.addLeds(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) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: if (first_check) { delay(1000); first_check = false; } else { 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(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"); } void loop() { loops_since_http += 1; long current_millis = millis(); if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) { if (doing_animation) { animation_table[current_animation](); } else { if (animation_var_1 == 1) { animation_fade_help(); } else { delay(5); } } last_animation_millis = current_millis; } else { loops_since_http = 0; //Serial.println("Waiting for animation"); process_HTTP(); delay(1); } // put your main code here, to run repeatedly: //Serial.println("Hello"); //delay(1000); // leds[3] = CRGB::Blue; // leds[0] = CRGB::Red; // leds[1] = CRGB::Green; // leds[49] = CRGB::White; // FastLED.clear(); // FastLED.show(); // uint8_t delta = 10; // for (uint8_t i = 0; i < 360; i++) { // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4); // fill_rainbow(leds, NUM_LEDS, i, delta); // FastLED.show(); // delay(10); // } } void process_HTTP() { WiFiClient client = server.available(); if (client && millis() - latest_animation_change > 1000) { int anim = 0; Serial.println("new client"); // an http request ends with a blank line boolean foundSlash = false; boolean foundHashtag = false; int leds1 = 0; int leds2 = 0; uint8_t newR = 0; uint8_t newG = 0; uint8_t newB = 0; int doFade = 0; //Between 0 and 1 int cycles = 20; int cycleLength = 100; int currentLoop = 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 (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) { leds1 = max(fullArguments.toInt(), 0); } else if (currentLoop == 1) { leds2 = min(fullArguments.toInt(), NUM_LEDS - 1); } else if (currentLoop == 2) { newR = min(max(fullArguments.toInt(), 0), 255); } else if (currentLoop == 3) { newG = min(max(fullArguments.toInt(), 0), 255); } else if (currentLoop == 4) { newB = min(max(fullArguments.toInt(), 0), 255); } else if (currentLoop == 5) { doFade = fullArguments.toInt() % 2; } else if (currentLoop == 6) { cycles = fullArguments.toInt(); } else if (currentLoop == 7) { cycleLength = min(fullArguments.toInt(), 40); } fullArguments = ""; currentLoop++; } else { fullArguments = fullArguments + c; } } else if (foundSlash) { if (c == 'z') { foundHashtag = true; foundSlash = false; continue; } else if (c == '(') { anim = current_animation - 1; if (anim < 0) { anim = NUMBER_OF_ANIMATIONS - 1; } } else if (c == ')') { anim = current_animation + 1; } else { anim = c - 'A'; } if (anim < 0) { anim = -anim; } anim %= NUMBER_OF_ANIMATIONS; 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; } if (c == '/') { foundSlash = true; } } } // give the web browser time to receive the data delay(300); // close the connection: client.stop(); //Serial.println("client disconnected"); if (foundSlash) { doing_animation = true; setup_table[anim](); current_animation = anim; latest_animation_change = millis(); } else { doing_animation = false; // 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); } } } void clear_strip() { FastLED.clear(); FastLED.show(); } void animation_rainbow() { fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1); FastLED.show(); animation_i++; animation_i %= 256; //Serial.println(animation_i); //Serial.println("Animation 1 step"); } void animation_running() { //Serial.println("Animation 2 step"); leds[animation_i] = CRGB(100, 255, 200); FastLED.show(); animation_i++; if (animation_i >= NUMBER_OF_LEDS) { animation_i = 0; clear_strip(); } } 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); FastLED.show(); animation_i++; animation_i %= NUMBER_OF_LEDS/3; } void animation_off() { clear_strip(); animation_step_time = 10000; } 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]; uint8_t startRed = animation_table_1[i][0]; uint8_t startGreen = animation_table_1[i][1]; uint8_t startBlue = animation_table_1[i][2]; uint8_t goalRed = animation_table_1[i][3]; uint8_t goalGreen = animation_table_1[i][4]; uint8_t goalBlue = animation_table_1[i][5]; uint8_t r = (goalRed - startRed) * currentStep / howManyCycles + startRed; 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); // } leds[i * difference] = CRGB(r, b, g); animation_table_1[i][6] += 1; if (currentStep >= howManyCycles) { // uint8_t newR = (uint8_t) random(0, 255); // uint8_t newG = (uint8_t) random(0, 255); // uint8_t newB = (uint8_t) random(0, 255); uint8_t newH = (uint8_t) random(0, 255); uint8_t newS = (uint8_t) random(200, 255); CRGB newColor = CRGB(0, 0, 0); newColor.setHSV(newH, newS, 255); 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(50, 255); animation_table_1[i][0] = r; animation_table_1[i][1] = g; animation_table_1[i][2] = b; animation_table_1[i][7] = steps; animation_table_1[i][6] = 0; } } 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]; int finnishR = animation_table_1[0][2]; int finnishG = animation_table_1[0][3]; int finnishB = animation_table_1[0][4]; int cycles = animation_table_1[0][5]; int startR = animation_table_1[0][6]; int startG = animation_table_1[0][7]; int startB = animation_table_1[1][0]; uint8_t r = (finnishR - startR) * animation_i / cycles + startR; 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++) { leds[i] = CRGB(r, g, b); } animation_i++; if (animation_i >= cycles) { for (int i = leds1; i < leds2; i++) { leds[i] = CRGB(finnishR, finnishG, finnishB); } animation_var_1 = 0; } FastLED.show(); } void setup_rainbow() { normal_setup(); animation_var_1 = 4; } void normal_setup() { clear_strip(); animation_step_time = 100; animation_i = 0; 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; } //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles } normal_setup(); 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) { animation_step_time = cycleLength; animation_var_1 = (int) doFade; animation_i = 0; doing_animation = false; if (doFade) { animation_table_1[0][0] = lamps1; animation_table_1[0][1] = lamps2; animation_table_1[0][2] = newColor.r; animation_table_1[0][3] = newColor.g; animation_table_1[0][4] = newColor.b; animation_table_1[0][5] = cycles; animation_table_1[0][6] = leds[lamps1].r; animation_table_1[0][7] = leds[lamps1].g; animation_table_1[1][0] = leds[lamps1].b; } else { for (int i = lamps1; i <= lamps2; i++) { leds[i] = newColor; } FastLED.show(); } }