Hey guys
I'm doing my first arduino project and I'm building an automated painter, where two stepper motors are pushing a crossbar over a piece of glass. I set up the motors using a CNC shield with two polulu motors. As seen on the following image.
The motors are running as the X and Y motor. I have connected a limit switch on one side and connected it to A0 analog input.
The code i'm using is currently only moving the X motor when homing. I would like them to move together so they are at the same position after the homing. Then I have to add a button to start the program which is currently runnnig automatically after homing. But i'm not sure how to make the code so that you don't have to hold the button, but just push it and then it will go to the desired position and then return home.
#include <SpeedyStepper.h>
//
// pin assignments
//
const int LED_PIN = 13;
const int MOTOR_X_STEP_PIN = 9;
const int MOTOR_Y_STEP_PIN = 6;
const int MOTOR_Z_STEP_PIN = 3;
const int MOTOR_X_DIR_PIN = 8;
const int MOTOR_Y_DIR_PIN = 5;
const int MOTOR_Z_DIR_PIN = 2;
const int STEPPERS_ENABLE_PIN = 10;
const int LIMIT_SWITCH_PIN = A0;
//const int LIMIT_SWITCH_Y_PIN = 10;
//const int LIMIT_SWITCH_Z_PIN = 11;
//const int SPINDLE_ENABLE_PIN = 12;
//const int SPINDLE_DIRECTION_PIN = 13;
//
// create the stepper motor objects
//
SpeedyStepper stepperX;
SpeedyStepper stepperY;
void setup()
{
//
// setup the LED pin and enable print statements
//
pinMode(LED_PIN, OUTPUT);
pinMode(STEPPERS_ENABLE_PIN, OUTPUT); // be sure to do this
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
Serial.begin(9600);
//
// connect and configure the stepper motor to there IO pins
//
stepperX.connectToPins(MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);
stepperY.connectToPins(MOTOR_Y_STEP_PIN, MOTOR_Y_DIR_PIN);
//
// enable the stepper motors
//
digitalWrite(STEPPERS_ENABLE_PIN, LOW); // be sure to do this
// Homing the motors using a limit Switch
// First you must tell the library "how many steps it takes to move one
// millimeter". My setup is configured with a lead-screw having
// 25 full-steps/mm and 1x microstepping.
//
stepperX.setStepsPerMillimeter(25 * 1); // 1x microstepping
stepperY.setStepsPerMillimeter(25 * 1); // 1x microstepping
//
// set the speed and acceleration rates for the stepper motor
//
stepperX.setSpeedInMillimetersPerSecond(20);
stepperX.setAccelerationInMillimetersPerSecondPerSecond(10.0);
stepperY.setSpeedInMillimetersPerSecond(20);
stepperY.setAccelerationInMillimetersPerSecondPerSecond(10.0);
//
// move the motor toward the limit switch to find the "Home" position
//
const float homingSpeedInMMPerSec = 5.0;
const float maxHomingDistanceInMM = 380; // since my lead-screw is 38cm long, should never move more than that
const int directionTowardHome = -1; // direction to move toward limit switch: 1 goes positive direction, -1 backward
if(stepperX.moveToHomeInMillimeters(directionTowardHome, homingSpeedInMMPerSec, maxHomingDistanceInMM, LIMIT_SWITCH_PIN) != true && stepperY.moveToHomeInMillimeters(directionTowardHome, homingSpeedInMMPerSec, maxHomingDistanceInMM, LIMIT_SWITCH_PIN) != true)
{
//
// this code is executed only if homing fails because it has moved farther
// than maxHomingDistanceInMM and never finds the limit switch, blink the
// LED fast forever indicating a problem
//
while(true)
{
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
delay(50);
}
}
//
// homing is now complete, the motor is stopped at position 0mm
//
delay(500);
stepperX.moveToPositionInMillimeters(10.0); // Moving the crossbar away from LS to start next loop
//
// if you want your 0 origin someplace else, you can change it
//
//stepper.setCurrentPositionInMillimeters(325);
}
void loop()
{
//
// Note 1: It is assumed that you are using a stepper motor with a
// 1.8 degree step angle (which is 200 steps/revolution). This is the
// most common type of stepper.
//
// Note 2: It is also assumed that your stepper driver board is
// configured for 1x microstepping.
//
// It is OK if these assumptions are not correct, your motor will just
// turn less than a full rotation when commanded to.
//
// Note 3: This example uses "relative" motions. This means that each
// command will move the number of steps given, starting from it's
// current position.
//
//
// how to run two stepper motors
//
Serial.println("Running the X & Y steppers");
runBothXAndYSteppers();
}
void runBothXAndYSteppers()
{
//
// use the function below to move two motors with speed coordination
// so that both stop at the same time, even if one moves farther than
// the other
//
//
// turn both motors 250,000 steps
//
long stepsX = 2500 * 100;
long stepsY = 2500 * 100;
float speedInStepsPerSecond = -5000;
float accelerationInStepsPerSecondPerSecond = 1000;
moveXYWithCoordination(stepsX, stepsY, speedInStepsPerSecond, accelerationInStepsPerSecondPerSecond);
delay(1000);
}
//
// move both X & Y motors together in a coordinated way, such that they each
// start and stop at the same time, even if one motor moves a greater distance
//
void moveXYWithCoordination(long stepsX, long stepsY, float speedInStepsPerSecond, float accelerationInStepsPerSecondPerSecond)
{
float speedInStepsPerSecond_X;
float accelerationInStepsPerSecondPerSecond_X;
float speedInStepsPerSecond_Y;
float accelerationInStepsPerSecondPerSecond_Y;
long absStepsX;
long absStepsY;
//
// setup initial speed and acceleration values
//
speedInStepsPerSecond_X = speedInStepsPerSecond;
accelerationInStepsPerSecondPerSecond_X = accelerationInStepsPerSecondPerSecond;
speedInStepsPerSecond_Y = speedInStepsPerSecond;
accelerationInStepsPerSecondPerSecond_Y = accelerationInStepsPerSecondPerSecond;
//
// determine how many steps each motor is moving
//
if (stepsX >= 0)
absStepsX = stepsX;
else
absStepsX = -stepsX;
if (stepsY >= 0)
absStepsY = stepsY;
else
absStepsY = -stepsY;
//
// determine which motor is traveling the farthest, then slow down the
// speed rates for the motor moving the shortest distance
//
if ((absStepsX > absStepsY) && (stepsX != 0))
{
//
// slow down the motor traveling less far
//
float scaler = (float) absStepsY / (float) absStepsX;
speedInStepsPerSecond_Y = speedInStepsPerSecond_Y * scaler;
accelerationInStepsPerSecondPerSecond_Y = accelerationInStepsPerSecondPerSecond_Y * scaler;
}
if ((absStepsY > absStepsX) && (stepsY != 0))
{
//
// slow down the motor traveling less far
//
float scaler = (float) absStepsX / (float) absStepsY;
speedInStepsPerSecond_X = speedInStepsPerSecond_X * scaler;
accelerationInStepsPerSecondPerSecond_X = accelerationInStepsPerSecondPerSecond_X * scaler;
}
//
// setup the motion for the X motor
//
stepperX.setSpeedInStepsPerSecond(speedInStepsPerSecond_X);
stepperX.setAccelerationInStepsPerSecondPerSecond(accelerationInStepsPerSecondPerSecond_X);
stepperX.setupRelativeMoveInSteps(stepsX);
//
// setup the motion for the Y motor
//
stepperY.setSpeedInStepsPerSecond(speedInStepsPerSecond_Y);
stepperY.setAccelerationInStepsPerSecondPerSecond(accelerationInStepsPerSecondPerSecond_Y);
stepperY.setupRelativeMoveInSteps(stepsY);
//
// now execute the moves, looping until both motors have finished
//
while((!stepperX.motionComplete()) || (!stepperY.motionComplete()))
{
stepperX.processMovement();
stepperY.processMovement();
}
}
Thanks