I tried this but it moves back to zero position. "-" doesn't have any effect.
add a
stepper.run();
in loop
Yes. I added. It's now running. It's great help to learn "Case".
Thanks a lot.
you can mark ONE post as solution.
you can say thank you to several helpers by pressing the heart/like under their post.
Thanks to all for your guidance
This addition is useful. When you leave IDLE for LEARN, reset the distance counter.
state = LEARN;
distance = 0; //... reset distance for new recording
Serial.println(F("LEARN"));
I had to wire the stepper differently but the code works in the simulator.
It does seem to play back with some loss of fidelity, I cannot look into that at this time.
I'll find out first about the blocking or not, but recall @noiasca saying
I hope .move() is blocking.
but then the stepper didn't until a loop attention method was called. So there may be fruitful avenues of inquiry there if you find the steps taken unequal.
Also, I added the idea of using the STOP button to make the stepper go back to zero.
That worked, but the current button handling means that at the end of a recording, you would immediately get taken back to zero. If you are on the button for a human scale little moment.
@pankajsharma200 is this to be part of something you would like to say anything about?
a7
just as an Idea:
replace the slow down delay with millis()-previousMillis implementation - than you will gain several .run() calls for each single +1 move.
Yes, It is part of one project and new addition.
I am still struggling. I tried to reach solution by tweaking code, but it is still not running as required. Below are the issues.
-
When startPin is LOW, it starts counting steps

-
In mid, if startPin becomes HIGH it should stop counting steps.
-
If again, startPin is LOW, it should start counting from last distance or it should start adding in last count.
-
Once stopPin is pressed, it should freeze distance, which is presently good

-
When replayPin is pressed, motor should run freeze steps and then stop permanently.
if again replayPin is pressed motor should run freeze steps and then stop and continue to do so until and unless new distance is calibrated.
- Presently, motor continue to run indefinitely if replayPin is pressed while it should stop after running freeze steps.
below is code after tweaking . I haven't inserted parts which i tried but didn't work.
#include <AccelStepper.h>
#define HALFSTEP 8
#define motorPin1 8 // IN1 on ULN2003 ==> Blue on 28BYJ-48
#define motorPin2 9 // IN2 on ULN2003 ==> Pink on 28BYJ-48
#define motorPin3 10 // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4 11 // IN4 on ULN2003 ==> Orange on 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
int distance = 0;
const uint8_t startPin = 5;
const uint8_t stopPin = 6;
const uint8_t replayPin = 7;
enum State {IDLE, LEARN, REPLAY} state;
void initStepper() {
stepper.setMaxSpeed(1000);
stepper.setSpeed(100);
stepper.setAcceleration(1000); // Set Acceleration
}
void runFSM() {
switch (state) {
case IDLE :
if (digitalRead(startPin) == LOW) {
state = LEARN;
distance = 0; //... reset distance for new recording
Serial.println(F("LEARN"));
}
if (digitalRead(replayPin) == LOW) {
state = REPLAY;
Serial.println(F("REPLAY"));
}
break;
case LEARN:
{
if (digitalRead(stopPin) == LOW) {
state = IDLE;
Serial.println(F("IDLE"));
}
stepper.move(1);
stepper.run(); // triggers the stepper if necessary
distance++;
Serial.println(distance);
}
delay(5); // just a dirty delay to slow down the movement
break;
case REPLAY:
Serial.print(F("steps to go ")); Serial.println(distance);
stepper.move(distance);
stepper.run(); // triggers the stepper if necessary
state = IDLE;
break;
}
}
void setup() {
Serial.begin(115200);
pinMode(startPin, INPUT_PULLUP); // Button to initiate steps to move
pinMode(stopPin, INPUT_PULLUP); // Button to freeze steps to be moved by stepper
pinMode(replayPin, INPUT_PULLUP); // Button to Start the motor to run
initStepper();
}
void loop() {
runFSM();
}
dear @pankajsharma200
you should describe the observed behaviour and the wanted behaviour with more words and very very precise words.
"in mid" is a very unprecise description.
same thing with
what exactly is "last distance"?
What do you mean by "freeze steps" ?? There is no variable freeze steps in your code!
Permamently in the sense of never ever again makes no sense.
So your permanently is something different and there is a some kind of condition and if this condition becomes true I guess your stepper-motor shall rotate again.
What is this condition?
Additionally give an overview about your project
If you describe your real application the users here will get a much better understanding of what you want to do.
If your english is limited I recommend that you use google-translate.
Write down a very precise and very detailed description. Then use google translate for translation. The grammar will not be brilliant but the bigger amount of words will still be
much better understandable than your short postings above
paragraph google translated to hindi
рдпрд╣ рдПрдХ рдкреНрд░рджрд░реНрд╢рди рд╣реИ рдХрд┐ Google-рдЕрдиреБрд╡рд╛рдж рдХрд┐рддрдирд╛ рдЕрдЪреНрдЫрд╛ рдХрд╛рдо рдХрд░рддрд╛ рд╣реИред
рдпрд╣ рд╣рд┐рдВрджреА рдЕрдиреБрд╡рд╛рдж рдЧреВрдЧрд▓-рдЯреНрд░рд╛рдВрд╕рд▓реЗрдЯ рджреНрд╡рд╛рд░рд╛ рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИред
рдЬреИрд╕рд╛ рдХрд┐ рдЖрдк рджреЗрдЦ рд╕рдХрддреЗ рд╣реИрдВ, рдкрд╛рда рд╕рдордЭрдиреЗ рдореЗрдВ рдЕрдЪреНрдЫрд╛ рд╣реИред
рдпрджрд┐ рдЖрдк рдЕрдкрдиреЗ рд╡рд╛рд╕реНрддрд╡рд┐рдХ рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдХрд╛ рд╡рд░реНрдгрди рдХрд░рддреЗ рд╣реИрдВ рддреЛ рдпрд╣рд╛рдВ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЛ рдмреЗрд╣рддрд░ рд╕рдордЭ рдорд┐рд▓реЗрдЧреА рдХрд┐ рдЖрдк рдХреНрдпрд╛ рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВред
рдпрджрд┐ рдЖрдкрдХреА рдЕрдВрдЧреНрд░реЗрдЬреА рд╕реАрдорд┐рдд рд╣реИ рддреЛ рдореЗрд░рд╛ рд╕реБрдЭрд╛рд╡ рд╣реИ рдХрд┐ рдЖрдк рдЧреВрдЧрд▓-рдЯреНрд░рд╛рдВрд╕рд▓реЗрдЯ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░реЗрдВред
рдмрд╣реБрдд рд╕рдЯреАрдХ рдФрд░ рдмрд╣реБрдд рд╡рд┐рд╕реНрддреГрдд рд╡рд┐рд╡рд░рдг рд▓рд┐рдЦреЗрдВ. рдлрд┐рд░ рдЕрдиреБрд╡рд╛рдж рдХреЗ рд▓рд┐рдП Google Translate рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░реЗрдВред рд╡реНрдпрд╛рдХрд░рдг рд╢рд╛рдирджрд╛рд░ рдирд╣реАрдВ рд╣реЛрдЧрд╛ рд▓реЗрдХрд┐рди рд╢рдмреНрджреЛрдВ рдХреА рдмрдбрд╝реА рдорд╛рддреНрд░рд╛ рдлрд┐рд░ рднреА рд╣реЛрдЧреА
рдЙрдкрд░реЛрдХреНрдд рдЖрдкрдХреА рд╕рдВрдХреНрд╖рд┐рдкреНрдд рдкреЛрд╕реНрдЯрд┐рдВрдЧ рдХреА рддреБрд▓рдирд╛ рдореЗрдВ рдпрд╣ рдХрд╛рдлреА рдмреЗрд╣рддрд░ рдврдВрдЧ рд╕реЗ рд╕рдордЭ рдореЗрдВ рдЖрддрд╛ рд╣реИ
yah ek pradarshan hai ki googlai-anuvaad kitana achchha kaam karata hai.
yah hindee anuvaad googal-traansalet dvaara kiya gaya hai.
jaisa ki aap dekh sakate hain, paath samajhane mein achchha hai.
yadi aap apane vaastavik eplikeshan ka varnan karate hain to yahaan upayogakartaon ko behatar samajh milegee ki aap kya karana chaahate hain.
yadi aapakee angrejee seemit hai to mera sujhaav hai ki aap googal-traansalet ka upayog karen.
bahut sateek aur bahut vistrt vivaran likhen. phir anuvaad ke lie googlai translatai ka upayog karen. vyaakaran shaanadaar nahin hoga lekin shabdon kee badee maatra phir bhee hogee
uparokt aapakee sankshipt posting kee tulana mein yah kaaphee behatar dhang se samajh mein aata hai
best regards Stefan
You should post the modificated code as a code-section too.
Please find full description of project.
Here, I am trying to calibrate X-axis carriage's travel distance after Z-axis carriage presses either of the two Z- axis limit switches.
Once the predetermined distance is calibrated, the X-axis carriage will travel the same distance whenever a limit switch is pressed.
Pre-defined distance calibration: With the startpin and stop pin, I want to pre-define, how many steps or distance my stepper motor will move after each Z-axis stroke is completed or limit switch is pressed .
Initial Set-up: When the startpin is pressed, it should start counting the number of steps / distance the motor has taken / travelled, and when the stoppin is pressed, it should stop counting / running.
Since the operator will be calibrating the distance moved after the physical check, the motor should stop running as soon as the start pin is released/unpressed.
To further calibrate the distance, the operator may wish to add a few more steps. It should be able to add further steps by pressing startpin. Once the stoppin is pressed, the steps taken or distance covered will be available for further activity.
Continous Run: And now, if either of the limit switches is pressed then the X-axis motor/carriage should move and stop after the number of steps/distance is completed. Again, if either of the two limit switches is pressed, it should travel the same distance otherwise it should remain stationary.
Now what is working and not working
This is working. ![]()
This is not working.
This is not working.
This is working.
This is not working. Motor is continuously running.
OK. I re-phrase in my words to make sure that I understand it right:
Only as long as the start-pin is pressed down the stepper-motor should rotate step by step.
For each step the variable distance is incremented by one.
As soon as the start-pin is released the motor shall stop
If the user is pressing the start-pin again the stepper-motor shall rotate again.
and again for each step the variable distance is incremented by one.
Example:
initial pressing down start-pin =>
dictance counts up 1,2,3,........250
start-pin is released => motor stops
user presses down start-pin distance continues to count up
250,251,252,....257
start-pin is released => motor stops variable distance holds value 257
user presses stop-pin the number of steps stored in variable distance (257 steps) is used
for further action.
Here I recommend to re-name the buttons:
what your "start-pin" is really doing is moving
what your stop-pin is really doing is store actual amount of steps.
The names of everything in your code should describe spot-on what its purpose is / what this part of the code is really doing.
In future postings avoid using duplicate deviating descriptions.
Example:
make it unique in the meaning by writing
as long as the startpin is pressed, the motor should move, for each step the motor has done, increment number of steps stored in variable distance.
Leave away this part: distance the motor has taken / travelled,
It is confusing instead of improving clarity.
There is something new
I try to write it in my words
The above described calibration is for the X-axes
There are two limit-switches mounted on the X-axis
One limitswitch on the left end, one limit-switch on the right end
If one of the limit-switches is pressed the X-axis-motor shall move
the number of steps stored into variable distance.
It might well be that my understand is partially or completely wrong.
You will have to confirm or correct the description.
As a general hint. Use the exact names for everything
examples
"not writing "it"
what "it"?
write as many words as are nescessary to describe it specifically enough that any mis-interpretation can be excluded.
the limit-switch on the left end
Never use general words like "it", "them", "there" etc.
You can invest one time 15 minutes in such a posting to make the description precise
or
investing 150 minutes of time
for a quick posting wating 30 minutes for an answer
that just asks back
for a quick posting wating 30 minutes for an answer
that just asks back
... repeat
best regards Stefan
Correct. Your understanding is correct.
Agreed
You understood correctly.
I agree with you.
There is one limit-switch on the left end and a second limit-switch on the right end.
This means you can simply use the limit-switches for your calibration-process.
Move stepper-motor until limit-switch is pressed down.
Set variable distance to zero.
invert moving-direction and move until other limit-switch is pressed down.
You have written that the stepper-motor shall always rotate into the same direction.
How does rotating into the same direction fit to the fact that you have limit-switches on the left/right end of your axis ???
You did either understand something wrong or my description is not correct in all details.
As you can see now:
quick and short postings slow things down.
How about posting a picture of the mechanic of your device?
Limit switches are on Z-axis while we are counting steps to move stepper on X-axis. Once either of limit switch is pressed by Z-axis carriage, X-axis stepper motor will run the stored steps.
Let me try to prepare rough pic.
Paint Assembly.pdf (1.6 MB)
Attaching a Y and Z axis carriage which will be installed on crawler having X-axis movement. This assembly will be having limit switches on both side on Z-axis.
Y-axis carriage is having independent movment while X and Z axis are having dependent movement. Whenever, either of limit switches installed on Z-axis assembly is pressed, X-axis motor shall run pre-stored steps.
too quick I don't understand.
If you like to waste your time by playing a ping-pong game of
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
quick postings quick asking back
go ahead
You can post pictures by simply pasting the picture
into the edit-field of a posting
StefanL,
I don't have full assemly picture. This equipment is under development.
I defined axis on two pictures. First picture is attachment to second picture. Second picture is having only X-axis movement. Attacment will installed on arm.
