// ****************************************************************** // * * // * An example for using the "CNC V3 Shield" board * // * * // * S. Reifel & Co. 6/24/2018 * // * * // ****************************************************************** // Available on Amazon at very low cost is a stepper driver board shield for // the Arduino Uno. It is called the "CNC V3 Shield". It supports up to // 4 steppers. This example shows how to use it. // // // Documentation at: // https://github.com/Stan-Reifel/FlexyStepper // // // Note: FlexyStepper's companion library that supports faster step rates can // be found here: // https://github.com/Stan-Reifel/SpeedyStepper // // // For all driver boards, it is VERY important that you set the motor // current before running the example. This is typically done by adjusting // a potentiometer on the board. Read the driver board's documentation to // learn how. // *********************************************************************** #include // // pin assignments // const int LED_PIN = 13; const int MOTOR_X_STEP_PIN = 2; const int MOTOR_Y_STEP_PIN = 3; const int MOTOR_Z_STEP_PIN = 4; const int MOTOR_X_DIR_PIN = 5; const int MOTOR_Y_DIR_PIN = 6; const int MOTOR_Z_DIR_PIN = 7; const int STEPPERS_ENABLE_PIN = 8; const int LIMIT_SWITCH_X_PIN = 9; 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 // FlexyStepper stepperX; FlexyStepper stepperZ; void setup() { // // setup the LED pin and enable print statements // pinMode(LED_PIN, OUTPUT); pinMode(STEPPERS_ENABLE_PIN, OUTPUT); // be sure to do this Serial.begin(9600); // // connect and configure the stepper motor to there IO pins // stepperX.connectToPins(MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN); stepperZ.connectToPins(MOTOR_Z_STEP_PIN, MOTOR_Z_DIR_PIN); // // enable the stepper motors // digitalWrite(STEPPERS_ENABLE_PIN, LOW); // be sure to do this //run this once for now, add controll code to loop later runBothXAndZSteppers(); } 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. // // // first show how to run just one stepper motor // //Serial.println("Running just the X stepper"); //runJustTheXStepper(); //runJustTheZStepper(); // // now show how to run two stepper motors // //Serial.println("Running the X & Z steppers"); //runBothXAndZSteppers(); } void runJustTheXStepper() { // // set the speed and acceleration rates for the stepper motor // stepperX.setSpeedInStepsPerSecond(100); stepperX.setAccelerationInStepsPerSecondPerSecond(10); // // Rotate the motor in the forward direction one revolution (200 steps). // This function call will not return until the motion is complete. // stepperX.moveRelativeInSteps(200); // // now that the rotation has finished, delay 1 second before starting // the next move // delay(1000); // // rotate backward 1 rotation, then wait 1 second // stepperX.moveRelativeInSteps(-200); delay(1000); // // This time speedup the motor, turning 10 revolutions. Note if you // tell a stepper motor to go faster than it can, it just stops. // stepperX.setSpeedInStepsPerSecond(800); stepperX.setAccelerationInStepsPerSecondPerSecond(800); stepperX.moveRelativeInSteps(200 * 10); delay(2000); } void runJustTheZStepper() { // // set the speed and acceleration rates for the stepper motor // stepperZ.setSpeedInStepsPerSecond(100); stepperZ.setAccelerationInStepsPerSecondPerSecond(10); // // Rotate the motor in the forward direction one revolution (200 steps). // This function call will not return until the motion is complete. // stepperZ.moveRelativeInSteps(200); // // now that the rotation has finished, delay 1 second before starting // the next move // delay(1000); // // rotate backward 1 rotation, then wait 1 second // stepperZ.moveRelativeInSteps(-200); delay(1000); // // This time speedup the motor, turning 10 revolutions. Note if you // tell a stepper motor to go faster than it can, it just stops. // stepperZ.setSpeedInStepsPerSecond(800); stepperZ.setAccelerationInStepsPerSecondPerSecond(800); stepperZ.moveRelativeInSteps(200 * 10); delay(2000); } void runBothXAndZSteppers() { // // 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 200 steps, but in opposite directions // long stepsX = -200 * 5; long stepsZ = 200 * 30; float speedInStepsPerSecond = 100; float accelerationInStepsPerSecondPerSecond = 100; moveXZWithCoordination(stepsX, stepsZ, speedInStepsPerSecond, accelerationInStepsPerSecondPerSecond); delay(3000); } // // move both X & Z 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 moveXZWithCoordination(long stepsX, long stepsZ, float speedInStepsPerSecond, float accelerationInStepsPerSecondPerSecond) { float speedInStepsPerSecond_X; float accelerationInStepsPerSecondPerSecond_X; float speedInStepsPerSecond_Z; float accelerationInStepsPerSecondPerSecond_Z; long absStepsX; long absStepsZ; // // setup initial speed and acceleration values // speedInStepsPerSecond_X = speedInStepsPerSecond; accelerationInStepsPerSecondPerSecond_X = accelerationInStepsPerSecondPerSecond; speedInStepsPerSecond_Z = speedInStepsPerSecond; accelerationInStepsPerSecondPerSecond_Z = accelerationInStepsPerSecondPerSecond; // // determine how many steps each motor is moving // if (stepsX >= 0) absStepsX = stepsX; else absStepsX = -stepsX; if (stepsZ >= 0) absStepsZ = stepsZ; else absStepsZ = -stepsZ; // // determine which motor is traveling the farthest, then slow down the // speed rates for the motor moving the shortest distance // if ((absStepsX > absStepsZ) && (stepsX != 0)) { // // slow down the motor traveling less far // float scaler = (float) absStepsZ / (float) absStepsX; speedInStepsPerSecond_Z = speedInStepsPerSecond_Z * scaler; accelerationInStepsPerSecondPerSecond_Z = accelerationInStepsPerSecondPerSecond_Z * scaler; } if ((absStepsZ > absStepsX) && (stepsZ != 0)) { // // slow down the motor traveling less far // float scaler = (float) absStepsX / (float) absStepsZ; 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.setTargetPositionRelativeInSteps(stepsX); // // setup the motion for the Z motor // stepperZ.setSpeedInStepsPerSecond(speedInStepsPerSecond_Z); stepperZ.setAccelerationInStepsPerSecondPerSecond(accelerationInStepsPerSecondPerSecond_Z); stepperZ.setTargetPositionRelativeInSteps(stepsZ); // // now execute the moves, looping until both motors have finished // while((!stepperX.motionComplete()) || (!stepperZ.motionComplete())) { stepperX.processMovement(); stepperZ.processMovement(); } }