LedThing.ino.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "FastLED.h"
  2. #include <SPI.h>
  3. #include <WiFiNINA.h>
  4. #define NUM_LEDS 150
  5. //50
  6. #define DATA_PIN 9
  7. int NUMBER_OF_ANIMATIONS = 7;
  8. int NUMBER_OF_LEDS = 150;
  9. char ssid[] = "Telia-E39815";
  10. char pass[] = "B3C606D8E7";
  11. boolean doing_animation = true;
  12. int current_animation = 0;
  13. int loops_since_http = 0;
  14. int animation_step_time = 0;
  15. int animation_i = 0;
  16. int animation_var_1 = 0;
  17. int animation_table_1[10][8] = {};
  18. long last_animation_millis = millis();
  19. long latest_animation_change = millis();
  20. //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
  21. extern void animation_rainbow(); // forward declaration
  22. extern void animation_running(); // forward declaration
  23. extern void animation_chasing();
  24. extern void animation_off();
  25. extern void animation_random1();
  26. extern void animation_rgb_light();
  27. extern void animation_random2();
  28. void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1, animation_rgb_light, animation_random2};
  29. extern void setup_rainbow(); // forward declaration
  30. extern void setup_random1();
  31. extern void normal_setup();
  32. extern void setup_chosen(int, int, CRGB, boolean, int, int);
  33. extern void setup_random2();
  34. void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1, normal_setup, setup_random2};
  35. CRGB leds[NUM_LEDS];
  36. int status = WL_IDLE_STATUS;
  37. WiFiServer server(80);
  38. void setup() {
  39. setup_random1();
  40. // put your setup code here, to run once:
  41. //Serial.begin(9600);
  42. Serial.begin(9600);
  43. while (!Serial) {
  44. ; // wait for serial port to connect. Needed for native USB port only
  45. }
  46. Serial.println("Starting");
  47. FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); //TM1804 BRG
  48. FastLED.setBrightness(255);
  49. FastLED.clear();
  50. FastLED.show();
  51. Serial.println("Connected LEDs");
  52. delay(1000);
  53. // for (int i = 15; i >= 10; i--) { //Remove this when small spce left
  54. // uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
  55. // for (int currentLed = 5; currentLed >= 0; currentLed--) {
  56. // leds[currentLed + i*5] = CRGB(color, 255-color, 0);
  57. // leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
  58. // FastLED.show();
  59. // delay(5*i/3 + random(0, 25*i + 1) + 1);
  60. // }
  61. // }
  62. //pinMode(DATA_PIN, OUTPUT);
  63. boolean first_check = true;
  64. while (status != WL_CONNECTED) {
  65. Serial.print("Attempting to connect to SSID: ");
  66. Serial.println(ssid);
  67. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  68. status = WiFi.begin(ssid, pass);
  69. // wait 10 seconds for connection:
  70. if (first_check) {
  71. delay(1000);
  72. first_check = false;
  73. } else {
  74. delay(10000);
  75. }
  76. }
  77. Serial.println("Connected to WiFi");
  78. // for (int i = 10; i >= 5; i--) { //Remove this when small spce left
  79. // uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
  80. // for (int currentLed = 5; currentLed >= 0; currentLed--) {
  81. // leds[currentLed + i*5] = CRGB(color, 255-color, 0);
  82. // leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
  83. // FastLED.show();
  84. // delay(5*i/3 + random(0, 25*i + 1) + 1);
  85. // }
  86. // }
  87. Serial.println(WiFi.localIP());
  88. server.begin();
  89. delay(1000);
  90. Serial.println("Ready");
  91. // for (int i = 5; i >= 0; i--) { //insert this when small space -> for (int i = (NUM_LEDS / 5) / 2; i >= 0; i--) {
  92. // uint8_t color = i*(NUM_LEDS/10 - 1) + NUM_LEDS/10;
  93. // for (int currentLed = 5; currentLed >= 0; currentLed--) {
  94. // leds[currentLed + i*5] = CRGB(color, 255-color, 0);
  95. // leds[5-currentLed + NUM_LEDS/2-5 + ((NUM_LEDS/10 - i)*5)] = CRGB(color, 255-color, 0);
  96. // FastLED.show();
  97. // delay(5*i/3 + random(0, 25*i + 1) + 1);
  98. // }
  99. // }
  100. delay(1000);
  101. current_animation = 0;
  102. setup_table[current_animation]();
  103. Serial.println("Started");
  104. }
  105. void loop() {
  106. loops_since_http += 1;
  107. long current_millis = millis();
  108. if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
  109. if (doing_animation) {
  110. animation_table[current_animation]();
  111. } else {
  112. if (animation_var_1 == 1) {
  113. animation_fade_help();
  114. } else {
  115. delay(5);
  116. }
  117. }
  118. last_animation_millis = current_millis;
  119. } else {
  120. loops_since_http = 0;
  121. //Serial.println("Waiting for animation");
  122. status=WiFi.status();
  123. if (status == WL_DISCONNECTED || status == WL_CONNECTION_LOST) {
  124. while (status != WL_CONNECTED) {
  125. status = WiFi.begin(ssid, pass);
  126. delay(1000);
  127. }
  128. }
  129. process_HTTP();
  130. delay(1);
  131. }
  132. // put your main code here, to run repeatedly:
  133. //Serial.println("Hello");
  134. //delay(1000);
  135. // leds[3] = CRGB::Blue;
  136. // leds[0] = CRGB::Red;
  137. // leds[1] = CRGB::Green;
  138. // leds[49] = CRGB::White;
  139. // FastLED.clear();
  140. // FastLED.show();
  141. // uint8_t delta = 10;
  142. // for (uint8_t i = 0; i < 360; i++) {
  143. // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
  144. // fill_rainbow(leds, NUM_LEDS, i, delta);
  145. // FastLED.show();
  146. // delay(10);
  147. // }
  148. }
  149. void process_HTTP() {
  150. WiFiClient client = server.available();
  151. if (client && millis() - latest_animation_change > 1000) {
  152. int anim = 0;
  153. Serial.println("new client");
  154. // an http request ends with a blank line
  155. int readingState = 0;
  156. int leds1 = 0;
  157. int leds2 = 0;
  158. uint8_t newR = 0;
  159. uint8_t newG = 0;
  160. uint8_t newB = 0;
  161. int doFade = 0; //Between 0 and 1
  162. int cycles = 20;
  163. int cycleLength = 100;
  164. int currentLoop = 0;
  165. int setting = 0;
  166. int value = 0;
  167. String fullArguments = "";
  168. while (client.connected()) {
  169. if (client.available()) {
  170. char c = client.read();
  171. //Serial.write(c);
  172. if (readingState == 2) { //http://192.168.112.41/z0:49:0:255:0:0:20:1000;
  173. if (c == ';') {
  174. client.println("HTTP/1.1 200 OK");
  175. client.println("Content-Type: text/plain");
  176. client.println("Connection: close"); // the connection will be closed after completion of the response
  177. client.println();
  178. client.println("OK");
  179. break;
  180. } else if (c == ':') {
  181. if (currentLoop == 0) {
  182. leds1 = max(fullArguments.toInt(), 0);
  183. } else if (currentLoop == 1) {
  184. leds2 = min(fullArguments.toInt(), NUM_LEDS - 1);
  185. } else if (currentLoop == 2) {
  186. newR = min(max(fullArguments.toInt(), 0), 255);
  187. } else if (currentLoop == 3) {
  188. newG = min(max(fullArguments.toInt(), 0), 255);
  189. } else if (currentLoop == 4) {
  190. newB = min(max(fullArguments.toInt(), 0), 255);
  191. } else if (currentLoop == 5) {
  192. doFade = fullArguments.toInt() % 2;
  193. } else if (currentLoop == 6) {
  194. cycles = fullArguments.toInt();
  195. } else if (currentLoop == 7) {
  196. cycleLength = min(fullArguments.toInt(), 40);
  197. }
  198. fullArguments = "";
  199. currentLoop++;
  200. } else {
  201. fullArguments = fullArguments + c;
  202. }
  203. } else if (readingState == 3) {
  204. Serial.println("Inside readingState == 3");
  205. if (c == ';') {
  206. client.println("HTTP/1.1 200 OK");
  207. client.println("Content-Type: text/plain");
  208. client.println("Connection: close"); // the connection will be closed after completion of the response
  209. client.println();
  210. client.println("OK");
  211. break;
  212. } else if (c == ':') {
  213. if (currentLoop == 0) {
  214. setting = fullArguments.toInt();
  215. } else if (currentLoop == 1) {
  216. value = fullArguments.toInt();
  217. Serial.println(value);
  218. }
  219. fullArguments = "";
  220. currentLoop++;
  221. Serial.println(currentLoop);
  222. } else {
  223. fullArguments = fullArguments + c;
  224. Serial.println(fullArguments);
  225. }
  226. } else if (readingState != 0) {
  227. if (c == 'y') {
  228. readingState = 3;
  229. continue;
  230. } if (c == 'z') {
  231. readingState = 2;
  232. continue;
  233. } else if (c == '(') {
  234. anim = current_animation - 1;
  235. if (anim < 0) {
  236. anim = NUMBER_OF_ANIMATIONS - 1;
  237. }
  238. } else if (c == ')') {
  239. anim = current_animation + 1;
  240. } else {
  241. anim = c - 'A';
  242. }
  243. if (anim < 0) {
  244. anim = -anim;
  245. }
  246. anim %= NUMBER_OF_ANIMATIONS;
  247. client.println("HTTP/1.1 200 OK");
  248. client.println("Content-Type: text/plain");
  249. client.println("Connection: close"); // the connection will be closed after completion of the response
  250. client.println();
  251. client.println("OK");
  252. break;
  253. }
  254. if (c == '/') {
  255. readingState = 1;
  256. }
  257. }
  258. }
  259. // give the web browser time to receive the data
  260. delay(300);
  261. // close the connection:
  262. client.stop();
  263. //Serial.println("client disconnected");
  264. if (readingState == 1) {
  265. doing_animation = true;
  266. setup_table[anim]();
  267. current_animation = anim;
  268. latest_animation_change = millis();
  269. } else if (readingState == 3) {
  270. Serial.println(setting);
  271. Serial.println(value);
  272. if (setting == 1) {
  273. animation_step_time = value;
  274. } else if (setting == 2) {
  275. animation_var_1 = value;
  276. }
  277. } else if (readingState == 2) {
  278. doing_animation = false;
  279. // Serial.println(leds1);
  280. // Serial.println(leds2);
  281. // Serial.println(newR);
  282. // Serial.println(newG);
  283. // Serial.println(newB);
  284. // Serial.println(doFade);
  285. setup_chosen(leds1, leds2, CRGB(newR, newG, newB), (boolean) doFade, cycles, cycleLength);
  286. }
  287. }
  288. }
  289. void clear_strip() {
  290. FastLED.clear();
  291. FastLED.show();
  292. }
  293. void animation_fade_help() {
  294. int leds1 = animation_table_1[0][0];
  295. int leds2 = animation_table_1[0][1];
  296. int finnishR = animation_table_1[0][2];
  297. int finnishG = animation_table_1[0][3];
  298. int finnishB = animation_table_1[0][4];
  299. int cycles = animation_table_1[0][5];
  300. int startR = animation_table_1[0][6];
  301. int startG = animation_table_1[0][7];
  302. int startB = animation_table_1[1][0];
  303. uint8_t r = (finnishR - startR) * animation_i / cycles + startR;
  304. uint8_t g = (finnishG - startG) * animation_i / cycles + startG;
  305. uint8_t b = (finnishB - startB) * animation_i / cycles + startB;
  306. for (int i = leds1; i <= leds2; i++) {
  307. leds[i] = CRGB(r, g, b);
  308. }
  309. animation_i++;
  310. if (animation_i >= cycles) {
  311. for (int i = leds1; i < leds2; i++) {
  312. leds[i] = CRGB(finnishR, finnishG, finnishB);
  313. }
  314. animation_var_1 = 0;
  315. }
  316. FastLED.show();
  317. }
  318. void setup_chosen(int lamps1, int lamps2, CRGB newColor, boolean doFade = false, int cycles = 20, int cycleLength = 100) {
  319. animation_step_time = cycleLength;
  320. animation_var_1 = (int) doFade;
  321. animation_i = 0;
  322. doing_animation = false;
  323. if (doFade) {
  324. animation_table_1[0][0] = lamps1;
  325. animation_table_1[0][1] = lamps2;
  326. animation_table_1[0][2] = newColor.r;
  327. animation_table_1[0][3] = newColor.g;
  328. animation_table_1[0][4] = newColor.b;
  329. animation_table_1[0][5] = cycles;
  330. animation_table_1[0][6] = leds[lamps1].r;
  331. animation_table_1[0][7] = leds[lamps1].g;
  332. animation_table_1[1][0] = leds[lamps1].b;
  333. } else {
  334. for (int i = lamps1; i <= lamps2; i++) {
  335. leds[i] = newColor;
  336. }
  337. FastLED.show();
  338. }
  339. }