LedThing.ino.ino 14 KB

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