The stepper motor moves the carriage and the limit buttons (Accelstepper)

Hi. On google groups on the subject of "Accelstepper" I found a post by Mike McCauley with the code I wanted to test in my simple project. I am trying to understand the Accelstepper library (I am not a programmer) and use it to move two stepper motors that will be controlled independently with the X, Y joystick - They will guide the trolley on v-slot profiles. For now, I have limited myself to one stepper motor to learn the principle of operation. This code works great for me, but I'd like to add limit buttons to it. I wrote on the google group, but the topic seems to be closed.
I understand that the basis of this program is to keep reading joystick values ​​through the millis () function in a loop. If I add limiting buttons, their value must also be constantly read in the millis () loop so that the cart stops when the limit button is pressed and goes back a few steps in the opposite direction, temporarily deactivating the code responsible for moving the cart with the joystick? So the bounding button loop must take precedence over the X, Y control loop. I wonder where to put the bounding button loop in this code.

Mike's original code looks like this:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3,
4, 5

#define JOYSTICK_PIN A0
#define MAX_SPEED 100
#define INPUT_READ_INTERVAL 100
unsigned long last_input_time = 0;

void setup()
{
Serial.begin(9600);

// Change these to suit your stepper if you want
stepper.setMaxSpeed(0);
stepper.setAcceleration(10);
// stepper.moveTo(1000000000);
}

void loop()
{
// Every INPUT_READ_INTERVAL milliseconds, read inputs.
// We do this infrequently to prevent interfering
// with the stepper motor high speed stepping
// Get the current joystick position as analog value

unsigned long current_time = millis();
if (current_time - last_input_time > INPUT_READ_INTERVAL)
{
int joystick_in = analogRead(JOYSTICK_PIN);
// Map the raw analog value to speed range from -MAX_SPEED to MAX_SPEED
int desired_speed = map(joystick_in, 0, 1023, -MAX_SPEED, MAX_SPEED);
// Serial.println(desired_speed);

// Based on the input, set targets and max speed
stepper.setMaxSpeed(abs(desired_speed));
if (desired_speed == 0 && stepper.speed() == 0)
{
// Prevent running off the end of the position range
stepper.setCurrentPosition(0);
}
else if (desired_speed < 0)
{
stepper.moveTo(-1000000000);
}
else if (desired_speed > 0)
{
stepper.moveTo(1000000000);
}
last_input_time = current_time;
}


stepper.run();
}

Also, based on the post by the user groundFungus from this Arduino forum, I work this way:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 14, 12);

#define JOYSTICK_PIN 34
#define MAX_SPEED 1000
#define INPUT_READ_INTERVAL 100
unsigned long last_input_time = 0;
const byte buttonPin = 27;
bool lastButtonState = HIGH; // button history
bool moveDir = false;  // stepper direction flag
long moveSteps = 800;  // number of steps to move per button press

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  //pinMode(homebtn, INPUT_PULLUP);
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(0);
  stepper.setAcceleration(1000);
  // stepper.moveTo(1000000000);
}

void loop()
{
  unsigned long current_time = millis();
  if (current_time - last_input_time > INPUT_READ_INTERVAL)
  {
    int joystick_in = analogRead(JOYSTICK_PIN);
    // Map the raw analog value to speed range from -MAX_SPEED to MAX_SPEED
    int desired_speed = map(joystick_in, 0, 4095, -MAX_SPEED, MAX_SPEED);
    // Serial.println(desired_speed);

    // Based on the input, set targets and max speed
    stepper.setMaxSpeed(abs(desired_speed));
    bool buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      if (desired_speed == 0 && stepper.speed() == 0)
      {
        // Prevent running off the end of the position range
        stepper.setCurrentPosition(0);
      }
      else if (desired_speed < 0)
      {
        stepper.moveTo(-10000);
      }
      else if (desired_speed > 0)
      {
        stepper.moveTo(10000);
      }
      last_input_time = current_time;
    }
    else if (buttonState == LOW) {
      while (stepper.currentPosition() != 0) // Full speed basck to 0
      {
        stepper.setSpeed(0);
        stepper.run();

        

        // stepper.runToPosition();
        break;
      }
    }
  }
  stepper.run();  // must be called very often
  // ideally once every loop iteration
}

How do I approach this. Does the break; command is the right solution?

Review the attached code give to another user.

You can see the switches are polled every 50ms.

Add as many switches as you need.


//********************************************^************************************************
// Version    YY/MM/DD    Description
// 1.00       22/10/14    Running sketch
//
//


#include <AccelStepper.h>

//ULN2003 Motor Driver Pins
#define IN1                2
#define IN2                3
#define IN3                4
#define IN4                5

#define FULLSTEP           4
#define HALFSTEP           8

#define ENABLED            true
#define DISABLED           false

#define PRESSED            LOW
#define RELEASED           HIGH

AccelStepper myStepper(HALFSTEP, IN1, IN3, IN2, IN4);

const byte heartbeatLED    = 13;
const byte mySwitch        = 8;

boolean motorFlag          = DISABLED;

byte lastMySwitchState;

//timing stuff
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long motorTime;

const unsigned long motorInterval = 5000ul;

//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  for (int i = 2; i <= 4; i++)
  {
    pinMode(i, OUTPUT);
  }

  pinMode(heartbeatLED, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);

  //*************************************
  //continuous rotation stepper movement
  myStepper.setMaxSpeed(500);
  myStepper.setSpeed(400);       //CW
  //myStepper.setSpeed(-400);    //CCW

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatTime >= 500)
  {
    //restart the TIMER
    heartbeatTime = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          s w i t c h   T I M E R
  //is it time to check the switches ? (every 50ms)
  if (millis() - switchTime >= 50)
  {
    //restart the TIMER
    switchTime = millis();

    //go and check the switches
    checkSwitches();
  }

  //*************************************                          m o t o r   T I M E R
  //if enabled, has the motor TIMER expired ?
  if (motorFlag == ENABLED)
  {
    if (millis() - motorTime < motorInterval)
    {
      //must be executed each time through loop() for the motor to turn
      myStepper.runSpeed();
    }

    else
    {
      //turns off the motor
      motorFlag = DISABLED;
    }
  }

  //*************************************
  //other non blocking code goes here
  //*************************************

} //END of   loop()


//********************************************^************************************************
void checkSwitches()
{
  //*********************************************       m y S w i t c h
  //mySwitch code
  byte currentState = digitalRead(mySwitch);

  //**********************
  //was there a change in state ?
  if (lastMySwitchState != currentState)
  {
    //update to the new state
    lastMySwitchState = currentState;

    //**********************
    //when the motor is not turning, is the switch PRESSED ?
    if (motorFlag == DISABLED && currentState == PRESSED)
    {
      //enable the motor TIMER
      motorFlag = ENABLED;

      //restart the motor TIMER
      motorTime = millis();
    }

  } //END of   mySwitch code

} //END of   checkSwitches()

Schematic that goes with the above sketch.

Thank you very much for your answer. I will follow the code to understand it, it will probably take some time, because as I wrote, I am not a programmer. Regards.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.