/*---------------SETUP--------------
* Uno sketch to drive a stepper motor using the AccelStepper library.
Works with a step/direction constant current driver like the a4988.
It will accelerate the motor, then run at constant speed until an interrupt occurs, stop the motor,
then return it to it's starting (0) position.
1MAR24' {~Gadget}
* ------------ARDUINO UNO----------
* --POWER--
* -->
* 5V-->
* RES-->
* 3.3V-->
* 5V--> A4988 VDD
* GND-->A4988 GND
* GND-->
* VIN-->
* --ANALOG IN--
* A0-->
* A1-->
* A2-->
* A3-->
* A4-->
* A5-->
*--DIGITAL--
* SCL-->
* SDA-->
* REF-->
* GND-->
* 13-->
* 12-->
* 11-->
* 10-->
* 9-->
* 8-->
* 7-->
* 6-->
* 5-->
* 4-->
* 3-->
* 2-->endstop microswitch(SPST, NC)-->GND
* 1-->
* 0-->
*
*-------------A4988---------------
* VMOT-->100uf capacitor-->ground
* GND-->ground
* 2B-->phase 1, wire 1 of stepper
* 2A-->phase 1, wire 2 of stepper
* 1A-->phase 2, wire 1 of stepper
* 1B-->phase 2, wire 2 of stepper
* VDD-->UNO 5V
* GND-->UNO GND pin
*
* ENAB-->
* MS1-->
* MS2-->
* MS3-->
* RST--> A4988 SLP
* SLP-->A4988 RST
* STEP-->UNO digital pin 4
* DIR-->UNO digital pin 5
*
*/
#define OPTICAL_SWITCH_PIN 2 // Pin connected to the optical switch
#define LED_SWITCH_PIN 13 // Pin connected to the LED switch
#define STEPPER_DIR_PIN 3 // Pin connected to the A4988 stepper driver's DIR pin
#define STEPPER_STEP_PIN 4 // Pin connected to the A4988 stepper driver's STEP pin
#define STEPS_PER_REVOLUTION 200 // Steps per revolution for your stepper motor
#define STEPPER_SPEED 1000 // Speed of the stepper motor (steps per second)
#define TRIGGER_DELAY 1000 // Delay after triggering the optical switch (milliseconds)
//#define EXTRA_STEPS 500 // Extra steps to move after optical switch is no longer triggered
void setup() {
pinMode(OPTICAL_SWITCH_PIN, INPUT_PULLUP); // Set optical switch pin as input with internal pull-up resistor
//pinMode(LED_SWITCH_PIN, INPUT_PULLUP); // Set LED switch pin as input with internal pull-up resistor
pinMode(STEPPER_DIR_PIN, OUTPUT); // Set stepper driver direction pin as output
pinMode(STEPPER_STEP_PIN, OUTPUT); // Set stepper driver step pin as output
}
void loop() {
if (digitalRead(OPTICAL_SWITCH_PIN) == LOW) { // If optical switch is triggered
moveStepper(1000); // Move stepper motor 10000 steps
delay(TRIGGER_DELAY); // Wait for the object to move away from the optical switch
//moveStepper(EXTRA_STEPS); // Move stepper motor additional 500 steps
delay(1000); // Wait for some time before going back to original position
moveStepper(-1000); // Move stepper motor back to original position (10000 + 500 - 11000 = -500)
}
}
void moveStepper(int steps) {
digitalWrite(STEPPER_DIR_PIN, steps > 0 ? HIGH : LOW); // Set direction based on sign of steps
steps = abs(steps); // Take absolute value of steps
for (int i = 0; i < steps; i++) {
digitalWrite(STEPPER_STEP_PIN, HIGH);
delayMicroseconds(1000); // Adjust this value for desired speed
digitalWrite(STEPPER_STEP_PIN, LOW);
delayMicroseconds(1000); // Adjust this value for desired speed
}
}