#include Servo myServo; // Create a servo object // Define stepper motor pins const int stepPin = 5; const int dirPin = 6; const int buttonPin1 = 2; // Button pin for controlling the stepper motor // Define servo motor pin const int servoPin = 13; // Define servo control pin const int buttonPin2 = 3; // Button pin for controlling the servo volatile bool motorRunning = false; // Flag variable to record the working status of the stepper motor volatile bool servoRunning = false; // Flag variable to record the working status of the servo unsigned long lastInterruptTime1 = 0; // Last interrupt time for the stepper motor unsigned long lastInterruptTime2 = 0; // Last interrupt time for the servo unsigned long lastMotorStepTime = 0; // Last stepping time of the stepper motor unsigned long lastServoMoveTime = 0; // Last movement time of the servo int motorStepCount = 0; // Step counter for the stepper motor int servoAngle = 0; // Servo angle bool motorDirection = true; // Flag variable to record the direction of the stepper motor const int motorStepInterval = 200; // Step interval of the stepper motor (microseconds) const int servoMoveInterval = 20; // Movement interval of the servo (milliseconds) const int motorSteps = 20000; // Number of steps per run const int maxServoAngle = 60; // Maximum angle of the servo const int servoSpeedFactor = 1; // Factor to adjust the speed of the servo movement, the larger the value, the slower the speed void setup() { // Initialize the serial port for debugging Serial.begin(9600); // Initialize stepper motor pins pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(buttonPin1, INPUT_PULLUP); // Use internal pull-up resistor // Initialize servo myServo.attach(servoPin); pinMode(buttonPin2, INPUT_PULLUP); // Use internal pull-up resistor // Attach interrupts to buttonPin1 and buttonPin2 attachInterrupt(digitalPinToInterrupt(buttonPin1), toggleMotor, FALLING); attachInterrupt(digitalPinToInterrupt(buttonPin2), toggleServo, FALLING); Serial.println("Setup complete"); } void loop() { unsigned long currentTime = millis(); if (motorRunning && (currentTime - lastMotorStepTime >= motorStepInterval / 1000)) { runMotor(currentTime); } if (servoRunning && (currentTime - lastServoMoveTime >= servoMoveInterval * servoSpeedFactor)) { runServo(currentTime); } } // Stepper motor control function void runMotor(unsigned long currentTime) { if (motorStepCount < motorSteps) { digitalWrite(stepPin, HIGH); delayMicroseconds(100); digitalWrite(stepPin, LOW); lastMotorStepTime = currentTime; motorStepCount++; } else { motorStepCount = 0; motorDirection = !motorDirection; // Toggle direction digitalWrite(dirPin, motorDirection ? HIGH : LOW); Serial.print("Motor direction: "); Serial.println(motorDirection ? "Forward" : "Backward"); } } // Servo control function void runServo(unsigned long currentTime) { static bool increasing = true; // Record the movement direction of the servo if (increasing) { servoAngle++; if (servoAngle >= maxServoAngle) { increasing = false; } } else { servoAngle--; if (servoAngle <= 0) { increasing = true; } } myServo.write(servoAngle); Serial.println(servoAngle); lastServoMoveTime = currentTime; } // Stepper motor interrupt service routine void toggleMotor() { unsigned long interruptTime = millis(); if (interruptTime - lastInterruptTime1 > 200) { // Simple debounce processing motorRunning = !motorRunning; motorStepCount = 0; Serial.print("Motor running: "); Serial.println(motorRunning); lastInterruptTime1 = interruptTime; } } // Servo interrupt service routine void toggleServo() { unsigned long interruptTime = millis(); if (interruptTime - lastInterruptTime2 > 200) { // Simple debounce processing servoRunning = !servoRunning; Serial.print("Servo running: "); Serial.println(servoRunning); lastInterruptTime2 = interruptTime; } }