#include <AccelStepper.h>
// Define the connections between the motor and the A4988 driver
#define dirPinNut 0
#define stepPinNut 1
#define dirPinBolt 2
#define stepPinBolt 3
// Define the number of steps per revolution for the motor
#define stepsPerRevolution 24
// Define motor interface type
#define motorInterfaceType 1
// Create an instance of the AccelStepper class
AccelStepper stepperNut(motorInterfaceType, stepPinNut, dirPinNut);
AccelStepper stepperBolt(motorInterfaceType, stepPinBolt, dirPinBolt);
void setup() {
// Set the speed and acceleration of the motor
stepperNut.setMaxSpeed(1000);
stepperNut.setAcceleration(100);
stepperBolt.setMaxSpeed(1000);
stepperBolt.setAcceleration(100);
// Set the initial position of the motor to 0
stepperNut.setCurrentPosition(0);
stepperBolt.setCurrentPosition(0);
// Setup limit switch
pinMode(4,INPUT); // Setup for nut feeding system
pinMode(8,INPUT); // Setup for bolt feeding system
}
void loop() {
// Ensure the actuator is in home position
if (digitalRead(4) == LOW) {
stepperNut.runToNewPosition(0);
}
// Rotate the motor clockwise to push up three nuts sequentially
stepperNut.runToNewPosition(576-96*2);
// Wait for the next cycle
delay(1000);
stepperNut.runToNewPosition(576-96);
delay(1000);
stepperNut.runToNewPosition(576);
delay(1000);
// Ensure the actuator is in home position
if (digitalRead(8) == LOW) {
stepperBolt.runToNewPosition(0);
}
// Rotate the motor clockwise to push up three bolts sequentially
stepperBolt.runToNewPosition(748-295*2);
delay(1000);
stepperBolt.runToNewPosition(748-295);
delay(1000);
stepperBolt.runToNewPosition(748);
delay(1000);
// Rotate the motor anticlockwise to fully retract the linear actuator
stepperNut.runToNewPosition(0);
stepperBolt.runToNewPosition(0);
// Pause for 10 seconds
delay(10000);
}