#include "FastLED.h" #include #include #define NUM_LEDS 50 #define DATA_PIN 9 int NUMBER_OF_ANIMATIONS = 3; char ssid[] = "Pannkakshuset"; char pass[] = "lavalampa"; int current_animation = 0; int loops_since_http = 0; int animation_step_time = 0; int animation_i = 0; int animation_var_1 = 0; long last_animation_millis = 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(); void (*animation_table[])() = {animation_rainbow, animation_running, animation_chasing}; extern void setup_rainbow(); // forward declaration extern void setup_running(); // forward declaration extern void setup_chasing(); void (*setup_table[])() = {setup_rainbow, setup_running, setup_chasing}; CRGB leds[NUM_LEDS]; int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { // 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 } //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); } } server.begin(); delay(3000); FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness(255); FastLED.clear(); FastLED.show(); delay(1000); setup_table[current_animation](); } void loop() { loops_since_http += 1; long current_millis = millis(); if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) { animation_table[current_animation](); 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) { int anim = 0; Serial.println("new client"); // an http request ends with a blank line boolean foundSlash = false; while (client.connected()) { if (client.available()) { char c = client.read(); //Serial.write(c); if (foundSlash) { 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: application/json"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println("{}"); break; } if (c == '/') { foundSlash = true; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); //Serial.println("client disconnected"); setup_table[anim](); } } 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 %= 260; //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 >= 49) { animation_i = 0; clear_strip(); } } 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); FastLED.show(); animation_i++; animation_i %= 50; } void setup_rainbow() { Serial.println("Animation 1 setup"); clear_strip(); animation_step_time = 100; animation_i = 0; animation_var_1 = 10; current_animation = 0; } void setup_running() { Serial.println("Animation 2 setup"); clear_strip(); animation_step_time = 100; animation_i = 0; animation_var_1 = 0; current_animation = 1; } void setup_chasing() { Serial.println("Animation 3 setup"); clear_strip(); animation_step_time = 100; animation_i = 0; animation_var_1 = 0; current_animation = 2; }