LedThing.ino.ino 14 KB

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