Driving stepper for use in oscillating arm

Needing a little help with the coding part of this project.

I am trying to program discrete speeds to a multi-position switch.

I will also be implementing two hall sensors to regulate where the end stop is and switch directions of the motor.

I currently have code that will control rotation direction using the hall sensors, code that works with the switch, and code that drives the motor. I am having trouble putting it all together. I am working on just setting the discrete speeds to the different switch positions.

Can someone point me in the right direction of where I am going wrong? I am wondering if it is an order of operations issue.

I have been using the accelstepper missing manual but still unsure trying to put it together. Largely confused on using .run() or .runSpeed

As you can see, I have the hall sensor part currently turned off.

With the code as is, serial will report back the switch position along with the speed. However, the motor is not turning. Also, when in switch position 1, the speed reports as 0. If I turn to position 2, then back to position 1, it will report the speed increasing by 1 each time it prints.

#include <AccelStepper.h>


//1600 steps/rev 


// // motor Connections
const int dirPin = 2;
const int stepPin = 5;
const int enPin = 8;


AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);


//switch connections
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;


//hall effect sensors
//const byte sensorPin_1 = 18;
//const byte sensorPin_2 = 19;

// //variables
// bool setdir = LOW;  //set dirPinection


//interupt handlers

// void limit_1() {
//   //reverse motor
//   setdir = !setdir;
// }
// void limit_2() {
//   //reverse motor
//   setdir = !setdir;
// }


void setup() {


  //switch set-up
  pinMode(motoroff, INPUT_PULLUP);
  pinMode(motoron4, INPUT_PULLUP);
  pinMode(motoron5, INPUT_PULLUP);
  pinMode(motoron6, INPUT_PULLUP);
  pinMode(motoron7, INPUT_PULLUP);


  //stepper set-up
  stepper.setEnablePin(enPin);
  stepper.setMaxSpeed(1600);
  stepper.setAcceleration(100);
  stepper.disableOutputs();

  // //hall effect sensor set-up
  // pinMode(sensorPin_1, INPUT);  //declares hall sensor as input
  // pinMode(sensorPin_2, INPUT);  //""

  // attachInterrupt(digitalPinToInterrupt(sensorPin_1), limit_1, FALLING);  //do not change it to 'CHANGE'
  // attachInterrupt(digitalPinToInterrupt(sensorPin_2), limit_2, FALLING);

  Serial.begin(9600);
  Serial.println("testing Accelstepper");
}


//set speed to 0 in switch postion 1
void switch1() {
  Serial.println("Switch in Position 1");
  stepper.disableOutputs();
  stepper.stop();
}
//set speed in switch postion 2
void switch2() {
  Serial.println("Switch in Position 2");
  stepper.enableOutputs();
  stepper.setSpeed(2);
  stepper.runSpeed();
}

//set speed in switch postion 3
void switch3() {
  Serial.println("Switch in Position 3");
  stepper.enableOutputs();
  stepper.setSpeed(4);
  stepper.runSpeed();
}
//set speed in switch postion 4
void switch4() {
  Serial.println("Switch in Position 4");
  stepper.enableOutputs();
  stepper.setSpeed(6);
  stepper.runSpeed();
}

//set speed in switch postion 5
void switch5() {
  Serial.println("Switch in Position 5");
  stepper.enableOutputs();
  stepper.setSpeed(8);
  stepper.runSpeed();

}


void switchloop() {
  //switch loop
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);

  if (switch_position_1_status == LOW) {
    switch1();

  } else if (switch_position_2_status == LOW) {
    switch2();

  } else if (switch_position_3_status == LOW) {
    switch3();

  } else if (switch_position_4_status == LOW) {
    switch4();

  } else if (switch_position_5_status == LOW) {
    switch5();

  }
}


void loop()
//main code here, run repeatedly
{

  switchloop();  //checks switch position
  float mSpeed = stepper.speed();
  Serial.println(mSpeed);
  //endstop(); //end stop function
}

I want to reply and say that I have solved my issues.

I was able to get the motor turning at low speeds by getting rid of the enableOutput() and disableOutput(). Not sure why that was screwing things up. I was still running into an issue where it would cap out motor speed. I realized that the serial commands were hurting the motor speed, therefore commenting them out fixed the issue.

Since it may be helpful, I will post the code that works for me. If anyone takes a look and finds a more efficient way, please comment.

Now it's time to implement the hall sensors.

#include <AccelStepper.h>


//1600 steps/rev 


// // motor Connections
const int dirPin = 2;
const int stepPin = 5;
//const int enPin = 8;


AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);


//switch connections
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;


//hall effect sensors
const byte sensorPin_1 = 18;
const byte sensorPin_2 = 19;

//variables
bool setdir = LOW;  //set dirPinection


//interupt handlers

void limit_1() {
  //reverse motor
  setdir = !setdir;
}
void limit_2() {
  //reverse motor
  setdir = !setdir;
}


void setup() {


  //switch set-up
  pinMode(motoroff, INPUT_PULLUP);
  pinMode(motoron4, INPUT_PULLUP);
  pinMode(motoron5, INPUT_PULLUP);
  pinMode(motoron6, INPUT_PULLUP);
  pinMode(motoron7, INPUT_PULLUP);


  //stepper set-up
  //stepper.setEnablePin(enPin);
  stepper.setMaxSpeed(2000);
  //stepper.setAcceleration(100);
  // stepper.disableOutputs();

  //hall effect sensor set-up
  pinMode(sensorPin_1, INPUT);  //declares hall sensor as input
  pinMode(sensorPin_2, INPUT);  //""

  attachInterrupt(digitalPinToInterrupt(sensorPin_1), limit_1, FALLING);  //do not change it to 'CHANGE'
  attachInterrupt(digitalPinToInterrupt(sensorPin_2), limit_2, FALLING);

  Serial.begin(9600);
  Serial.println("testing Accelstepper");
}


//set speed to 0 in switch postion 1
void switch1() {
  stepper.setSpeed(0);
}
//set speed in switch postion 2
void switch2() {
  stepper.setSpeed(400);

}

//set speed in switch postion 3
void switch3() {
  stepper.setSpeed(800);
}
//set speed in switch postion 4
void switch4() {
  stepper.setSpeed(1200);
}

//set speed in switch postion 5
void switch5() {
  stepper.setSpeed(1600);

}


void switchloop() {
  //switch loop
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);

  if (switch_position_1_status == LOW) {
    switch1();

  } else if (switch_position_2_status == LOW) {
    switch2();

  } else if (switch_position_3_status == LOW) {
    switch3();

  } else if (switch_position_4_status == LOW) {
    switch4();

  } else if (switch_position_5_status == LOW) {
    switch5();

  }
}

void endstop()
  

void loop()
//main code here, run repeatedly
{

  switchloop();  //checks switch position
  stepper.runSpeed();

You should declare setdir with the volatile keyword since it is used in an interrupt.

To build off that. I am using a Hall sensor on both ends of travel.

Can I multiply the speed integer by a negative value to flip the direction when the sensor goes low?

The example code I found for exactly what I am trying to do is below. However, I am unsure how to implement this with the accelstepper library.

// Define connections
#define HALL_SENSOR_A      18
#define HALL_SENSOR_B      19
#define DIR      2
#define STEP      5

// Variables
int pd = 400;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handlers

void limit_a (){

  // Reverse motor
  setdir = !setdir;
  
}

void limit_b (){

  // Reverse motor
  setdir = !setdir;
  
}


void setup() {
  
  // Setup the stepper controller pins as Outputs
  pinMode(DIR,OUTPUT); 
  pinMode(STEP,OUTPUT);
  
  // Setup the Hall Effect switches as Inputs
  pinMode(HALL_SENSOR_A, INPUT);
  pinMode(HALL_SENSOR_B, INPUT);
  
  // Attach interrupt pins to handlers
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_A), limit_a, FALLING);
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_B), limit_b, FALLING);
   
}

void loop() {
  
    digitalWrite(DIR,setdir);
    digitalWrite(STEP,HIGH);
    delayMicroseconds(pd);
    digitalWrite(STEP,LOW);
    delayMicroseconds(pd);
 
}

The RIGHT direction is to begin your program by finding ONE end stop and calling that ZERO. Then finding the other end stop and count the number of steps from the first end stop. Now work with the number of steps between the two. Half the steps will be midway.

1 Like

How far does the arm oscillate? In degrees? In steps?

This is what I am building. An oscillating boom. There is about 317mm of available travel. Currently at 1600steps/rev therefore, were looking at about 63400 steps total.

I now have a homing function built in. I guess I could just get by with one sensor for homing and then telling the stepper to move to the end and come back?

I'd start with the accelstepper bounce example, set the arm in the middle, and set the stepper.moveTo(500); line to a number of steps shorter than your limits. That example uses .run() so it can do accelleration/decelleration.

I'd then extend the bounce example to add code to detect the endstops and save and report the position at the end. Completely untested code:

// Bounce.pde  
// extended for https://forum.arduino.cc/t/driving-stepper-for-use-in-oscillating-arm/1104841
// -*- mode: C++ -*-
//
// Make a single stepper bounce from one limit to another
//
// Copyright (C) 2012 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
 
#include <AccelStepper.h>
 
// Define a stepper and the pins it will use
// // motor Connections
const int dirPin = 2;
const int stepPin = 5;
const int enPin = 8;

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

#define HALL_SENSOR_A      18
#define HALL_SENSOR_B      19

int rightMax = 500;
int leftMax = -500;
 
void setup()
{  
    // Setup the Hall Effect switches as Inputs
  pinMode(HALL_SENSOR_A, INPUT);
  pinMode(HALL_SENSOR_B, INPUT);
   Serial.begin(115200);
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(20);
  stepper.moveTo(rightMax);
}
 
void loop()
{
    // If at the end of travel go to the other end
    if (digitalRead(HALL_SENSOR_A) == LOW){
      rightMax = stepper.currentPosition();
      stepper.moveTo(leftMax);
      Serial.print("reached right endstop at step");
      Serial.println(rightMax); 
     }
    if (digitalRead(HALL_SENSOR_B) == LOW){
      leftMax = stepper.currentPosition();
      stepper.moveTo(rightMax);
      Serial.print("reached left endstop at step");
      Serial.println(rightMax); 
     } 

    stepper.run();
}

Thank you for the reply.

My one question for this is implementation with the rotary switch I am using. I was having a hard time programming set speeds based on the switch position while using the run() command. However, I have figured it out using runSpeed().

Any thoughts there?

Here's where my code is at currently, the endstop function doesn't work as written:

#include <AccelStepper.h>


//1600 steps/rev 
//8mm travel per revolution
//317mm total travel length

//total travel - 63400 steps

// // motor Connections
const int dirPin = 2;
const int stepPin = 5;
//const int enPin = 8;




AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);


//switch connections
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;


//hall effect sensors
const byte sensorPin_1 = 18;
const byte sensorPin_2 = 19;

//variables
volatile bool setdir = LOW;  //set dirPinection


//interupt handlers

void limit_1() {
  //reverse motor
  setdir = !setdir;
}
void limit_2() {
  //reverse motor
  setdir = !setdir;
}

void homefunction() {
  while (digitalRead(sensorPin_1) == HIGH) {
    stepper.setSpeed(-1000);
    stepper.runSpeed();
      }
}

void setup() {


  //switch set-up
  pinMode(motoroff, INPUT_PULLUP);
  pinMode(motoron4, INPUT_PULLUP);
  pinMode(motoron5, INPUT_PULLUP);
  pinMode(motoron6, INPUT_PULLUP);
  pinMode(motoron7, INPUT_PULLUP);


  //stepper set-up
  //stepper.setEnablePin(enPin);
  stepper.setMaxSpeed(2000);
  //stepper.setAcceleration(100);
  // stepper.disableOutputs();

  //hall effect sensor set-up
  pinMode(sensorPin_1, INPUT);  //declares hall sensor as input
  pinMode(sensorPin_2, INPUT);  //""

  attachInterrupt(digitalPinToInterrupt(sensorPin_1), limit_1, FALLING);  //do not change it to 'CHANGE'
  attachInterrupt(digitalPinToInterrupt(sensorPin_2), limit_2, FALLING);

  homefunction();

  

  Serial.begin(9600);
  Serial.println("testing Accelstepper");

}


//set speed to 0 in switch postion 1
void switch1() {
  //Serial.println("Switch in Position 1");
  // stepper.disableOutputs();
  stepper.setSpeed(0);
  //stepper.stop();
}
//set speed in switch postion 2
void switch2() {
  //Serial.println("Switch in Position 2");
  //stepper.enableOutputs();
  stepper.setSpeed(400);
  //stepper.runSpeed();
}

//set speed in switch postion 3
void switch3() {
  //Serial.println("Switch in Position 3");
  //stepper.enableOutputs();
  stepper.setSpeed(800);
  //stepper.runSpeed();
}
//set speed in switch postion 4
void switch4() {
  //Serial.println("Switch in Position 4");
  //stepper.enableOutputs();
  stepper.setSpeed(1200);
  //stepper.runSpeed();
}

//set speed in switch postion 5
void switch5() {
  //Serial.println("Switch in Position 5");
  //stepper.enableOutputs();
  stepper.setSpeed(1600);
  //stepper.runSpeed();

}


void switchloop() {
  //switch loop
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);

  if (switch_position_1_status == LOW) {
    switch1();

  } else if (switch_position_2_status == LOW) {
    switch2();

  } else if (switch_position_3_status == LOW) {
    switch3();

  } else if (switch_position_4_status == LOW) {
    switch4();

  } else if (switch_position_5_status == LOW) {
    switch5();

  }
}

//void endstop(){
  // if (sensorPin_1 == LOW) {
 //   digitalWrite(dirPin, HIGH);
  } 
  
 // else if (sensorPin_2 == LOW) {
//    digitalWrite(dirPin, LOW);
  }  
}


void loop()
//main code here, run repeatedly
{

  switchloop();  //checks switch position
  endstop(); //end stop function
  stepper.runSpeed();
  float mSpeed = stepper.speed();
  //Serial.println(mSpeed);


}

Well, with a quarter of a million steps, that won't fit in an int, so I'd change my answer to use:

long int rightMax = 253600;
long int leftMax = -253600;

to be sure it worked from any starting position.

To add in the rest of the controls, I'd add in your switchloop() and it's children, and have them update the stepper.moveTo() target.

but instead of triggering a LED, I'd have them

1 Like

runSpeed doesn't care about position, it just takes steps at speed.

On my extended bounce example code, to add in the switch controls, I'd have the switchloop() and children adjust stepper.setMaxSpeed(xxx) and I'd also wrap some ideas from BlinkWithoutDelay and StateChangeDetection example so that rather than continuously, they only make one setting change per switch change:

void daveXswitchloop() {
  //switch loop
  // debounce this loop with millis():
  static unsigned long last = 0;
  unsigned long interval = 100;
  if(millis() - last < interval) return;  // too early
  last = millis();
  static int lastState[6] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH};
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);


  if (switch_position_1_status == LOW && lastState[1] == HIGH) {
    switch1();
  } else if (switch_position_2_status == LOW && lastState[2] == HIGH)) {
    switch2();

  } else if (switch_position_3_status == LOW && lastState[3] == HIGH)) {
    switch3();

  } else if (switch_position_4_status == LOW && lastState[4] == HIGH)) {
    switch4();

  } else if (switch_position_5_status == LOW&& lastState[5] == HIGH)) {
    switch5();
  }
  lastState[1]= switch_position_1_status;
  lastState[2]= switch_position_2_status;
  lastState[3]= switch_position_3_status;
  lastState[4]= switch_position_4_status;
  lastState[5]= switch_position_5_status;

}
1 Like

Thank you Dave. I now have a working solution!

Here is my final code in case someone may find it useful.

#include <elapsedMillis.h>
#include <AccelStepper.h>

//1600 steps/rev 
//8mm travel per revolution
//317mm total travel length

//total travel - 63400 steps

// // motor Connections
const int dirPin = 2;
const int stepPin = 5;

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

//switch connections
// 23 - off
// 25 - Drum 4rpm
// 27 - Drum 5rpm
// 29 - Drum 6rpm
// 31 - Drum 7rpm
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;

//hall effect sensors
const byte sensorPin_1 = 18;
const byte sensorPin_2 = 19;

//variables
long int rightMax = 253600;
long int leftMax = -253600;

void homefunction() {
  while (digitalRead(sensorPin_1) == HIGH) {
    stepper.setSpeed(-1000);
    stepper.runSpeed();
      }
}

void setup() {

  //switch set-up
  pinMode(motoroff, INPUT_PULLUP);
  pinMode(motoron4, INPUT_PULLUP);
  pinMode(motoron5, INPUT_PULLUP);
  pinMode(motoron6, INPUT_PULLUP);
  pinMode(motoron7, INPUT_PULLUP);

  //stepper set-up
  //stepper.setEnablePin(enPin);
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(10000);
  // stepper.disableOutputs();

  //hall effect sensor set-up
  pinMode(sensorPin_1, INPUT);  //declares hall sensor as input
  pinMode(sensorPin_2, INPUT);  //""

  homefunction();
}

//set speed to 0 in switch postion 1
void switch1() {
  stepper.setMaxSpeed(0);
}
//set speed in switch postion 2
void switch2() {
  stepper.setMaxSpeed(400);
}

//set speed in switch postion 3
void switch3() {
  stepper.setMaxSpeed(800);
}
//set speed in switch postion 4
void switch4() {
  stepper.setMaxSpeed(1200);
}

//set speed in switch postion 5
void switch5() {
  stepper.setMaxSpeed(1600);
}

void daveXswitchloop() {
  //switch loop
  // debounce this loop with millis():
  static unsigned long last = 0;
  unsigned long interval = 100;
  if(millis() - last < interval) return;  // too early
  last = millis();
  static int lastState[6] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH};
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);


  if (switch_position_1_status == LOW && lastState[1] == HIGH) {
    switch1();
  } else if (switch_position_2_status == LOW && lastState[2] == HIGH) {
    switch2();

  } else if (switch_position_3_status == LOW && lastState[3] == HIGH) {
    switch3();

  } else if (switch_position_4_status == LOW && lastState[4] == HIGH) {
    switch4();

  } else if (switch_position_5_status == LOW&& lastState[5] == HIGH) {
    switch5();
  }
  lastState[1]= switch_position_1_status;
  lastState[2]= switch_position_2_status;
  lastState[3]= switch_position_3_status;
  lastState[4]= switch_position_4_status;
  lastState[5]= switch_position_5_status;
}

void endstop(){
   if (digitalRead(sensorPin_1) == LOW) {
    //leftMax = stepper.currentPosition();
    stepper.moveTo(rightMax);
  } 
  
  else if (digitalRead(sensorPin_2) == LOW) {
    //rightMax = stepper.currentPosition();
    stepper.moveTo(leftMax);
  }  
}

void loop()
//main code here, run repeatedly
{

  daveXswitchloop();  //checks switch position
  endstop(); //end stop function
  stepper.run();
}
1 Like

Great. You had most of it, and it looks like my suggestions helped you out.

Good luck.

I am back to having issues with my code, and am lost finding the right solution.

The only issue is that after homing, the stepper will run full speed in negative direction with the switch in the "1" position. In switch position "1", I want the stepper to home and stay where it's at.

I believe it is due to me calling stepper.run() in the loop; however, I have set the speed to 0 in switch1().

Anywhere I try an use stepper.stop() it is not working for me.

Any guidance here?

#include <elapsedMillis.h>
#include <AccelStepper.h>

//800 steps/rev 
//8mm travel per revolution
//317mm total travel length

//total travel - 31700 steps

// // motor Connections
const int dirPin = 2;
const int stepPin = 5;

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

//switch connections
// 23 - off
// 25 - Drum 4rpm
// 27 - Drum 5rpm
// 29 - Drum 6rpm
// 31 - Drum 7rpm
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;

//hall effect sensors
const byte sensorPin_1 = 18;
const byte sensorPin_2 = 19;

//variables
long int rightMax = 0;
long int leftMax = -25780;



// void serial() {
//   Serial.begin(9600);
//   Serial.println("testing accelstepper");
//   }


void homefunction() {
  while (digitalRead(sensorPin_2) == HIGH) {
    stepper.setSpeed(1000);
    stepper.runSpeed();
      }
  stepper.setCurrentPosition(0);
  //stepper.stop();
  //Serial.println("Homing Completed");
  //Serial.println(stepper.currentPosition());     
  //while (digitalRead(sensorPin_2) == LOW) {
    //stepper.stop();
  //}     
 }

void setup() {

  //switch set-up
  pinMode(motoroff, INPUT_PULLUP);
  pinMode(motoron4, INPUT_PULLUP);
  pinMode(motoron5, INPUT_PULLUP);
  pinMode(motoron6, INPUT_PULLUP);
  pinMode(motoron7, INPUT_PULLUP);

  //stepper set-up
  //stepper.setEnablePin(enPin);
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(15000);
  // stepper.disableOutputs();

  //hall effect sensor set-up
  pinMode(sensorPin_1, INPUT);  //declares hall sensor as input
  pinMode(sensorPin_2, INPUT);  //""

  //serial();
  homefunction();
  //stepper.stop();
}

//set speed to 0 in switch postion 1
void switch1() {
  stepper.setMaxSpeed(0);
  //stepper.stop();
}
//set speed in switch postion 2
void switch2() {
  stepper.setMaxSpeed(1409);
}

//set speed in switch postion 3
void switch3() {
  stepper.setMaxSpeed(1761);
}
//set speed in switch postion 4
void switch4() {
  stepper.setMaxSpeed(2114);
}

//set speed in switch postion 5
void switch5() {
  stepper.setMaxSpeed(2467);
}

void daveXswitchloop() {
  //switch loop
  // debounce this loop with millis():
  static unsigned long last = 0;
  unsigned long interval = 100;
  if(millis() - last < interval) return;  // too early
  last = millis();
  static int lastState[6] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH};
  int switch_position_1_status = digitalRead(motoroff);
  int switch_position_2_status = digitalRead(motoron4);
  int switch_position_3_status = digitalRead(motoron5);
  int switch_position_4_status = digitalRead(motoron6);
  int switch_position_5_status = digitalRead(motoron7);


  if (switch_position_1_status == LOW && lastState[1] == HIGH) {
    switch1();
  } else if (switch_position_2_status == LOW && lastState[2] == HIGH) {
    switch2();

  } else if (switch_position_3_status == LOW && lastState[3] == HIGH) {
    switch3();

  } else if (switch_position_4_status == LOW && lastState[4] == HIGH) {
    switch4();

  } else if (switch_position_5_status == LOW&& lastState[5] == HIGH) {
    switch5();
  }
  lastState[1]= switch_position_1_status;
  lastState[2]= switch_position_2_status;
  lastState[3]= switch_position_3_status;
  lastState[4]= switch_position_4_status;
  lastState[5]= switch_position_5_status;
}

void endstop(){
   if (digitalRead(sensorPin_1) == LOW) {
    //leftMax = stepper.currentPosition();
    stepper.moveTo(rightMax);
    //Serial.println(stepper.currentPosition());
  } 
  
  else if (digitalRead(sensorPin_2) == LOW) {
    //rightMax = stepper.currentPosition();
    stepper.moveTo(leftMax);
  }  
}

void loop()
//main code here, run repeatedly
{

  daveXswitchloop();  //checks switch position
  endstop(); //end stop function
  stepper.run();
}

I'm going to bump this to retain visibility.

accelstepper has a great dis-advantage:
accelstepper has either blocking functions where you must wait until reaching the target-position has happened or non-blocking functions that require to be called very very often at least calling the function more often per second than the step-pulses occur

There is a different stepper-motor-library where the pulses for the stepper-motor are created based on a timer-interrupt which enables to create the step-pulses "in the back-ground" and you are able to execute other code while step-pulses are created.

You can change the speed including a stop at any time even in the middle of any movement.

The function calls have different names than in accel-stepper but the advantage of "set and forget" to move any amout of steps and still beeing able to stop at any time and to change speed at any time makes life much more easier

best regards Stefan

I think your problem may be in the endstop() routine. If you are sitting on an endstop, it updates the target towards the opposite endstop, and then it will try to accelerate away and would countermand a stepper.stop()

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