LedThing.ino.ino 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. int current_animation = 0;
  10. int loops_since_http = 0;
  11. int animation_step_time = 0;
  12. int animation_i = 0;
  13. int animation_var_1 = 0;
  14. uint8_t animation_table_1[10][8] = {};
  15. long last_animation_millis = millis();
  16. long latest_animation_change = millis();
  17. //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
  18. extern void animation_rainbow(); // forward declaration
  19. extern void animation_running(); // forward declaration
  20. extern void animation_chasing();
  21. extern void animation_off();
  22. extern void animation_random1();
  23. void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1};
  24. extern void setup_rainbow(); // forward declaration
  25. extern void setup_running(); // forward declaration
  26. extern void setup_chasing();
  27. extern void setup_random1();
  28. void (*setup_table[])() = {animation_off, setup_rainbow, setup_running, setup_chasing, setup_random1};
  29. CRGB leds[NUM_LEDS];
  30. int status = WL_IDLE_STATUS;
  31. WiFiServer server(80);
  32. void setup() {
  33. setup_random1();
  34. // put your setup code here, to run once:
  35. //Serial.begin(9600);
  36. Serial.begin(9600);
  37. while (!Serial) {
  38. ; // wait for serial port to connect. Needed for native USB port only
  39. }
  40. //pinMode(DATA_PIN, OUTPUT);
  41. boolean first_check = true;
  42. while (status != WL_CONNECTED) {
  43. Serial.print("Attempting to connect to SSID: ");
  44. Serial.println(ssid);
  45. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  46. status = WiFi.begin(ssid, pass);
  47. // wait 10 seconds for connection:
  48. if (first_check) {
  49. delay(1000);
  50. first_check = false;
  51. } else {
  52. delay(10000);
  53. }
  54. }
  55. server.begin();
  56. delay(3000);
  57. FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  58. FastLED.setBrightness(255);
  59. FastLED.clear();
  60. FastLED.show();
  61. delay(1000);
  62. setup_table[current_animation]();
  63. }
  64. void loop() {
  65. loops_since_http += 1;
  66. long current_millis = millis();
  67. if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
  68. animation_table[current_animation]();
  69. last_animation_millis = current_millis;
  70. } else {
  71. loops_since_http = 0;
  72. //Serial.println("Waiting for animation");
  73. process_HTTP();
  74. delay(1);
  75. }
  76. // put your main code here, to run repeatedly:
  77. //Serial.println("Hello");
  78. //delay(1000);
  79. // leds[3] = CRGB::Blue;
  80. // leds[0] = CRGB::Red;
  81. // leds[1] = CRGB::Green;
  82. // leds[49] = CRGB::White;
  83. // FastLED.clear();
  84. // FastLED.show();
  85. // uint8_t delta = 10;
  86. // for (uint8_t i = 0; i < 360; i++) {
  87. // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
  88. // fill_rainbow(leds, NUM_LEDS, i, delta);
  89. // FastLED.show();
  90. // delay(10);
  91. // }
  92. }
  93. void process_HTTP() {
  94. WiFiClient client = server.available();
  95. if (client && millis() - latest_animation_change > 1000) {
  96. int anim = 0;
  97. Serial.println("new client");
  98. // an http request ends with a blank line
  99. boolean foundSlash = false;
  100. while (client.connected()) {
  101. if (client.available()) {
  102. char c = client.read();
  103. //Serial.write(c);
  104. if (foundSlash) {
  105. if (c == '(') {
  106. anim = current_animation - 1;
  107. if (anim < 0) {
  108. anim = NUMBER_OF_ANIMATIONS - 1;
  109. }
  110. } else if (c == ')') {
  111. anim = current_animation + 1;
  112. } else {
  113. anim = c - 'A';
  114. }
  115. if (anim < 0) {
  116. anim = -anim;
  117. }
  118. anim %= NUMBER_OF_ANIMATIONS;
  119. client.println("HTTP/1.1 200 OK");
  120. client.println("Content-Type: text/plain");
  121. client.println("Connection: close"); // the connection will be closed after completion of the response
  122. client.println();
  123. client.println("OK");
  124. break;
  125. }
  126. if (c == '/') {
  127. foundSlash = true;
  128. }
  129. }
  130. }
  131. // give the web browser time to receive the data
  132. delay(300);
  133. // close the connection:
  134. client.stop();
  135. //Serial.println("client disconnected");
  136. setup_table[anim]();
  137. current_animation = anim;
  138. latest_animation_change = millis();
  139. }
  140. }
  141. void clear_strip() {
  142. FastLED.clear();
  143. FastLED.show();
  144. }
  145. void animation_rainbow() {
  146. fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1);
  147. FastLED.show();
  148. animation_i++;
  149. animation_i %= 260;
  150. //Serial.println(animation_i);
  151. //Serial.println("Animation 1 step");
  152. }
  153. void animation_running() {
  154. //Serial.println("Animation 2 step");
  155. leds[animation_i] = CRGB(100, 255, 200);
  156. FastLED.show();
  157. animation_i++;
  158. if (animation_i >= 50) {
  159. animation_i = 0;
  160. clear_strip();
  161. }
  162. }
  163. void animation_chasing() {
  164. //Serial.println("Animation 2 step");
  165. clear_strip();
  166. leds[animation_i] = CRGB(100, 255, 200);
  167. leds[(animation_i + 10) % 50] = CRGB(100, 255, 200);
  168. leds[(animation_i + 20) % 50] = CRGB(100, 255, 200);
  169. leds[(50 - animation_i) - 1] = CRGB(0, 255, 255);
  170. // leds[(50 - animation_i - 10)] = CRGB(0, 255, 255);
  171. // leds[(50 - animation_i)] = CRGB(0, 255, 255);
  172. FastLED.show();
  173. animation_i++;
  174. animation_i %= 50;
  175. }
  176. void animation_off() {
  177. clear_strip();
  178. animation_step_time = 10000;
  179. }
  180. void animation_random1() {
  181. for (int i = 0; i < NUM_LEDS/5; i++) {
  182. int howManyCycles = animation_table_1[i][7];
  183. int currentStep = animation_table_1[i][6];
  184. uint8_t startRed = animation_table_1[i][0];
  185. uint8_t startGreen = animation_table_1[i][1];
  186. uint8_t startBlue = animation_table_1[i][2];
  187. uint8_t goalRed = animation_table_1[i][3];
  188. uint8_t goalGreen = animation_table_1[i][4];
  189. uint8_t goalBlue = animation_table_1[i][5];
  190. uint8_t r = (goalRed - startRed) * currentStep / howManyCycles + startRed;
  191. uint8_t g = (goalGreen - startGreen) * currentStep / howManyCycles + startGreen;
  192. uint8_t b = (goalBlue - startBlue) * currentStep / howManyCycles + startBlue;
  193. for (int led = 0; led < 5; led++) {
  194. leds[led + i*5] = CRGB(r, b, g);
  195. }
  196. animation_table_1[i][6] += 1;
  197. if (currentStep >= howManyCycles) {
  198. uint8_t newR = (uint8_t) random(0, 255);
  199. uint8_t newG = (uint8_t) random(0, 255);
  200. uint8_t newB = (uint8_t) random(0, 255);
  201. animation_table_1[i][3] = newR;
  202. animation_table_1[i][4] = newG;
  203. animation_table_1[i][5] = newB;
  204. uint8_t steps = (uint8_t) random(10, 50);
  205. animation_table_1[i][0] = r;
  206. animation_table_1[i][1] = g;
  207. animation_table_1[i][2] = b;
  208. animation_table_1[i][7] = steps;
  209. animation_table_1[i][6] = 0;
  210. if (i == 0) {
  211. Serial.println(newR);
  212. Serial.println(newG);
  213. Serial.println(newB);
  214. }
  215. }
  216. // uint8_t r = leds[i*5].r + animation_table_1[i][0];
  217. // uint8_t g = leds[i*5].g + animation_table_1[i][1];
  218. // uint8_t b = leds[i*5].b + animation_table_1[i][2];
  219. // if (i == 0) {
  220. // Serial.println(r);
  221. // Serial.println(g);
  222. // Serial.println(b);
  223. // }
  224. // boolean rDone = false;
  225. // boolean gDone = false;
  226. // boolean bDone = false;
  227. // if (animation_table_1[i][0] > 0 && r >= animation_table_1[i][3]) {
  228. // r = animation_table_1[i][3];
  229. // rDone = true;
  230. // }
  231. // if (animation_table_1[i][1] > 0 && g >= animation_table_1[i][4]) {
  232. // g = animation_table_1[i][4];
  233. // gDone = true;
  234. // }
  235. // if (animation_table_1[i][2] > 0 && b >= animation_table_1[i][5]) {
  236. // b = animation_table_1[i][5];
  237. // bDone = true;
  238. // }
  239. // if (animation_table_1[i][0] <= 0 && r <= animation_table_1[i][3]) {
  240. // r = animation_table_1[i][3];
  241. // rDone = true;
  242. // }
  243. // if (animation_table_1[i][1] <= 0 && g <= animation_table_1[i][4]) {
  244. // g = animation_table_1[i][4];
  245. // gDone = true;
  246. // }
  247. // if (animation_table_1[i][2] <= 0 && b <= animation_table_1[i][5]) {
  248. // b = animation_table_1[i][5];
  249. // bDone = true;
  250. // }
  251. // if (i == 0) {
  252. // Serial.println(rDone);
  253. // Serial.println(gDone);
  254. // Serial.println(bDone);
  255. // }
  256. //
  257. // for (int led = 0; led < 5; led++) {
  258. // leds[led + i*5] = CRGB(r, b, g);
  259. // }
  260. //
  261. // if (rDone && gDone && bDone) {
  262. // if (i == 0) {
  263. // Serial.println("New color");
  264. // } else {
  265. // Serial.println("New color other i");
  266. // }
  267. // uint8_t newR = (uint8_t) random(0, 255);
  268. // uint8_t newG = (uint8_t) random(0, 255);
  269. // uint8_t newB = (uint8_t) random(0, 255);
  270. // animation_table_1[i][3] = newR;
  271. // animation_table_1[i][4] = newG;
  272. // animation_table_1[i][5] = newB;
  273. //
  274. // uint8_t steps = (uint8_t) random(10, 50);
  275. // uint8_t newRStep = (uint8_t) (newR - r)/steps;
  276. // uint8_t newGStep = (uint8_t) (newG - g)/steps;
  277. // uint8_t newBStep = (uint8_t) (newB - b)/steps;
  278. // if (newRStep < 1) {
  279. // newRStep = 1;
  280. // }
  281. // if (newGStep < 1) {
  282. // newGStep = 1;
  283. // }
  284. // if (newBStep < 1) {
  285. // newBStep = 1;
  286. // }
  287. // animation_table_1[i][0] = newRStep;
  288. // animation_table_1[i][1] = newGStep;
  289. // animation_table_1[i][2] = newBStep;
  290. // if (i == 0) {
  291. // Serial.println(newR);
  292. // Serial.println(newG);
  293. // Serial.println(newB);
  294. // }
  295. // }
  296. }
  297. for (int i = 0; i < NUM_LEDS/5; i++) {
  298. uint8_t startRed = leds[i*5].r;
  299. uint8_t startGreen = leds[i*5].g;
  300. uint8_t startBlue = leds[i*5].b;
  301. uint8_t goalRed = leds[((i+1)*5)%NUM_LEDS].r;
  302. uint8_t goalGreen = leds[((i+1)*5)%NUM_LEDS].g;
  303. uint8_t goalBlue = leds[((i+1)*5)%NUM_LEDS].b;
  304. for (int currentStep = 1; currentStep < 5; currentStep++) {
  305. uint8_t r = (goalRed - startRed) * currentStep / 5 + startRed;
  306. uint8_t g = (goalGreen - startGreen) * currentStep / 5 + startGreen;
  307. uint8_t b = (goalBlue - startBlue) * currentStep / 5 + startBlue;
  308. leds[i*5+currentStep] = CRGB(r, g, b);
  309. }
  310. }
  311. FastLED.show();
  312. }
  313. void setup_rainbow() {
  314. normal_setup();
  315. animation_var_1 = 10;
  316. }
  317. void normal_setup() {
  318. clear_strip();
  319. animation_step_time = 100;
  320. animation_i = 0;
  321. animation_var_1 = 0;
  322. }
  323. void setup_running() {
  324. normal_setup();
  325. }
  326. void setup_chasing() {
  327. normal_setup();
  328. }
  329. void setup_random1() {
  330. for (int i = 0; i < NUM_LEDS; i++) {
  331. for (int i = 0; i < 8; i++) {
  332. animation_table_1[i][i] = 0;
  333. }
  334. //= {0, 0, 0, 0, 0, 0}; //Step size R, Step size G, Step size B, fR, fG, fB
  335. //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
  336. }
  337. normal_setup();
  338. animation_step_time = 500;
  339. }