| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #include "FastLED.h"
- #include <SPI.h>
- #include <WiFiNINA.h>
- #define NUM_LEDS 50
- #define DATA_PIN 9
- char ssid[] = "Pannkakshuset";
- char pass[] = "lavalampa";
- int current_animation = 0;
- int loops_since_http = 0;
- int animation_step_time = 0;
- int animation_i = 0;
- int animation_var_1 = 0;
- long last_animation_millis = millis();
- //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
- extern void animation_rainbow(); // forward declaration
- extern void animation_running(); // forward declaration
- void (*animation_table[])() = {animation_rainbow, animation_running};
- extern void setup_rainbow(); // forward declaration
- extern void setup_running(); // forward declaration
- void (*setup_table[])() = {setup_rainbow, setup_running};
- CRGB leds[NUM_LEDS];
- int status = WL_IDLE_STATUS;
- WiFiServer server(80);
- void setup() {
- // put your setup code here, to run once:
- //Serial.begin(9600);
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- //pinMode(DATA_PIN, OUTPUT);
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
- status = WiFi.begin(ssid, pass);
- // wait 10 seconds for connection:
- delay(10000);
- }
- server.begin();
-
- delay(3000);
- FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness(255);
- FastLED.clear();
- FastLED.show();
- delay(1000);
- setup_table[current_animation]();
- }
- void loop() {
- loops_since_http += 1;
- long current_millis = millis();
- if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
- animation_table[current_animation]();
- last_animation_millis = current_millis;
- } else {
- loops_since_http = 0;
- //Serial.println("Waiting for animation");
- process_HTTP();
- delay(1);
- }
- // put your main code here, to run repeatedly:
- //Serial.println("Hello");
- //delay(1000);
- // leds[3] = CRGB::Blue;
- // leds[0] = CRGB::Red;
- // leds[1] = CRGB::Green;
- // leds[49] = CRGB::White;
- // FastLED.clear();
- // FastLED.show();
- // uint8_t delta = 10;
- // for (uint8_t i = 0; i < 360; i++) {
- // //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
- // fill_rainbow(leds, NUM_LEDS, i, delta);
- // FastLED.show();
- // delay(10);
- // }
-
- }
- void process_HTTP() {
- WiFiClient client = server.available();
- if (client) {
- int anim = 0;
- Serial.println("new client");
- // an http request ends with a blank line
- boolean foundSlash = false;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //Serial.write(c);
- if (foundSlash) {
- anim = c - 'A';
- client.println("HTTP/1.1 200 OK");
- break;
- }
- if (c == '/') {
- foundSlash = true;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- Serial.println("client disconnected");
- setup_table[anim]();
- }
- }
- void clear_strip() {
- FastLED.clear();
- FastLED.show();
- }
- void animation_rainbow() {
- fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1);
- FastLED.show();
- animation_i++;
- animation_i %= 260;
- //Serial.println(animation_i);
- //Serial.println("Animation 1 step");
- }
- void animation_running() {
- //Serial.println("Animation 2 step");
- leds[animation_i] = CRGB(100, 255, 200);
- FastLED.show();
- animation_i++;
- if (animation_i >= 49) {
- animation_i = 0;
- clear_strip();
- }
- }
- void setup_rainbow() {
- Serial.println("Animation 1 setup");
- clear_strip();
- animation_step_time = 10;
- animation_i = 0;
- animation_var_1 = 10;
- current_animation = 0;
- }
- void setup_running() {
- Serial.println("Animation 2 setup");
- clear_strip();
- animation_step_time = 100;
- animation_i = 0;
- animation_var_1 = 0;
- current_animation = 1;
- }
|