Hi! I've made a two wheeled robot with two Stepper Motors. I want to make the Stepper Motors move a set distance when a button is pushed. I've been struggling with the coding, can someone help me??
Post what you have done so far and we might be able to spot where you are going wrong.
Basically is is just a matter of sending the right number of pulses, but not so fast that the motor skips a pulse.
Well, I couldn't do a lot, I just did some simple coding for moving one Stepper motor at a time, I'm very new at Arduino. I don't know which code I have to use to make a Stepper motor go a set distance. Do you know how to do that?
I've been experimenting with one motor and the only thing I'm able to do now, is that one motor moves when I keep the button pressed. I don't know how to keep the motor rotating after pressing the button once.
Distance depends on the geometry of your robot.
π is heavily involved.
Yes I told you just send it a fixed number of pulses.
You count them in a variable and stop sending them when you reach your number.
So again please post what you have tried.
The only thing that works is this:
#include <Stepper.h>
int Distance = 0;
const int stepsPerRevolution = 512;
Stepper myStepper1(stepsPerRevolution, 10, 12, 11, 13);
int Switch = 2;
void setup() {
pinMode(Switch, INPUT_PULLUP);
myStepper1.setSpeed(60);
}
void loop() {
if(digitalRead(Switch) == LOW)
{myStepper1.step(1);
}
}
There are a couple of things you need to decide. Do you want it to move WHILE the button remains pressed or move IF the button is pressed? This will affect how you process the button. Currently it it only moves WHILE you have the button pressed.
I want it to move IF the button is pressed. I've been struggling trying to do that, but I couldn't found the way yet
In that case you need to look for a change in the input from HIGH to LOW. Here is a tutorial on state change detection: StateChangeDetection
If the switch is closed or ’once’ when the switch is first closed ?
Have a look here, This guy has done loads of stepper motor stuff and explains it in detail step by step through the code.
It will give you some really good stuff, He now uses an STM32 but can easily be ported to arduino
You could count the steps on how far it has took to move the distance and then by learning his code you could adopt it to suit your own. But you need to take the time to learn it, I did and have created an auto leveling plate
https://www.youtube.com/watch?v=6zqq79mXjFw&list=PLaeIi4Gbl1T8zzA9jt4VeGGsA6XmIMLWz&index=13
Thanks for posting, now we can see lots of detail you missed out. Like the number of steps you use for a complete rotation, and that you use the Stepper.h library and you are using a unipolar motor. So before you delve into complex stuff try this. Add this new function and change your loop function.
void turns(int numberOfRotations){
for(int i = 0; i< numberOfRotations; i++){
for(int steps = 0; i< 500; steps++) { // do one rotation
myStepper1.step(1); // do one step
}
}
void loop() {
if(digitalRead(Switch) == LOW) // you will have your finger off the button by the time the loop goes round again
turns(10); // make 10 rotations
}
Thank you very much! it works fine, now the motor rotates if I press the button, but now once i press it it doesn't stop
So you have made a change, what was it? Maybe you misunderstood something?
In cases like this you need to post again your whole code in a new post (don't modify anything) then we stand a chance of helping you.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.