LedThing.ino.ino 15 KB

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