#include #include #include const char *ssid = "SSID"; const char *password = "PASSWORD"; WiFiUDP udp; const int localPort = 4210; #define PIN 5 #define NUMPIXELS 12 #define DEFAULT_BRIGHTNESS 75 Adafruit_NeoPixel ring(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); String lastColor = "green"; int currentBrightness = DEFAULT_BRIGHTNESS; uint32_t getColorFromString(String colorName) { if (colorName == "red") return ring.Color(255, 0, 0); if (colorName == "green") return ring.Color(0, 255, 0); if (colorName == "blue") return ring.Color(0, 0, 255); if (colorName == "purple") return ring.Color(148, 0, 211); return ring.Color(0, 0, 0); } void applyColor(String color) { if (color != "off") lastColor = color; if (color == "red") ring.fill(ring.Color(255, 0, 0)); else if (color == "green") ring.fill(ring.Color(0, 255, 0)); else if (color == "blue") ring.fill(ring.Color(0, 0, 255)); else if (color == "purple") ring.fill(ring.Color(148, 0, 211)); else if (color == "off") ring.clear(); ring.show(); } uint32_t Wheel(byte pos) { pos = 255 - pos; if (pos < 85) return ring.Color(255 - pos * 3, 0, pos * 3); if (pos < 170) { pos -= 85; return ring.Color(0, pos * 3, 255 - pos * 3); } pos -= 170; return ring.Color(pos * 3, 255 - pos * 3, 0); } void rainbow(uint8_t wait) { for (uint16_t j = 0; j <= 255; j++) { for (uint16_t i = 0; i < ring.numPixels(); i++) { ring.setPixelColor(i, Wheel((i + j) & 255)); } ring.show(); delay(wait); } } void turnClockwise(String color) { for (int i = 0; i < NUMPIXELS; i++) { ring.clear(); ring.setPixelColor(i, getColorFromString(color)); ring.show(); delay(100); } ring.clear(); ring.show(); } void processCommand(String input) { input.trim(); input.toLowerCase(); if (input.startsWith("brightness")) { int value = input.substring(10).toInt(); value = constrain(value, 0, 100); currentBrightness = map(value, 0, 100, 0, 255); ring.setBrightness(currentBrightness); applyColor(lastColor); Serial.print("Brightness set to "); Serial.println(value); } else if (input == "on") { applyColor(lastColor); } else if (input == "off") { applyColor("off"); } else if (input == "turn") { turnClockwise(lastColor); applyColor(lastColor); } else if (input == "rainbow") { rainbow(50); } else { applyColor(input); } } void setup() { Serial.begin(115200); Serial.println("\nConnecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\n WiFi connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); ring.begin(); ring.setBrightness(currentBrightness); ring.clear(); ring.show(); udp.begin(localPort); Serial.print("UDP listening on port "); Serial.println(localPort); } void loop() { // 1. Serial Console Command if (Serial.available()) { String input = Serial.readStringUntil('\n'); processCommand(input); } // 2. UDP Packet Command int packetSize = udp.parsePacket(); if (packetSize) { char incoming[255]; int len = udp.read(incoming, 255); if (len > 0) incoming[len] = '\0'; Serial.print("UDP Received: "); Serial.println(incoming); processCommand(String(incoming)); } }