LedThing.ino.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "FastLED.h"
  2. #include <SPI.h>
  3. #include <WiFiNINA.h>
  4. #define NUM_LEDS 50
  5. #define DATA_PIN 9
  6. char ssid[] = "Pannkakshuset";
  7. char pass[] = "lavalampa";
  8. int current_animation = 0;
  9. int loops_since_http = 0;
  10. int animation_step_time = 0;
  11. int animation_i = 0;
  12. int animation_var_1 = 0;
  13. long last_animation_millis = millis();
  14. //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
  15. extern void animation_rainbow(); // forward declaration
  16. extern void animation_running(); // forward declaration
  17. void (*animation_table[])() = {animation_rainbow, animation_running};
  18. extern void setup_rainbow(); // forward declaration
  19. extern void setup_running(); // forward declaration
  20. void (*setup_table[])() = {setup_rainbow, setup_running};
  21. CRGB leds[NUM_LEDS];
  22. int status = WL_IDLE_STATUS;
  23. WiFiServer server(80);
  24. void setup() {
  25. // put your setup code here, to run once:
  26. //Serial.begin(9600);
  27. Serial.begin(9600);
  28. while (!Serial) {
  29. ; // wait for serial port to connect. Needed for native USB port only
  30. }
  31. //pinMode(DATA_PIN, OUTPUT);
  32. while (status != WL_CONNECTED) {
  33. Serial.print("Attempting to connect to SSID: ");
  34. Serial.println(ssid);
  35. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  36. status = WiFi.begin(ssid, pass);
  37. // wait 10 seconds for connection:
  38. delay(10000);
  39. }
  40. server.begin();
  41. delay(3000);
  42. FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  43. FastLED.setBrightness(255);
  44. FastLED.clear();
  45. FastLED.show();
  46. delay(1000);
  47. setup_table[current_animation]();
  48. }
  49. void loop() {
  50. loops_since_http += 1;
  51. long current_millis = millis();
  52. if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
  53. animation_table[current_animation]();
  54. last_animation_millis = current_millis;
  55. } else {
  56. loops_since_http = 0;
  57. //Serial.println("Waiting for animation");
  58. process_HTTP();
  59. delay(1);
  60. }
  61. // put your main code here, to run repeatedly:
  62. //Serial.println("Hello");
  63. //delay(1000);
  64. // leds[3] = CRGB::Blue;
  65. // leds[0] = CRGB::Red;
  66. // leds[1] = CRGB::Green;
  67. // leds[49] = CRGB::White;
  68. // FastLED.clear();
  69. // FastLED.show();
  70. // uint8_t delta = 10;
  71. // for (uint8_t i = 0; i < 360; i++) {
  72. // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
  73. // fill_rainbow(leds, NUM_LEDS, i, delta);
  74. // FastLED.show();
  75. // delay(10);
  76. // }
  77. }
  78. void process_HTTP() {
  79. WiFiClient client = server.available();
  80. if (client) {
  81. int anim = 0;
  82. Serial.println("new client");
  83. // an http request ends with a blank line
  84. boolean foundSlash = false;
  85. while (client.connected()) {
  86. if (client.available()) {
  87. char c = client.read();
  88. //Serial.write(c);
  89. if (foundSlash) {
  90. anim = c - 'A';
  91. client.println("HTTP/1.1 200 OK");
  92. break;
  93. }
  94. if (c == '/') {
  95. foundSlash = true;
  96. }
  97. }
  98. }
  99. // give the web browser time to receive the data
  100. delay(1);
  101. // close the connection:
  102. client.stop();
  103. Serial.println("client disconnected");
  104. setup_table[anim]();
  105. }
  106. }
  107. void clear_strip() {
  108. FastLED.clear();
  109. FastLED.show();
  110. }
  111. void animation_rainbow() {
  112. fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1);
  113. FastLED.show();
  114. animation_i++;
  115. animation_i %= 260;
  116. //Serial.println(animation_i);
  117. //Serial.println("Animation 1 step");
  118. }
  119. void animation_running() {
  120. //Serial.println("Animation 2 step");
  121. leds[animation_i] = CRGB(100, 255, 200);
  122. FastLED.show();
  123. animation_i++;
  124. if (animation_i >= 49) {
  125. animation_i = 0;
  126. clear_strip();
  127. }
  128. }
  129. void setup_rainbow() {
  130. Serial.println("Animation 1 setup");
  131. clear_strip();
  132. animation_step_time = 10;
  133. animation_i = 0;
  134. animation_var_1 = 10;
  135. current_animation = 0;
  136. }
  137. void setup_running() {
  138. Serial.println("Animation 2 setup");
  139. clear_strip();
  140. animation_step_time = 100;
  141. animation_i = 0;
  142. animation_var_1 = 0;
  143. current_animation = 1;
  144. }