I'm having a problem with my CW jog button. I'm hoping to read the steps as I hold the button down and stop when I release button. When I press the CW button, I have stepper_1.moveTo(20000);
so that it stops at the 20000 position if I held it down. However, I think my code is blocking it somehow. When I press and release the button, the stepper just runs all the way to 20000 while its counting steps in serial monitor. I tried moving this code to the Loop section and no change. Any ideas?
Here's the problem area -
void toCWdirection() { // buffer tank jog CW
stepper_1.setSpeedSteps( 5000, 0 );
stepper_1.moveTo(20000);
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
Here's my complete code -
// trying to home without use of limit switch. cw ccw buttons go same way.
#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
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...
stepper_1.setSpeedSteps( 10000, 200 );
stepper_1.doSteps(-2000);
while (stepper_1.moving()) {
Serial.println(stepper_1.readSteps());
}
delay (2000);
Serial.println("hard stop reached, now move forward a small bit");
stepper_1.doSteps(100);
while (stepper_1.moving()) {
Serial.println(stepper_1.readSteps());
}
Serial.println("now set zero");
stepper_1.setZero();
stepper_1.moveTo(0);
while (stepper_1.moving()) {
Serial.println(stepper_1.readSteps());
}
Serial.println("buffer tank homing finished!");
}
void toFortyPoint() { // buffer tank 40 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( FORTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
void toEightyPoint() { // buffer tank 80 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( EIGHTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
void toOnesixtyPoint() { // buffer tank 160 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( ONESIXTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
void toCWdirection() { // buffer tank jog CW
stepper_1.setSpeedSteps( 5000, 0 );
stepper_1.moveTo(20000);
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
void toCCWdirection() { // buffer tank jog CCW
stepper_1.setSpeedSteps( 5000, 0 );
stepper_1.moveTo(0);
}