LedThing.ino.ino 13 KB

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