I am learning and testing and i have the following problem.
This code make a stepper turn and stop at some define positions. My problem is that when the loop end(so i think) the motor return to the starting position and do all the steps again. For this reason i want to stop it using the stop() you see at the end.
This stop produce an error in compiling!
What do i do wrong since the AccelStepper doc mention this stop()
Also from where should i call the loop to have the process done only one time?
Thank for any help for this new user.
#include <AccelStepper.h>
int motorSpeed = 2500;
int motorAccel = 600;
int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3
int x;
int POSstp;
int led = 11;
int led1 = 8;
//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin);
void setup(){
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
Serial.begin(9600);
}
void loop(){
x = stepper.currentPosition();
while (x < 3200) {
Serial.println(x);
stepper.runToNewPosition(x);
digitalWrite(led, HIGH);
digitalWrite(led, LOW);
x = x + 800; // diff between each stop
}
stepper.stop();
}
The point of the loop() function is that it is called repeatedly. Your loop goes to some position then iterates to a second position, then returns. Loop() is then called again, it goes back to the initial position, ad infinitum.
If you want your sketch to stop outright after completing a single task, you must either put it into an infinite loop or shutdown the CPU.
If the code must go in loop() just have a global variable that starts as "false" and changes to "true" when the motor has run. Don't run the motor if the value of that variable is "true".
Hi,
The code is the code at the begining of this thread.
The error i get is:
'class AccelStepper' has no member named 'Stop'
I noted that the error write stop with a cap S, but if I change stop() to Stop() the error remains the same.
All other such as stepper.run(), etc work fine and the code compile without any problem. I checked online tutorial and my code is very similar. If i cut and paste the tutorial examples i find, if there is stop() in it, it will not compile.
So ???? I really cannot figure this one.
For the other suggestion regarding the loop, i made the changes and all is fine.
Do you perhaps have an outdated copy of AccelStepper? Either way, open the header file and look for a stop method in the declared class. If it's not there, it's not there.
lou123a:
Actually no. As I am learning so i assume that if it is on the AccelStepper website sample code it is something that i can use or at least exist.
We can't offer help against thin air. Show us the example code and show us your code.
This is my code (removed all non-relevant parts)
It will compile if I REM out the stepper.stop(); line
If I use the stop(), I get the following error when i compile:
‘class AcceStepper’ has no member named ‘stop’
I opened the AccelStepper.h and it doesn’t show any stop() member. This would explain why I get the error!
But If I take the tutorial sample from the AccelStepper site, it clearly show the usage of the stop(), but it doesn’t compile on my machine either, and I get the same error.
It looks like I need to have a library with the member stop(), but if the author site doesn’t have it, where should I get it? And if I get a library with it should I reinstall all the Arduino, and library, or it will just copy over the ‘bad’ one?
I guess this is the problem, not the code.
In any case this is the code I have and the sample from teir site
#include <AccelStepper.h>
int motorSpeed = 2500; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 600; //steps/second/second to accelerate
int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3
int x;
int led = 11;
int led1 = 8;
//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin);
void setup(){
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
pinMode(led, OUTPUT); // moke up for flash
}
void loop(){
//if stepper is at desired location
x = stepper.currentPosition();
while (x < 3200) {
stepper.runToNewPosition(x); //move 32000 steps (should be 10 rev);
delay(3000); // Stay there for process to occur
x = x + 800;
}
// Indicate process is done and motor should stop
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
// if the following is used in the code, code will not compile
// stepper.stop(); // <<==== would stop the motor
}
Accelstepper code sample
#include <AccelStepper.h>
AccelStepper stepper;
void setup()
{ stepper.setMaxSpeed(150); stepper.setAcceleration(100);}
void loop()
{
stepper.moveTo(500);
while (stepper.currentPosition() != 300) // Full speed up to 300 stepper.run();
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
// Now stopped after quickstop
// Now go backwards
stepper.moveTo(-500);
while (stepper.currentPosition() != 0) // Full speed basck to 0
stepper.run();
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition(); // Now stopped after quickstop
}
while it's good that you found the newer version of AccelStepper, could you please let me (us) know where you found the newer version? i've been looking around (have the same problem) and have yet to find the one with the 'stop()' and other functions!
rholt:
while it's good that you found the newer version of AccelStepper, could you please let me (us) know where you found the newer version? i've been looking around (have the same problem) and have yet to find the one with the 'stop()' and other functions!
thanks in advance,
Russ from Coral Springs, Fl
I have the same problem, so i need also the new version of this librarie, i just found the older one
I'm new at programming in any library... So I would like someone help me to make a simple movement with two motors steppers.
What the steppers need to do:
Stepper1: move 260 pulses and stop (wait until the second stepper finish its movement);
Stepper 2: move 200 pulses Clockwise; move 200 pulses Counter clockwise;
Repeat this till the end switch enable;
When the EndSwitch is enable, Stepper1 back to inicial position.