How can I serial print the current position of my stepper after I jog it from the home zero position? It doesn't have to count/show every single step but just show where it ended after I release the jog button. This way I can use this position data in future programing. Here's my code showing my attempt to do this on my CW button at line 173. I added line 39 also. This doesn't work as it only prints one step every time I hit the button.
#define MAX8BUTTONS // saves RAM because only 4 switches are used
#define MAX_STEPPER 6
#include <MobaTools.h>
const int STEPS_PER_REV = 800;
const int POS_MAX = 20000;
const int FORTY_METER_POSITION = 5000;
const int EIGHTY_METER_POSITION = 10000;
const int ONESIXTY_METER_POSITION = 16000;
//create stepper object ( 800 steps / rev - 1/4 microstep )
// Stepper 1 buffer tank
const byte stepper1_pul = 2;
const byte stepper1_dir = 3;
MoToStepper stepper_1( STEPS_PER_REV, STEPDIR );
// Stepper 2 IPA tank
const byte stepper2_pul = 4;
const byte stepper2_dir = 5;
MoToStepper stepper_2( STEPS_PER_REV, STEPDIR );
const byte enaPin = 7;
// buttons must switch to Gnd
enum { buttonZero, buttonCW, buttonCCW, forty_meter_button, eighty_meter_button, onesixty_meter_button } ; // create names for the buttons
const byte buttonPins[] = {22, 23, 24, 26, 27, 28 }; // assign pins to the buttons
const byte buttonCnt = sizeof(buttonPins);
MoToButtons myButton( buttonPins, buttonCnt, 20, 500 );
// limit switch at refpoint
const byte refPin1 = 25; // limit pin stepper 1 homing
const byte refPin2 = 29; // limit pin stepper 2 homing
const byte atRefpoint = LOW; // dependig wether it is a NO or NC switch
int stepCount = 0;
void setup() {
Serial.begin(115200); while (!Serial);
// Stepper 1
stepper_1.attach( stepper1_pul, stepper1_dir );
stepper_1.attachEnable( enaPin, 10, LOW ); // Enable active
// Stepper 2
stepper_2.attach( stepper2_pul, stepper2_dir );
stepper_2.attachEnable( enaPin, 10, LOW ); // Enable active
pinMode(LED_BUILTIN, OUTPUT);
pinMode(refPin1, INPUT_PULLUP );
pinMode(refPin2, INPUT_PULLUP );
Serial.println("Starting loop");
}
void loop()
{
myButton.processButtons(); // reading and processing the buttons ( e.g. debouncing and state change detection)
digitalWrite( LED_BUILTIN, digitalRead( refPin1 ) );
// check buttons and react accordingly
if ( myButton.pressed(buttonZero) ) { // home all steppers to zero position
Serial.println(" Find ref point");
toRefPoint();
}
if ( myButton.pressed(buttonCW) ) { // jog slow clockwise direction
Serial.println("Jog CW towards end");
toCWdirection();
}
if ( myButton.pressed(buttonCCW) ) { // jog slow counter clockwise direction
Serial.println("Jog CCW back to 0 ");
toCCWdirection();
}
if ( myButton.pressed(forty_meter_button) ) { // move to 40m position
Serial.println("Move towards the 40M position");
toFortyPoint();
}
if ( myButton.pressed(eighty_meter_button) ) { // move to 80m position
Serial.println("Move towards the 80M position");
toEightyPoint();
}
if ( myButton.pressed(onesixty_meter_button) ) { // move to 160m position
Serial.println("Move towards the 160M position");
toOnesixtyPoint();
}
if ( myButton.released(buttonCW) || myButton.released(buttonCCW) ) {
Serial.println("Stop the stepper");
stepper_1.rotate(0);
}
}
void toRefPoint() { // Homing: move all steppers to ref point and set zeropoint
Serial.println("homing buffer tank..."); // buffer tank start homing...
if ( digitalRead( refPin1 ) != atRefpoint ) { // quick move to ref point it it's not already there
Serial.println("Quick move to limit switch");
stepper_1.setSpeedSteps( 10000, 200 );
stepper_1.rotate(-1);
while ( digitalRead( refPin1 ) != atRefpoint );
}
digitalWrite( LED_BUILTIN, digitalRead( refPin1 ) );
Serial.println("ref point reached, stop the stepper");
stepper_1.rotate(0);
while ( stepper_1.moving() ); // wait till stop
Serial.println("move slowly back to the switching point of the limit switch");
stepper_1.setSpeedSteps( 500 );
stepper_1.setRampLen(0);
stepper_1.rotate( 1 );
while ( digitalRead( refPin1 ) == atRefpoint );
Serial.println("Switch point reached - set zero");
stepper_1.setZero();
stepper_1.moveTo(0);
digitalWrite( LED_BUILTIN, digitalRead( refPin1 ) );
Serial.println("homing buffer tank finished!");
Serial.println("homing IPA tank..."); // IPA tank start homing...
if ( digitalRead( refPin2 ) != atRefpoint ) { // quick move to ref point it it's not already there
Serial.println("Quick move to limit switch");
stepper_2.setSpeedSteps( 10000, 200 );
stepper_2.rotate(-1);
while ( digitalRead( refPin2 ) != atRefpoint );
}
digitalWrite( LED_BUILTIN, digitalRead( refPin2 ) );
Serial.println("ref point reached, stop the stepper");
stepper_2.rotate(0);
while ( stepper_2.moving() ); // wait till stop
Serial.println("move slowly back to the switching point of the limit switch");
stepper_2.setSpeedSteps( 500 );
stepper_2.setRampLen(0);
stepper_2.rotate( 1 );
while ( digitalRead( refPin2 ) == atRefpoint );
Serial.println("Switch point reached - set zero");
stepper_2.setZero();
stepper_2.moveTo(0);
digitalWrite( LED_BUILTIN, digitalRead( refPin2 ) );
Serial.println("IPA tank homing finished!");
}
void toFortyPoint() { // buffer tank 40 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( FORTY_METER_POSITION );
}
void toEightyPoint() { // buffer tank 80 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( EIGHTY_METER_POSITION );
}
void toOnesixtyPoint() { // buffer tank 160 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( ONESIXTY_METER_POSITION );
}
void toCWdirection() { // buffer tank jog CW
stepper_1.setSpeedSteps( 5000, 0 );
stepper_1.moveTo(20000);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
}
void toCCWdirection() { // buffer tank jog CCW
stepper_1.setSpeedSteps( 5000, 0 );
stepper_1.moveTo(0);
}