LedThing.ino.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 %= 256;
  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. uint8_t newH = (uint8_t) random(0, 255);
  202. uint8_t newS = (uint8_t) random(200, 255);
  203. CRGB newColor = CRGB(0, 0, 0);
  204. newColor.setHSV(newH, newS, 255);
  205. Serial.println(newColor.r);
  206. Serial.println(newColor.g);
  207. Serial.println(newColor.b);
  208. animation_table_1[i][3] = newColor.r;
  209. animation_table_1[i][4] = newColor.g;
  210. animation_table_1[i][5] = newColor.b;
  211. uint8_t steps = (uint8_t) random(10, 50);
  212. animation_table_1[i][0] = r;
  213. animation_table_1[i][1] = g;
  214. animation_table_1[i][2] = b;
  215. animation_table_1[i][7] = steps;
  216. animation_table_1[i][6] = 0;
  217. }
  218. // uint8_t r = leds[i*5].r + animation_table_1[i][0];
  219. // uint8_t g = leds[i*5].g + animation_table_1[i][1];
  220. // uint8_t b = leds[i*5].b + animation_table_1[i][2];
  221. // if (i == 0) {
  222. // Serial.println(r);
  223. // Serial.println(g);
  224. // Serial.println(b);
  225. // }
  226. // boolean rDone = false;
  227. // boolean gDone = false;
  228. // boolean bDone = false;
  229. // if (animation_table_1[i][0] > 0 && r >= animation_table_1[i][3]) {
  230. // r = animation_table_1[i][3];
  231. // rDone = true;
  232. // }
  233. // if (animation_table_1[i][1] > 0 && g >= animation_table_1[i][4]) {
  234. // g = animation_table_1[i][4];
  235. // gDone = true;
  236. // }
  237. // if (animation_table_1[i][2] > 0 && b >= animation_table_1[i][5]) {
  238. // b = animation_table_1[i][5];
  239. // bDone = true;
  240. // }
  241. // if (animation_table_1[i][0] <= 0 && r <= animation_table_1[i][3]) {
  242. // r = animation_table_1[i][3];
  243. // rDone = true;
  244. // }
  245. // if (animation_table_1[i][1] <= 0 && g <= animation_table_1[i][4]) {
  246. // g = animation_table_1[i][4];
  247. // gDone = true;
  248. // }
  249. // if (animation_table_1[i][2] <= 0 && b <= animation_table_1[i][5]) {
  250. // b = animation_table_1[i][5];
  251. // bDone = true;
  252. // }
  253. // if (i == 0) {
  254. // Serial.println(rDone);
  255. // Serial.println(gDone);
  256. // Serial.println(bDone);
  257. // }
  258. //
  259. // for (int led = 0; led < 5; led++) {
  260. // leds[led + i*5] = CRGB(r, b, g);
  261. // }
  262. //
  263. // if (rDone && gDone && bDone) {
  264. // if (i == 0) {
  265. // Serial.println("New color");
  266. // } else {
  267. // Serial.println("New color other i");
  268. // }
  269. // uint8_t newR = (uint8_t) random(0, 255);
  270. // uint8_t newG = (uint8_t) random(0, 255);
  271. // uint8_t newB = (uint8_t) random(0, 255);
  272. // animation_table_1[i][3] = newR;
  273. // animation_table_1[i][4] = newG;
  274. // animation_table_1[i][5] = newB;
  275. //
  276. // uint8_t steps = (uint8_t) random(10, 50);
  277. // uint8_t newRStep = (uint8_t) (newR - r)/steps;
  278. // uint8_t newGStep = (uint8_t) (newG - g)/steps;
  279. // uint8_t newBStep = (uint8_t) (newB - b)/steps;
  280. // if (newRStep < 1) {
  281. // newRStep = 1;
  282. // }
  283. // if (newGStep < 1) {
  284. // newGStep = 1;
  285. // }
  286. // if (newBStep < 1) {
  287. // newBStep = 1;
  288. // }
  289. // animation_table_1[i][0] = newRStep;
  290. // animation_table_1[i][1] = newGStep;
  291. // animation_table_1[i][2] = newBStep;
  292. // if (i == 0) {
  293. // Serial.println(newR);
  294. // Serial.println(newG);
  295. // Serial.println(newB);
  296. // }
  297. // }
  298. }
  299. for (int i = 0; i < NUM_LEDS/5; i++) {
  300. uint8_t startRed = leds[i*5].r;
  301. uint8_t startGreen = leds[i*5].g;
  302. uint8_t startBlue = leds[i*5].b;
  303. uint8_t goalRed = leds[((i+1)*5)%NUM_LEDS].r;
  304. uint8_t goalGreen = leds[((i+1)*5)%NUM_LEDS].g;
  305. uint8_t goalBlue = leds[((i+1)*5)%NUM_LEDS].b;
  306. for (int currentStep = 1; currentStep < 5; currentStep++) {
  307. uint8_t r = (goalRed - startRed) * currentStep / 5 + startRed;
  308. uint8_t g = (goalGreen - startGreen) * currentStep / 5 + startGreen;
  309. uint8_t b = (goalBlue - startBlue) * currentStep / 5 + startBlue;
  310. leds[i*5+currentStep] = CRGB(r, g, b);
  311. }
  312. }
  313. FastLED.show();
  314. }
  315. void setup_rainbow() {
  316. normal_setup();
  317. animation_var_1 = 10;
  318. }
  319. void normal_setup() {
  320. clear_strip();
  321. animation_step_time = 100;
  322. animation_i = 0;
  323. animation_var_1 = 0;
  324. }
  325. void setup_running() {
  326. normal_setup();
  327. }
  328. void setup_chasing() {
  329. normal_setup();
  330. }
  331. void setup_random1() {
  332. for (int i = 0; i < NUM_LEDS; i++) {
  333. for (int i = 0; i < 8; i++) {
  334. animation_table_1[i][i] = 0;
  335. }
  336. //= {0, 0, 0, 0, 0, 0}; //Step size R, Step size G, Step size B, fR, fG, fB
  337. //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
  338. }
  339. normal_setup();
  340. animation_step_time = 500;
  341. }