//// [000_000000][001_000000][002_000000][003_000000][004_000000][005_000000][006_000000][007_000000][001_FF0000]{ //// [@@@_FFFFFF] [@@@_1B00EA] [@@@_1B00EA]![@@@_1B00EA]"[@@@_1B00EA]#[@@@_1B00EA]! #include #define PIN 48 #define N_LEDS 8 WS2812FX strip = WS2812FX(N_LEDS, PIN, NEO_GRB + NEO_KHZ800); String srl_string = ""; String led_string = ""; String rgb_string = ""; uint32_t i = 0; uint32_t led = 0; uint32_t rgb = 0; uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; uint8_t m = 0; char incoming_chr = ""; uint8_t command = 0; // Initial setup void setup() { Serial.begin(115200); // Initialize the debug serial port /* * It is recommended to use the lower baud rate. The higher the baud rate, * the higher the bit error rate will be, and the control action might fail. */ Serial3.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port //// strip.begin(); strip.init(); strip.setBrightness(127); // 255 ? strip.setSpeed(255); // 1000 ? strip.setMode(FX_MODE_STATIC); strip.start(); //// pinMode(LED_BUILTIN, OUTPUT); // Initialize the digital pin LED_BUILTIN as an output } // Read a command from ESP8266 over serial void read_string() { while (Serial3.available() > 0) { incoming_chr = Serial3.read(); srl_string += incoming_chr; Serial.print(incoming_chr); // DEBUG switch (incoming_chr) { case '[': command = 1; led_string = ""; rgb_string = ""; break; case ']': command = 0; if ((led_string.length() == 3) && (rgb_string.length() == 6)) { rgb = (uint32_t) strtol(&rgb_string[0], NULL, 16); r = rgb >> 16; g = rgb >> 8 & 0xFF; b = rgb & 0xFF; if ((led_string[0] == "@") && (led_string[1] == "@") && (led_string[2] == "@")) { for (i = 0; i < N_LEDS; i++) { strip.setPixelColor(i, r, g, b); } } else { led = (uint32_t) strtol( &led_string[0], NULL, 16); if (led < N_LEDS) { strip.setPixelColor(led, r, g, b); } } } led_string = ""; rgb_string = ""; srl_string = ""; break; case '_': if (command == 1) { command = 2; rgb_string = ""; } break; /**** ASCII: 32 - 47 ****/ /*** !"#$%&'()*+,-./ ***/ case ' ' ... '/' : m = ((int)incoming_chr) - 32; strip.setColor(rgb); strip.setMode(m); strip.show(); break; /**** ASCII: 71 - 90 ****/ /* GHIJKLMNOPQRSTUVWXYZ */ case 'G' ... 'Z' : m = ((int)incoming_chr) - 55; strip.setColor(rgb); strip.setMode(m); strip.show(); break; /*** ASCII: 103 - 122 ***/ /* ghijklmnopqrstuvwxyz */ case 'g' ... 'z' : m = ((int)incoming_chr) - 67; strip.setColor(rgb); strip.setMode(m); strip.show(); break; /****** ASCII: 123 ******/ /********** { ***********/ case '{' : m = 56; /* INDIVIDUAL */ strip.show(); break; default: if (command == 1) { led_string += incoming_chr; } if (command == 2) { rgb_string += incoming_chr; } } } } // The loop function runs over and over again forever void loop() { if (Serial3.available() > 0) { read_string(); } else { switch (m) { case 56: /* INDIVIDUAL */ break; default: strip.service(); } } }