Problems running AccelStepper with Big Easy Driver

Hello,

I am new to electronics and I am currently working on configuring a rig that has a platform travelling along the Y axis. I am using buttons to control the travel.

Here is my hardware:

Nema 17 17HS19-1684S stepper Step Motor, Stepping Motors, Stepper Motors, Brushless Dc ...
Arduino UNO Rev3
Big Easy Driver http://www.hobbytronics.co.uk/big-easy-stepper-driver
DC Power supply HY3003D set to 8V and it draws approx 1A while running the motor.

I am also using parts from other old rigs I didn't build, like a remote control with 10 buttons. For my program, I am trying to use one button (red, pin 2) to stop the stepper, one button (green, pin 3) to make the platform travel up and down until the stop button is pressed, one button (yellow, pin 4) to go down while the button is pressed and one button (orange, pin 5) to move down while the button is pressed.

I got the basic code working from Easy Driver examples patched together (Easy Driver Examples).

Please see for_blog in attachments.

I have commented out the AccelStepper in the above example, and this works and lets me do what I want for now. However, I would like to change to using AccelStepper to be able to ramp hold. Even the most basic sections I am trying do not work as I would like them to. I realise it's a coding issue, because right now I am telling it to move to a certain position on pressing green, then it reaches it, then when I press it again I should re-initialise it but I don't really know how. I would like it to oscillate until I press the red button.

Please see for_blog_accel in attachments.

To be more clear, I want my platform to move up while I hold a button, down when I hold another button. I want it to oscillate on the press of another button until I stop it by pressing another (alternatively on a second press, to stop it from oscillating). Four buttons in total. I always want it to move downwards first when the green is being pressed, and to start the movement from scratch.

On the final rig, I want it all automatically timed, so I can tell it to oscillate by travelling a certain distance up and down for a fixed amount of time. I also want it to run 2 steppers on each side of the platform with exactly the same settings for symmetry, so a group command would be helpful. Including a switch will be how I am planning to initialise the platform (so I can tell it to go down till it hits the switch, then run the oscillating part), but I am clearly out of my depth here.

Best regards,
A.

for_blog.ino (4.14 KB)

for_blog_accel.ino (4.21 KB)

39906054_250942475759731_2350960811729485824_n.jpg

Have you tried the examples that come with the AccelStepper library - do they work?

There is no point trying anything more complicated if the simple examples don't work.

For using the AccelStepper library stepper.run() should be called all the time, and considerably more frequently than the expected step rate. In other words, don't put it inside any IF clause - just put it as the last thing in loop()

...R

Hi,

Yes, I've tried all the examples and they work, my problems only arise when I add the conditions. I will try moving the stepper.run outside the ifs, thank you.

A

Thanks Robin!

I have 2 Projects going on, to digitize 2 mills using stepper motors. Being forced use shotgun intencity to make AccelStepper work I'm glad I choosed another consept.
I have looked at AxelStepper earlier but choosed a more simple, novice friendly, approach.

Hello,

I tried the run.Stepper outside the IF, just in the loop, please see attachment, it is right after the if(Stepping==true) IF, however, it does not change anything.

I then changed the code in the if(Stepping==true), to make it oscillate, please see attachment, but it seems to refuse to take my command to stop on pressing the red button. I am clearly not getting the coding structures right, how can I fix it?

Thank you,
A.

for_blog_accel_runinloop.ino (4.27 KB)

for_blog_accel_green_oscillate.ino (4.26 KB)

aclara:
I tried the run.Stepper outside the IF, just in the loop, please see attachment

It's a few days since you last posted so I have forgotten all about it. You have posted two programs but you have not explained what is different about them.

In any case, let's just focus on one of them - but you decide which one. Ideally it should be the simplest. Then explain in as much detail as you can what happens when you run it.

You code is very hard to read. Please remove everything that is not immediately relevant and use the Auto-Format tool to indent the code consistently. Make sure that every } is on a line by itself. Make sure that every { is the last thing on the line.

...R

Hello,

I only have access to the rig during the week, as it is an internship project, that is why I haven't posted since last week. I will go through the code and follow your instructions.

Thank you.
A.

Hello,

To start with something simple, I made another code.

int Stepping = false; //when program starts, the stepper is not moving

//defining pins on Arduino
const int red = 2; //for pin2 on Arduino, red wire on remote: stop when pressed, continue to not move when released
const int green = 3; //for pin3 on Arduino, green wire on remote: oscillate up and down when pressed, only stop when red button is pressed

const int DIRECTION = 8; //pin8 is DIRECTION
const int STEP = 9; //pin9 is STEP

unsigned long time;

#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER, STEP, DIRECTION); //for Big Easy Driver

void setup() {
  Serial.begin(9600); // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  
  pinMode(DIRECTION, OUTPUT);
  pinMode(STEP, OUTPUT);
  digitalWrite(DIRECTION, HIGH);
  digitalWrite(STEP, LOW);

  pinMode(red, INPUT);//stop
  pinMode(green, INPUT);//up and down

  stepper.setMaxSpeed(3000);
  stepper.setSpeed(500);
  stepper.setAcceleration(1000); 
}

void loop() {

  if (digitalRead(green) == LOW && Stepping == false) { //if green button is pressed and the stepper's not moving
    Stepping = true; //make the stepper move
    Serial.println("Green was pressed");
  }
  else if (digitalRead(red) == LOW && Stepping == true) { //if red button is pressed, the stepper should stop moving until green is pressed again
    Stepping = false;
    Serial.println("Red was pressed");
  }

  //FOR PRESSING GREEN This makes it go up and down in a loop, which is what I call oscillating
  if (Stepping == true) { //tells the stepper how to move
    stepper.runToNewPosition(0); //start at origin
    stepper.runToNewPosition(4000); //move up
    Serial.print("Time for green: ");
    time = millis();
    Serial.println(time);
  }
  else {
    stepper.stop(); //FOR PRESSING RED This only stops the motor if I long press it
    Serial.print("Time for red: ");
    time = millis();
    Serial.println(time);
  }
}

Hopefully this is easier to read.

When I run this, the program starts with the motor not moving, reading the state of the red button I believe. Then, I press the green button, and it starts moving up and down, with what seems to be a much slower code reading, based on what it is printing in the monitor window (please see attached text file). I then press the red button as it is moving, expecting it to stop. Nothing happens, it does not read my action. If I long press it, however, as the movement is back to the 0 point, it reads my command and it stops. The 'Time for red' is obviously much more frequent than 'Time for green' when the program runs.

What I would like it to do is to move up and down after I press green, and not to stop until I press red. When I press red, even if it's during going up and down, I want it to stop instantly.

( I then want to reinitialise the movement, so it takes whatever position it stopped in as the 0 point for when I press green. But this seems to be much further into my future. )

Thanks,
A.

to_discuss_data.txt (8.62 KB)

I find your IF and ELSE IF very confusing. I can't figure the relationship you are trying to capture. Maybe you should not have the ELSE on line 39?

You are using stepper.runToPosition() which will block until it completes so there is no means to detect the STOP button in the middle of the movement. If you want to be able to interrupt a move you should use stepper.run() and call it as the last thing in loop().

I think you code would be easier to understand if you use functional names like stop and start rather than colours like red and green.

...R