Stepper Code Example 1
From SnoCo Robotics
Sample Code of AccelStepper
AccelStepper is an arduino library. This sample code is used for demonstration of multiple code designed tips and tricks. Additions are welcomed.
#include <AccelStepper.h> /* * newBounce based on AccelStepper library Bounce Example * library reference https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html * * Chas Ihler * iradan.com */ AccelStepper stepper1(AccelStepper::DRIVER, 8, 9); AccelStepper stepper2(AccelStepper::DRIVER, 3, 4); #define STEPPER_SPEED 1000 #define STEPPER_ACCEL 2000 bool b_task_logging = true; const long l_wait_interval = 500; //tests const bool b_test_move = true; const bool b_test1_moveTo = false; const bool b_test2_moveTo = false; const bool b_test_runToPosition = false; const bool b_test_runSpeedToPosition = false; const bool b_test_setSpeed = false; bool b_run_once = false; int i_stop_count= 0; unsigned long ul_previousTaskmillis = 0; unsigned long ul_previousNBWmillis = 0; void setup() { delay(250); Serial.begin(115200); delay(100); Serial.println(" "); Serial.println(" ------------------------------ "); Serial.println(" AccelStepper Library "); Serial.println(" Demo "); Serial.println(" ------------------------------ "); Serial.println(" "); pinMode(3, OUTPUT); // Stepper 2 Pulse pinMode(4, OUTPUT); // Stepper 2 Direction pinMode(5, OUTPUT); // Stepper 2 Enable pinMode(8, OUTPUT); // Stepper 1 Pulse pinMode(9, OUTPUT); // Stepper 1 Direction pinMode(10, OUTPUT); // Stepper 1 Enable stepper1.setMaxSpeed(STEPPER_SPEED); stepper2.setMaxSpeed(STEPPER_SPEED); stepper1.setAcceleration(STEPPER_ACCEL); stepper2.setAcceleration(STEPPER_ACCEL); stepper1.setEnablePin(10); stepper2.setEnablePin(5); stepper1.setPinsInverted(false, true, false); stepper2.setPinsInverted(false, false, false); stepper1.disableOutputs(); stepper2.disableOutputs(); } bool non_blocking_wait(void) { unsigned long ul_currentMillis = millis(); if (ul_currentMillis - ul_previousNBWmillis >= l_wait_interval) { ul_previousNBWmillis = ul_currentMillis; return true; } return false; } void loop() { unsigned long ul_taskONEmillis = 0; unsigned long ul_taskTWOmillis = 0; unsigned long ul_taskTHREEmillis = 0; ul_previousTaskmillis = millis(); if (!b_run_once) { //I don't like to "do" anything in setup. stepper1.enableOutputs(); stepper2.enableOutputs(); stepper1.setCurrentPosition(500); stepper2.setCurrentPosition(500); if (b_test1_moveTo) stepper1.moveTo(400); if (b_test1_moveTo) stepper2.moveTo(800); if (b_test_runSpeedToPosition) stepper1.move(2000); b_run_once = true; } if (b_test_move) { //relative stepper1.move(750); } if (b_test1_moveTo) { //absolute if (stepper1.distanceToGo() == 0) stepper1.moveTo(-stepper1.currentPosition()); } if (b_test2_moveTo) { //absolute stepper1.moveTo(2000); stepper2.moveTo(2000); } if (b_test_runToPosition) { //Moves the motor (with acceleration/deceleration) to the target position and blocks until it is at position. Dont use this in event loops, since it blocks. stepper1.setCurrentPosition(-200); stepper1.runToNewPosition(1000); } if (b_test_runSpeedToPosition) { //Runs at the currently selected speed until the target position is reached. Does not implement accelerations. stepper1.runSpeedToPosition(); } if (b_test_setSpeed) { //Sets the desired constant speed for use with runSpeed(). stepper1.setSpeed(200); } ul_taskONEmillis = millis(); //task two goes here if (stepper1.distanceToGo() == 0) { if (i_stop_count > 20) { stepper1.disableOutputs(); } else { i_stop_count++; } } else { i_stop_count = 0; } delay(1); ul_taskTWOmillis = millis(); //task three goes here delay(1); ul_taskTHREEmillis = millis(); //Run Every time the loops runs, as often as practical if (b_test_setSpeed) { stepper1.runSpeed(); } else { stepper1.run(); stepper2.run(); } if (stepper1.isRunning()) { b_task_logging = true; } else { // b_task_logging = false; } if (b_task_logging) { if (non_blocking_wait()) { Serial.println("------------ Stepper Details ------------------"); Serial.print("Dist(steps) to Go: ");Serial.print(stepper1.distanceToGo());Serial.print(" Current Position: ");Serial.println(stepper1.currentPosition()); Serial.println(" "); float f_totalmillis = 0; unsigned long ul_currentmillis = 0; int i_display_time = 0; float f_display_pct = 0; ul_currentmillis = millis(); f_totalmillis = ul_currentmillis - ul_previousTaskmillis; Serial.println("-------------- Task Monitor -------------------"); Serial.print("TASK NAME ");Serial.print("TIME");Serial.print(" ");Serial.println("xx%"); Serial.println("-----------------------------------------------"); i_display_time = f_totalmillis; f_display_pct = 100; Serial.print("TOTAL ");Serial.print(i_display_time);Serial.print(" ");Serial.print(f_display_pct); Serial.println("%"); i_display_time = ul_taskONEmillis - ul_previousTaskmillis; f_display_pct = (i_display_time / f_totalmillis) * 100; Serial.print("ONE ");Serial.print(i_display_time);Serial.print(" ");Serial.print(f_display_pct); Serial.println("%"); i_display_time = ul_taskTWOmillis - ul_taskONEmillis; f_display_pct = (i_display_time / f_totalmillis) * 100; Serial.print("TWO ");Serial.print(i_display_time);Serial.print(" ");Serial.print(f_display_pct); Serial.println("%"); i_display_time = ul_taskTHREEmillis - ul_taskTWOmillis; f_display_pct = (i_display_time / f_totalmillis) * 100; Serial.print("THREE ");Serial.print(i_display_time);Serial.print(" ");Serial.print(f_display_pct); Serial.println("%"); Serial.println(" "); } } }