LedThing.ino.ino 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_fade_help() {
  247. int leds1 = animation_table_1[0][0];
  248. int leds2 = animation_table_1[0][1];
  249. int finnishR = animation_table_1[0][2];
  250. int finnishG = animation_table_1[0][3];
  251. int finnishB = animation_table_1[0][4];
  252. int cycles = animation_table_1[0][5];
  253. int startR = animation_table_1[0][6];
  254. int startG = animation_table_1[0][7];
  255. int startB = animation_table_1[1][0];
  256. uint8_t r = (finnishR - startR) * animation_i / cycles + startR;
  257. uint8_t g = (finnishG - startG) * animation_i / cycles + startG;
  258. uint8_t b = (finnishB - startB) * animation_i / cycles + startB;
  259. for (int i = leds1; i <= leds2; i++) {
  260. leds[i] = CRGB(r, g, b);
  261. }
  262. animation_i++;
  263. if (animation_i >= cycles) {
  264. for (int i = leds1; i < leds2; i++) {
  265. leds[i] = CRGB(finnishR, finnishG, finnishB);
  266. }
  267. animation_var_1 = 0;
  268. }
  269. FastLED.show();
  270. }
  271. void setup_chosen(int lamps1, int lamps2, CRGB newColor, boolean doFade = false, int cycles = 20, int cycleLength = 100) {
  272. animation_step_time = cycleLength;
  273. animation_var_1 = (int) doFade;
  274. animation_i = 0;
  275. doing_animation = false;
  276. if (doFade) {
  277. animation_table_1[0][0] = lamps1;
  278. animation_table_1[0][1] = lamps2;
  279. animation_table_1[0][2] = newColor.r;
  280. animation_table_1[0][3] = newColor.g;
  281. animation_table_1[0][4] = newColor.b;
  282. animation_table_1[0][5] = cycles;
  283. animation_table_1[0][6] = leds[lamps1].r;
  284. animation_table_1[0][7] = leds[lamps1].g;
  285. animation_table_1[1][0] = leds[lamps1].b;
  286. } else {
  287. for (int i = lamps1; i <= lamps2; i++) {
  288. leds[i] = newColor;
  289. }
  290. FastLED.show();
  291. }
  292. }