I'm having an issue with a WHILE loop statement. I'm trying to print the stepper steps while the stepper is moving. When I hold a button down on the IR remote, the stepper moves correctly then stops when I release the button but it doesn't print the position as it moves. I believe I have something wrong in the WHILE statement where it doesn't look at the timers correctly.
Here is the problem area -
// IR remote button controls-----------------------------------------------------------------------------------------------
void processButton()
{
switch (results.value)
{
//**********************
case 0xFF20DF: // Buffer tank CW jog
{
Serial.println("Buffer tank CW jog");
stepper_1.setSpeedSteps( 5000, 500 );
stepper_1.moveTo(20000);
TimerUp = millis();
while (stepper_1.moving()) {
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
if (TimerUpFlag && millis() - TimerUp >= 250ul)
TimerUpFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
stepper_1.stop();
Serial.println(stepper_1.readSteps());
}
break;
Here is my complete code -
// changed to home against hard stop, no limit switch, 2 steppers. IR remote
#define MAX8BUTTONS // saves RAM because only 4 switches are used
#define MAX_STEPPER 6
#include <MobaTools.h>
#include "IRremote.h"
const int STEPS_PER_REV = 800;
const int POS_MAX = 20000;
// 40 meter positions---------------------------------------------------------------------------------------
const int BUFFER_TANK_FORTY_METER_POSITION = 5000; // Buffer tank
const int IPA_TANK_FORTY_METER_POSITION = 3500; // IPA tank
// 80 meter positions----------------------------------------------------------------------------------------
const int BUFFER_TANK_EIGHTY_METER_POSITION = 10000; // Buffer tank
// 160 meter positions------------------------------------------------------------------------------------------
const int BUFFER_TANK_ONESIXTY_METER_POSITION = 16000; // Buffer tank
//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;
// create names for the buttons--------------------------------------------------------------------------------------------
// buttons must switch to Gnd
enum { buttonZero, buttonCW, buttonCCW, forty_meter_button, eighty_meter_button, onesixty_meter_button } ;
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 );
// IR remote --------------------------------------------------------------------------------------------------
const byte RECV_PIN = 8; //IR receive pin
IRrecv irrecv(RECV_PIN); //create instance of 'IRrecv'
decode_results results; //create instance of 'decode_results'
unsigned long TimerUp; //UP arrow on the remote
boolean TimerUpFlag = false;
unsigned long TimerDown; //DOWN arrow on the remote
boolean TimerDownFlag = false;
unsigned long dummy;
unsigned long * TimerPtr = &dummy; //pointer to the current timer
const byte leftLED = 10; //toggles on/off
const byte rightLED = 11; //toggles on/off
// Setup--------------------------------------------------------------------------------------------------
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);
Serial.println("Starting loop");
irrecv.enableIRIn(); //IR remote start receive
}
// reading and processing the buttons ( debouncing and state change detection)------------------------------------------
void loop()
{
myButton.processButtons();
// 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);
}
// IR remote controls------------------------------------------------------------------------------------------
if (irrecv.decode(&results)) //is there IR remote button code
{
//Serial.println(results.value);
processButton(); //process button press
irrecv.resume(); //restart for next button press
}
// Jogging stop timers---------------------------
//********************** // CW button release
//if timing is enabled, is it time to stop
if (TimerUpFlag && millis() - TimerUp >= 250ul)
{
TimerUpFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
stepper_1.stop();
stepper_2.stop();
}
//********************** // CCW button release
//if timing is enabled, is it time to stop
if (TimerDownFlag && millis() - TimerDown >= 250ul)
{
TimerDownFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
stepper_1.stop();
stepper_2.stop();
}
}
// Homing: move all steppers to ref point and set zeropoint----------------------------------------------------------------------------
void toRefPoint() {
Serial.println("homing buffer tank..."); // buffer tank start homing...
stepper_1.setSpeedSteps( 20000, 200 );
stepper_1.doSteps(-18000);
while (stepper_1.moving()) {
Serial.println(stepper_1.readSteps());
}
delay (1000);
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!");
delay(1000);
Serial.println("homing IPA tank..."); // IPA tank start homing...
stepper_2.setSpeedSteps( 20000, 200 );
stepper_2.doSteps(-2000);
while (stepper_2.moving()) {
Serial.println(stepper_2.readSteps());
}
delay (1000);
Serial.println("hard stop reached, now move forward a small bit");
stepper_2.doSteps(100);
while (stepper_2.moving()) {
Serial.println(stepper_2.readSteps());
}
Serial.println("now set zero");
stepper_2.setZero();
stepper_2.moveTo(0);
while (stepper_2.moving()) {
Serial.println(stepper_2.readSteps());
}
Serial.println("buffer tank homing finished!");
}
// 40 meter position-------------------------------------------------------------------------------------------------
void toFortyPoint() {
{ // buffer tank 40 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( BUFFER_TANK_FORTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
delay (1000);
{ // IPA tank 40 meter position
stepper_2.setSpeedSteps( 40000, 500 );
stepper_2.moveTo( IPA_TANK_FORTY_METER_POSITION );
while (stepper_2.moving())
Serial.println(stepper_2.readSteps());
}
}
// 80 meter position------------------------------------------------------------------------------------------------------
void toEightyPoint() { // buffer tank 80 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( BUFFER_TANK_EIGHTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
// 160 meter position-------------------------------------------------------------------------------------------------------
void toOnesixtyPoint() { // buffer tank 160 meter position
stepper_1.setSpeedSteps( 40000, 500 );
stepper_1.moveTo( BUFFER_TANK_ONESIXTY_METER_POSITION );
while (stepper_1.moving())
Serial.println(stepper_1.readSteps());
}
// CW jog buttons-------------------------------------------------------------------------------------------------------------
void toCWdirection() { // buffer tank jog CW
stepper_1.setSpeedSteps( 5000, 500 );
stepper_1.moveTo(20000);
while (stepper_1.moving() ) { // <<<= opening curly brace
// reading in the state of the button again and again and again
// how should your code "know" if a button is pressed or released LATER
// The only way is to read-in the button-state
// again and again and again I_N_S_I_D_E the while-loop
myButton.processButtons(); // <<< read-in button-states
if (myButton.released(buttonCW)) {
stepper_1.stop();
break;
}
Serial.println(stepper_1.readSteps());
}
}
// CCW jog buttons------------------------------------------------------------------------------------------------------------
void toCCWdirection() { // buffer tank jog CCW
stepper_1.setSpeedSteps( 5000, 500 );
stepper_1.moveTo(0);
while (stepper_1.moving() ) { // <<<= opening curly brace
// reading in the state of the button again and again and again
// how should your code "know" if a button is pressed or released LATER
// The only way is to read-in the button-state
// again and again and again I_N_S_I_D_E the while-loop
myButton.processButtons(); // <<< read-in button-states
if (myButton.released(buttonCCW)) {
stepper_1.stop();
break;
}
Serial.println(stepper_1.readSteps());
}
}
// IR remote button controls-----------------------------------------------------------------------------------------------
void processButton()
{
switch (results.value)
{
//**********************
case 0xFF20DF: // Buffer tank CW jog
{
Serial.println("Buffer tank CW jog");
stepper_1.setSpeedSteps( 5000, 500 );
stepper_1.moveTo(20000);
TimerUp = millis();
while (stepper_1.moving()) {
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
if (TimerUpFlag && millis() - TimerUp >= 250ul)
TimerUpFlag = false; //disable timing
TimerPtr = &dummy; //pointer to dummy timer
stepper_1.stop();
Serial.println(stepper_1.readSteps());
}
break;
//**********************
case 0xFFA05F: // Buffer tank CCW jog
{
Serial.println("Buffer tank CCW jog");
TimerPtr = &TimerDown; //point to this timer
TimerDownFlag = true; //enable timing
stepper_1.setSpeedSteps( 5000, 500 );
stepper_1.moveTo(0);
TimerDown = millis();
}
break;
//**********************
case 0xFF10EF: // IPA tank CW jog
{
Serial.println("IPA tank CW jog");
TimerPtr = &TimerUp; //point to this timer
TimerUpFlag = true; //enable timing
stepper_2.setSpeedSteps( 5000, 500 );
stepper_2.moveTo(20000);
TimerUp = millis();
}
break;
//**********************
case 0xFF906F: // IPA tank CCW jog
{
Serial.println("IPA tank CCW jog");
TimerPtr = &TimerDown; //point to this timer
TimerDownFlag = true; //enable timing
stepper_2.setSpeedSteps( 5000, 500 );
stepper_2.moveTo(0);
TimerDown = millis();
}
break;
//**********************
case 0xFF827D: //LEFT Arrow toggle on/off
{
Serial.println("LEFT");
digitalWrite(leftLED, !digitalRead(leftLED)); //Toggle LED
}
break;
//**********************
case 0xFF02FD: //RIGHT Arrow toggle on/off
{
Serial.println("RIGHT");
digitalWrite(rightLED, !digitalRead(rightLED)); //Toggle LED
}
break;
//**********************
case 0xFFFFFFFF: //Repeat code
{
*TimerPtr = millis(); //reset the current timer
}
break;
} // END switch case
}
}