#include #include #include #include #include //================ WiFi ================== const char* ssid = "TANGYUAN"; const char* password = "85379401"; WebServer server(80); bool machineRunning = false; String currentColor = "NONE"; //================ Servo ================== #define SERVO1_PIN 2 #define SERVO2_PIN 3 Servo servo1; Servo servo2; // 转盘位置 int turntablePos[3] = {10, 85, 160}; //================ TCS34725 ============== Adafruit_TCS34725 tcs = Adafruit_TCS34725( TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); //======================================== // 颜色识别 //======================================== String detectColor() { uint16_t r, g, b, c; tcs.getRawData(&r, &g, &b, &c); if (c < 100) return "NONE"; float rf = (float)r / c; float gf = (float)g / c; float bf = (float)b / c; if (rf > 0.45 && rf > gf && rf > bf) return "RED"; if (gf > 0.40 && gf > rf && gf > bf) return "GREEN"; if (rf > 0.30 && gf > 0.30 && bf < 0.20) return "YELLOW"; return "UNKNOWN"; } //======================================== // 分类舵机 // RED -> 0° // GREEN -> 60° // YELLOW -> 120° //======================================== void sortBean(String color) { if (color == "RED") { servo2.write(30); } else if (color == "GREEN") { servo2.write(60); } else if (color == "YELLOW") { servo2.write(90); } else { return; } delay(500); Serial.print("Sorted: "); Serial.println(color); } //======================================== // 网页主页 //======================================== void handleRoot() { String state; if (machineRunning) state = "RUNNING"; else state = "STOPPED"; String page = "" "" "" "" "" "" "" "" "" "
" "

Color Sorting Machine

" "

Status : " + state + "

" "

Detected Color :

" "

" + currentColor + "

" "
" "" "" "" "


" "" "" "" "
" "" ""; server.send(200, "text/html", page); } //======================================== // START //======================================== void handleStart() { machineRunning = true; server.sendHeader("Location", "/"); server.send(303); } //======================================== // STOP //======================================== void handleStop() { machineRunning = false; server.sendHeader("Location", "/"); server.send(303); } //======================================== // SETUP //======================================== void setup() { Serial.begin(115200); servo1.attach(SERVO1_PIN); servo2.attach(SERVO2_PIN); servo1.write(0); servo2.write(0); Wire.begin(); if (!tcs.begin()) { Serial.println("TCS34725 not found!"); while (1); } Serial.println("TCS34725 Ready"); // WiFi连接 WiFi.begin(ssid, password); Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // Web Server server.on("/", handleRoot); server.on("/start", handleStart); server.on("/stop", handleStop); server.begin(); Serial.println("Server Started"); } //======================================== // LOOP //======================================== void loop() { server.handleClient(); if (!machineRunning) { delay(10); return; } for (int i = 0; i < 3; i++) { server.handleClient(); if (!machineRunning) return; // 转盘转动 servo1.write(turntablePos[i]); Serial.print("Turntable Position: "); Serial.println(turntablePos[i]); delay(500); unsigned long startTime = millis(); while (millis() - startTime < 2000) { server.handleClient(); if (!machineRunning) return; String color = detectColor(); currentColor = color; if (color != "NONE" && color != "UNKNOWN") { Serial.print("Detected: "); Serial.println(color); sortBean(color); delay(1000); break; } delay(100); } } }