| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- #include "FastLED.h"
- #include <SPI.h>
- #include <WiFiNINA.h>
- #define NUM_LEDS 200 //150 //50
- #define NUM_LEDS_WS 150
- #define NUM_LEDS_TM 50
- #define DATA_PIN 9
- #define DATA_PIN_2 10
- int NUMBER_OF_ANIMATIONS = 7;
- int NUMBER_OF_LEDS = 200; //150 //50
- char ssid[] = "Våfflan";
- char pass[] = "HemmaIoT";
- boolean doing_animation = true;
- //int current_animation = 0;
- //int current_animation_1 = 0;
- int loops_since_http = 0;
- //int animation_step_time = 0;
- //int animation_i = 0;
- //int animation_var_1 = 0;
- int animation_table_1[2][10][8] = {};
- int number_of_splits = 2;
- long last_animation_millis = millis();
- long latest_animation_change = millis();
- int current_animation_table[2][7]; //animation_number, NUM_LEDS, led_offset, speed, table, var_1, i;
- long animation_millis[2];
- bool split_animations = false;
- //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
- extern void animation_rainbow(int); // forward declaration
- extern void animation_running(int); // forward declaration
- extern void animation_chasing(int);
- extern void animation_off(int);
- extern void animation_random1(int);
- extern void animation_rgb_light(int);
- extern void animation_random2(int);
- void (*animation_table[])(int) = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1, animation_rgb_light, animation_random2};
- extern void setup_rainbow(int); // forward declaration
- extern void setup_random1(int);
- extern void normal_setup(int);
- extern void setup_chosen(int, int, CRGB, boolean, int, int);
- extern void setup_random2(int);
- void (*setup_table[])(int) = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1, normal_setup, setup_random2};
- CRGB leds[NUM_LEDS];
- int status = WL_IDLE_STATUS;
- WiFiServer server(80);
- void setup() {
- current_animation_table[0][0] = 0;
- current_animation_table[0][1] = 150;
- current_animation_table[0][2] = 0;
- current_animation_table[1][0] = 0;
- current_animation_table[1][1] = 50;
- current_animation_table[1][2] = 150;
- animation_millis[0] = millis();
- animation_millis[1] = millis();
- setup_random1(0);
- // 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
- }
- Serial.println("Starting");
- FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS_WS).setCorrection( TypicalLEDStrip ); //TM1804 BRG //WS2812 GRB
- FastLED.addLeds<TM1804, DATA_PIN_2, BRG>(leds, NUM_LEDS_WS, NUM_LEDS_TM).setCorrection( TypicalLEDStrip ); //TM1804 BRG //WS2812 GRB
- 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);
- // }
- // }
- //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);
- }
- }
- 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);
- // }
- // }
- 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);
- // }
- // }
- delay(1000);
- //current_animation = 0;
- setup_table[0](0);
- setup_table[0](1);
- Serial.println("Started");
- }
- void loop() {
- loops_since_http += 1;
- if (loops_since_http < 100) {
- //Serial.println("In loop123123");
- long current_millis = millis();
- bool doneAnimationStep = false;
- for (int i = 0; i < number_of_splits; i++) {
- //Serial.println("In loop-1");
- //Serial.println(i);
- if (current_millis - animation_millis[i] >= current_animation_table[i][3]) {
- doneAnimationStep = true;
- //Serial.println("A");
- //current_animation_table[i][1] = 1;
- animation_table[current_animation_table[i][0]](i);
- // Serial.println(current_animation_table[0][0]);
- // Serial.println(current_animation_table[1][0]);
- animation_millis[i] = current_millis;
- //Serial.println("After loop");
- }
- //Serial.println("In loop-3");
- }
- if (doneAnimationStep) {
- FastLED.show();
- Serial.println(leds[170]);
- Serial.println(leds[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;
- //Serial.println("idle");
-
- } 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);
- }
- // 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
- int readingState = 0;
- 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;
- int setting = 0;
- int value = 0;
- int modify_animation = current_animation_table[0][0];
- int current_strip_half = 0;
- String fullArguments = "";
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //Serial.write(c);
- 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");
- 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 (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 == '0' || c == '1') {
- readingState = 1;
- split_animations = true;
- current_strip_half = String(c).toInt();
- if (c == '1') {
- modify_animation = current_animation_table[1][0];
- }
- continue;
- } else {
- split_animations = false;
- }
- if (c == 'y') {
- readingState = 3;
- continue;
- } if (c == 'z') {
- readingState = 2;
- continue;
- } if (c == '(') {
- anim = modify_animation - 1;
- if (anim < 0) {
- anim = NUMBER_OF_ANIMATIONS - 1;
- }
- } else if (c == ')') {
- anim = modify_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 == '/') {
- readingState = 1;
- }
- }
- }
- // give the web browser time to receive the data
- delay(300);
- // close the connection:
- client.stop();
- //Serial.println("client disconnected");
- if (readingState == 1) {
- doing_animation = true;
- setup_table[anim](current_strip_half);
- current_animation_table[current_strip_half][0] = anim;
- latest_animation_change = millis();
- Serial.println(current_strip_half);
- Serial.println(current_animation_table[current_strip_half][0]);
- Serial.println(anim);
- } else if (readingState == 3) {
- Serial.println(setting);
- Serial.println(value);
- if (setting == 1) {
- current_animation_table[current_strip_half][3] = value;
- //animation_step_time = value;
- } else if (setting == 2) {
- //animation_var_1 = value;
- current_animation_table[current_strip_half][5] = 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);
- setup_chosen(leds1, leds2, CRGB(newR, newG, newB), (boolean) doFade, cycles, cycleLength);
- }
- }
- }
- void clear_strip(int stripDiv) {
- for (int i = 0; i < current_animation_table[stripDiv][1]; i++) {
- leds[i + current_animation_table[stripDiv][2]] = CRGB(0, 0, 0);
- }
- //FastLED.show();
- }
- void animation_fade_help() {//TODO MAKE WORKABLE FOR SPLIT STRIP-------------------------------------------------------------------------------------------------------------------------------------------------------------
- // 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_chosen(int lamps1, int lamps2, CRGB newColor, boolean doFade = false, int cycles = 20, int cycleLength = 100) { //TODO-------------------------------------------------------------------------------------------------------------------------------------------------------------
- // 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();
- // }
- }
|