I' trying to get a stepper to run at fast speed (250) with accelstepper.h :AccelStepper: AccelStepper library for Arduino
Then when the position reaches 1300 it should slow down with acceleration to 100 and continue to move at speed 100 with acceleration untill position() 2380 is reached and then get back to position() 0 at max speed with no accelerations.
I'm trying to do it with the code below but he seems not to be listening to the different speeds.
I tried using setSpeed but tat gives a constant speed and i need accelerations.
Hope some can help.
if (startButtonState == 0 && homePin2StateA == 1 && crashButtonPressed == false)
{
digitalWrite(enableL, LOW);
stepperL.setMaxSpeed(250);
stepperL.setAcceleration(200);
stepperL.moveTo(1300); //set to 2380
stepperL.setSpeed(250);
if (stepperL.distanceToGo()== 0)
{stepperL.setMaxSpeed(100);
stepperL.setAcceleration(200);
stepperL.moveTo(1000); //set to 2380
}
stepperL.run();
}
i'm sry for the mistakes i'm relative nes to all of this.
This is the whole code with: UNCONDITIONALLY calling the run() method on EVERY pass through loop() for the stepperL.
Except in the while function for stepperA.
//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>
//NEMA 17 (Actuator)
AccelStepper stepperA(AccelStepper::DRIVER, 3, 4); // (DRIVER, Step, Directon)
int enableA = 2;
//NEMA 23 (Linear motion)
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8); // (DRIVER, Step, Directon)
int enableL = 11;
//_____________________________________________________________
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 2000;
enum motorStateENUM {
WAITING,
FIRSTRUN,
BETWEENRUNS,
SECONDRUN,
SECONDRUNDONE,
THIRDRUN,
FINISHED,
};
motorStateENUM motorState = WAITING;
//_______________________________________________________________
//productCrashPin
const int crashPin1 = 6;
int crashPin1State;
boolean crashButtonPressed = false;
//ACTUATOR
const int homePinA = 10;
int homePin2StateA;
//STARTBUTTON
const int startButton = 9;
int startButtonState = 0; //1 = open->not pressed
//STRATBUTTON TOGGLE
int startButtonReading; // the current reading from the input pin
int previouStartButtonReading = 0; // the previous reading from the input pin
//LINEAR MOTION MOTOR
const int homePinL = 5;
int homePinStateL;
boolean atStartPosition = false;
void setup() {//================================================================
stepperA.setMaxSpeed(2500);
stepperA.setAcceleration(15000);
pinMode(crashPin1, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(homePinA, INPUT_PULLUP);
pinMode(homePinL, INPUT_PULLUP);
pinMode(enableA, OUTPUT);
pinMode(enableL, OUTPUT);
//digitalWrite(enableA, HIGH);
// digitalWrite(enableL, HIGH);
Serial.begin(115200);
delay(200);
///////////////////////////STEPPERS GET BACK TO START POSITION\\\\\\\\\\\\\\\\\\\\\\
while (atStartPosition == false)
{
homePin2StateA = digitalRead(10);
homePinStateL = digitalRead(homePinL);
//_______________________________\\
if (homePin2StateA == 0 ) // 0 = not at his start position - 1 = at his position
{ //Serial.println (homePin2StateA);
digitalWrite(enableA, LOW);
stepperA.setSpeed(-1900); //set to -1900
stepperA.runSpeed();
}
if (homePin2StateA == 1)
{
stepperA.setCurrentPosition(0);
}
if (homePinStateL == 1 )
{ digitalWrite(enableL, LOW);
stepperL.setMaxSpeed(250);
stepperL.setAcceleration(300);
stepperL.moveTo(-2500);
stepperL.run();
}
if (homePinStateL == 0 )
{
stepperL.setCurrentPosition(0);
}
if (stepperA.currentPosition() == 0 and stepperL.currentPosition() == 0 )
{
(atStartPosition = false);
delay(200);
digitalWrite(enableA, HIGH);
digitalWrite(enableL, HIGH);
break;
}
}
///////////////////////////STEPPERS AT START POSITION////////////////////////////////
}
void loop() {//=========================================================
//Serial.println (homePin2StateA);
//Serial.println (digitalRead(startButton));
crashPin1State = digitalRead(crashPin1);
homePin2StateA = digitalRead(10);
if (crashPin1State == 0) {
crashButtonPressed = true;
}
////////////////////////////////////////////////////////////////////////
startButtonReading = digitalRead(startButton);
if (startButtonReading == 1 && previouStartButtonReading == 0)
{ if (startButtonState == 1)
startButtonState = 0;
else
startButtonState = 1;
}
/////////////////////////////////////////////////////////////////////////
if (startButtonState == 0 && homePin2StateA == 1 && crashButtonPressed == false)
{
digitalWrite(enableL, LOW);
stepperL.setMaxSpeed(250);
stepperL.setAcceleration(100);
stepperL.moveTo(1300); //set to 1300
//stepperL.setSpeed(250);
if (stepperL.distanceToGo()== 0)
{stepperL.setMaxSpeed(100);
stepperL.setAcceleration(200);
stepperL.moveTo(2380); //set to 2380
}
}
stepperL.run();
if (startButtonState == 0 and crashButtonPressed == true)
{ digitalWrite(enableL, HIGH);
digitalWrite(enableA, LOW);
// Serial.print(" Crash Pin1 state voor While = ");Serial.println (crashButtonPressed);
while (crashButtonPressed == true)
{
currentMillis = millis();
//Serial.print(" Crash Pin1 state in the While = ");Serial.println (crashButtonPressed);
if (motorState == WAITING) {
stepperA.moveTo(4000); // set to 4500
//stepperA.setSpeed(2000);
motorState = FIRSTRUN;
//Serial.println("first run");
}
if (motorState == BETWEENRUNS) {
if (currentMillis - endFirstRunMillis >= interval)
{
// Serial.println("second run");
//stepperA.setMaxSpeed(2800);
//stepperA.setAcceleration(15000);
//stepperA.setSpeed(2800);
stepperA.moveTo(8000); // set to 9000
motorState = SECONDRUN;
}
}
if (motorState == SECONDRUNDONE) {
//Serial.println("second run done");
stepperA.moveTo(0);
motorState = THIRDRUN;
}
if (stepperA.distanceToGo() == 0 ) {
if (motorState == FIRSTRUN) {
endFirstRunMillis = currentMillis;
motorState = BETWEENRUNS;
//Serial.println("between runs");
}
if (motorState == SECONDRUN) {
motorState = SECONDRUNDONE;
//Serial.println("at the end");
}
if (motorState == THIRDRUN) {
motorState = WAITING;
//Serial.println("finished");
crashButtonPressed = false;
digitalWrite(enableA, HIGH);
}
}
// stepperA.runSpeed();
stepperA.run();
}
}
previouStartButtonReading = startButtonReading;
}
Kumalix:
i'm sry for the mistakes i'm relative nes to all of this.
This is the whole code with:
You are not so new that people should have to waste time reminding you to post your complete program.
And this seems to be a continuation of the same project in your other Thread
You have not explained how your new question relates to all of the stuff you have already had help with - I don't want to be repeating myself.
Have you written a simple single-purpose program to learn how to control the motor the way you describe here? If not, do so. Then post the code if it does not work.
Don't try to learn a new technique within an existing complex program.
Do you KNOW what run() does? If not, it's time you learned.
It might make the stepper take a step. It might not. Whether it does, doesn't depends on two things. First, does the stepper need to move at all? If it's current position is 100, and it was commanded to move to 100, it doesn't need to move, so run() will not tell it to move.
If it does need to move, but it isn't time to step again, run() won't make the stepper move.
It should be obvious that you need to ditch ALL the while loops and you need to call run() for both steppers.
The loop() function, not surprisingly, loops. Let it do all the looping necessary.
On any given pass through loop(), you might be up against a limit switch, so you should stop. That means telling the stepper that where it is is where it needs to be. You might be moving towards the limit switch, in a homing move. If so, and you aren't at home, you want to move ONE step closer to home.
Yes there is my other tread but because of this being other issue (piece of code) then my subject of that thread i thought to place this isolated issue in a new thread. If you guy's prefer to continue on 1 thread about all the issues i come accros (and i probably will ) i wil post it on 1 thread.
@Paul: giving me some explanation about the accelstepper.h and with Robins input about "simpel code" for this specifiek move i have made a simpel skatch and i think solved a part of the problem
I should be doing something like this. Knowing the position of the stepper and then giving it commands what to do. Is this the way i should continue experimenting/testing?
Need to get it back to position 0 again. but that should not be a problem with the code below?
//DRV8825 VREF voltage NEMA 17= 0,65V can be 0.8V
//DRV8825 VREF voltage NEMA 23= 0,6AV can be 0.6V
#include <AccelStepper.h>
AccelStepper stepperL(AccelStepper::DRIVER, 7, 8); // (DRIVER, Step, Directon)
void setup()
{
}
void loop() {
if (stepperL.currentPosition() == 0 )
{
stepperL.setMaxSpeed(250);
stepperL.setAcceleration(300);
stepperL.moveTo(500);
}
if (stepperL.currentPosition() == 500 )
{
stepperL.setMaxSpeed(100);
stepperL.setAcceleration(150);
stepperL.moveTo(1000);
}
stepperL.run();
}
Kumalix:
I should be doing something like this. Knowing the position of the stepper and then giving it commands what to do. Is this the way i should continue experimenting/testing?
Does that program do what you want?
If not, what does it do?
The program in Reply #6 does not seem to be an attempt to implement what you described in your Original Post
Then when the position reaches 1300 it should slow down with acceleration to 100 and continue to move at speed 100 with acceleration untill position() 2380 is reached and then get back to position() 0 at max speed with no accelerations.
And, AFAIK the AccelStepper library is not intended for what you describe. It is designed to move N steps starting and ending at zero speed.
It is not particularly difficult to write your own code for accelerations and then you can do what you want. See the code in this link.
Also, you need to read carefully the piece I quoted as it contains some practical errors.