Hi There,
I am working on a project that does the following so far:
- Switch a solenoid to disengage a pin
- Turn a NEMA 23 stepper motor clockwise for at least one rotation
- Senses a proximity sensor at a certain location
- Stops the motor
- Turns the motor back 60 steps onto it's homing position.
- Releases the solenoid
Everything works fine for now, but the motor turns extremely slow as I am not using any libraries to turn the motor at the homing procedure but instead switching the Dir and Step pins High and LOW so that I can check if the limit switch was triggered in between steps.
I was wondering if anyone could please assist me in fine tuning the code for better use.
I am using a Nema 23 stepper motor with a TBC2160 driver and CNC shield onto a Arduino Mega. I want this hardware setup to allow further upgrades in the future.
This is the code I have so far. Please note I know the basics and am not an advanced programmer.
#include <FlexyStepper.h>
//
// pin assignments
//
const int LED_PIN = 13;
const int MOTOR_X_STEP_PIN = 2;
const int MOTOR_X_DIR_PIN = 5;
const int STEPPERS_ENABLE_PIN = 8;
const int solenoidPin = 53; //This is the solenoid output pin on the Mega
#define home_switch 52 // Pin 52 connected to Proximity sensor used as limit switch
int steps; // Used to set HOME position after Homing is completed
//
// create the stepper motor object
//
FlexyStepper stepperX;
void setup()
{
//
// setup the LED pin and enable print statements
//
pinMode(LED_PIN, OUTPUT);
pinMode(STEPPERS_ENABLE_PIN, OUTPUT);
pinMode(solenoidPin, OUTPUT);
Serial.begin(9600);
//
// connect and configure the stepper motor to its' IO pins
//
stepperX.connectToPins(MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);
//
// enable the stepper motor
//
digitalWrite(STEPPERS_ENABLE_PIN, LOW);
//
// set the speed and acceleration rates for the stepper motor
//
//stepperX.setSpeedInStepsPerSecond(1000);
//stepperX.setAccelerationInStepsPerSecondPerSecond(1000);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000);
//
// Note Rotating the motor in the forward direction one revolution (200*8 steps) as the driver is set to this setting.
while (!digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(MOTOR_X_DIR_PIN, HIGH);
digitalWrite(MOTOR_X_STEP_PIN, LOW);
delay(10); // More delay to slow even more while moving away from switch
digitalWrite(MOTOR_X_STEP_PIN, HIGH);
delay(10);
}
steps=0; // Reset position variable to zero
//stepperX.moveRelativeInSteps(-1660); // this could be used for a full rotation + 60 steps
Toolchanger();
}
void loop()
{
while (1) { // not sure if a while loop is necessary here, but it does work
if (steps > 0) { // To make sure the Stepper doesn't go beyond the Home Position
digitalWrite(MOTOR_X_DIR_PIN, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(MOTOR_X_STEP_PIN, LOW);
delay(1);
digitalWrite(MOTOR_X_STEP_PIN, HIGH);
delay(1);
}
}
}
void Toolchanger()
{
delay(1000);
stepperX.moveRelativeInSteps(60); // move the stepper 60 steps anti-clockwise
delay(1000);
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
}