// Code generated with the help of AI from the prompt described in the documentation; manually reviewed and adapted. #include // --- Servo 1 --- const int servoPin1 = D2; // PCB button const int buttonPCB1 = D4; // External arcade button const int buttonArcade1 = D0; Servo myServo1; const int restPos1 = 00; const int hitPos1 = 180; // --- Servo 2 --- const int servoPin2 = D1; const int buttonPin2 = D6; Servo myServo2; // Inverted movement const int restPos2 = 150; const int hitPos2 = 30; // --- LEDs --- const int ledPin1 = D3; const int ledPin2 = D5; // --- Timing control --- unsigned long moveStartTime1 = 0; unsigned long moveStartTime2 = 0; const int moveDuration = 200; bool servo1Active = false; bool servo2Active = false; // --- Button states --- int lastButtonPCB1 = HIGH; int lastButtonArcade1 = HIGH; int lastButtonState2 = HIGH; void setup() { Serial.begin(115200); // Servo 1 buttons pinMode(buttonPCB1, INPUT_PULLUP); pinMode(buttonArcade1, INPUT_PULLUP); // Servo 2 button pinMode(buttonPin2, INPUT_PULLUP); // LEDs pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); // Attach servos myServo1.attach(servoPin1); myServo2.attach(servoPin2); // Initial positions myServo1.write(restPos1); myServo2.write(restPos2); delay(500); // Read initial button states lastButtonPCB1 = digitalRead(buttonPCB1); lastButtonArcade1 = digitalRead(buttonArcade1); lastButtonState2 = digitalRead(buttonPin2); Serial.println("XIAO ESP32C6 ready"); } void loop() { unsigned long currentTime = millis(); // Read buttons int currentButtonPCB1 = digitalRead(buttonPCB1); int currentButtonArcade1 = digitalRead(buttonArcade1); int currentButtonState2 = digitalRead(buttonPin2); // ===================================================== // SERVO 1 - PCB BUTTON // ===================================================== if (lastButtonPCB1 == HIGH && currentButtonPCB1 == LOW) { delay(20); if (digitalRead(buttonPCB1) == LOW) { Serial.println("SERVO 1 - PCB BUTTON"); myServo1.write(hitPos1); digitalWrite(ledPin1, HIGH); servo1Active = true; moveStartTime1 = currentTime; } } // ===================================================== // SERVO 1 - EXTERNAL ARCADE BUTTON // ===================================================== if (lastButtonArcade1 == HIGH && currentButtonArcade1 == LOW) { delay(20); if (digitalRead(buttonArcade1) == LOW) { Serial.println("SERVO 1 - ARCADE BUTTON"); myServo1.write(hitPos1); digitalWrite(ledPin1, HIGH); servo1Active = true; moveStartTime1 = currentTime; } } // ===================================================== // SERVO 2 // ===================================================== if (lastButtonState2 == HIGH && currentButtonState2 == LOW) { delay(20); if (digitalRead(buttonPin2) == LOW) { Serial.println("SERVO 2"); myServo2.write(hitPos2); digitalWrite(ledPin2, HIGH); servo2Active = true; moveStartTime2 = currentTime; } } // ===================================================== // SERVO 1 RETURN // ===================================================== if (servo1Active && (currentTime - moveStartTime1 >= moveDuration)) { myServo1.write(restPos1); digitalWrite(ledPin1, LOW); servo1Active = false; } // ===================================================== // SERVO 2 RETURN // ===================================================== if (servo2Active && (currentTime - moveStartTime2 >= moveDuration)) { myServo2.write(restPos2); digitalWrite(ledPin2, LOW); servo2Active = false; } // Save button states lastButtonPCB1 = currentButtonPCB1; lastButtonArcade1 = currentButtonArcade1; lastButtonState2 = currentButtonState2; }