LedThing.ino.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 = 3;
  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. long last_animation_millis = millis();
  15. //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
  16. extern void animation_rainbow(); // forward declaration
  17. extern void animation_running(); // forward declaration
  18. extern void animation_chasing();
  19. void (*animation_table[])() = {animation_rainbow, animation_running, animation_chasing};
  20. extern void setup_rainbow(); // forward declaration
  21. extern void setup_running(); // forward declaration
  22. extern void setup_chasing();
  23. void (*setup_table[])() = {setup_rainbow, setup_running, setup_chasing};
  24. CRGB leds[NUM_LEDS];
  25. int status = WL_IDLE_STATUS;
  26. WiFiServer server(80);
  27. void setup() {
  28. // put your setup code here, to run once:
  29. //Serial.begin(9600);
  30. Serial.begin(9600);
  31. while (!Serial) {
  32. ; // wait for serial port to connect. Needed for native USB port only
  33. }
  34. //pinMode(DATA_PIN, OUTPUT);
  35. boolean first_check = true;
  36. while (status != WL_CONNECTED) {
  37. Serial.print("Attempting to connect to SSID: ");
  38. Serial.println(ssid);
  39. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  40. status = WiFi.begin(ssid, pass);
  41. // wait 10 seconds for connection:
  42. if (first_check) {
  43. delay(1000);
  44. first_check = false;
  45. } else {
  46. delay(10000);
  47. }
  48. }
  49. server.begin();
  50. delay(3000);
  51. FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  52. FastLED.setBrightness(255);
  53. FastLED.clear();
  54. FastLED.show();
  55. delay(1000);
  56. setup_table[current_animation]();
  57. }
  58. void loop() {
  59. loops_since_http += 1;
  60. long current_millis = millis();
  61. if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
  62. animation_table[current_animation]();
  63. last_animation_millis = current_millis;
  64. } else {
  65. loops_since_http = 0;
  66. //Serial.println("Waiting for animation");
  67. process_HTTP();
  68. delay(1);
  69. }
  70. // put your main code here, to run repeatedly:
  71. //Serial.println("Hello");
  72. //delay(1000);
  73. // leds[3] = CRGB::Blue;
  74. // leds[0] = CRGB::Red;
  75. // leds[1] = CRGB::Green;
  76. // leds[49] = CRGB::White;
  77. // FastLED.clear();
  78. // FastLED.show();
  79. // uint8_t delta = 10;
  80. // for (uint8_t i = 0; i < 360; i++) {
  81. // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
  82. // fill_rainbow(leds, NUM_LEDS, i, delta);
  83. // FastLED.show();
  84. // delay(10);
  85. // }
  86. }
  87. void process_HTTP() {
  88. WiFiClient client = server.available();
  89. if (client) {
  90. int anim = 0;
  91. Serial.println("new client");
  92. // an http request ends with a blank line
  93. boolean foundSlash = false;
  94. while (client.connected()) {
  95. if (client.available()) {
  96. char c = client.read();
  97. //Serial.write(c);
  98. if (foundSlash) {
  99. if (c == '(') {
  100. anim = current_animation - 1;
  101. if (anim < 0) {
  102. anim = NUMBER_OF_ANIMATIONS - 1;
  103. }
  104. } else if (c == ')') {
  105. anim = current_animation + 1;
  106. } else {
  107. anim = c - 'A';
  108. }
  109. if (anim < 0) {
  110. anim = -anim;
  111. }
  112. anim %= NUMBER_OF_ANIMATIONS;
  113. client.println("HTTP/1.1 200 OK");
  114. client.println("Content-Type: application/json");
  115. client.println("Connection: close"); // the connection will be closed after completion of the response
  116. client.println();
  117. client.println("{}");
  118. break;
  119. }
  120. if (c == '/') {
  121. foundSlash = true;
  122. }
  123. }
  124. }
  125. // give the web browser time to receive the data
  126. delay(1);
  127. // close the connection:
  128. client.stop();
  129. //Serial.println("client disconnected");
  130. setup_table[anim]();
  131. }
  132. }
  133. void clear_strip() {
  134. FastLED.clear();
  135. FastLED.show();
  136. }
  137. void animation_rainbow() {
  138. fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1);
  139. FastLED.show();
  140. animation_i++;
  141. animation_i %= 260;
  142. //Serial.println(animation_i);
  143. //Serial.println("Animation 1 step");
  144. }
  145. void animation_running() {
  146. //Serial.println("Animation 2 step");
  147. leds[animation_i] = CRGB(100, 255, 200);
  148. FastLED.show();
  149. animation_i++;
  150. if (animation_i >= 49) {
  151. animation_i = 0;
  152. clear_strip();
  153. }
  154. }
  155. void animation_chasing() {
  156. //Serial.println("Animation 2 step");
  157. clear_strip();
  158. leds[animation_i] = CRGB(100, 255, 200);
  159. leds[(animation_i + 10) % 50] = CRGB(100, 255, 200);
  160. leds[(animation_i + 20) % 50] = CRGB(100, 255, 200);
  161. FastLED.show();
  162. animation_i++;
  163. animation_i %= 50;
  164. }
  165. void setup_rainbow() {
  166. Serial.println("Animation 1 setup");
  167. clear_strip();
  168. animation_step_time = 100;
  169. animation_i = 0;
  170. animation_var_1 = 10;
  171. current_animation = 0;
  172. }
  173. void setup_running() {
  174. Serial.println("Animation 2 setup");
  175. clear_strip();
  176. animation_step_time = 100;
  177. animation_i = 0;
  178. animation_var_1 = 0;
  179. current_animation = 1;
  180. }
  181. void setup_chasing() {
  182. Serial.println("Animation 3 setup");
  183. clear_strip();
  184. animation_step_time = 100;
  185. animation_i = 0;
  186. animation_var_1 = 0;
  187. current_animation = 2;
  188. }