Hi all,
My first post, so please be gentle.
I'm experimenting with a stepper motor and a couple of buttons. I've found a library here unistep that I like and is simple to use with the motor I'm using. What I have is two buttons that control the motor. If I press button1 ,the motor rotates so many steps (4096 in my example code) one way. If I press button2, the motor rotates the same amount of steps in the opposite direction.
Using this code, if I press button1 and the motor starts turning, I have to wait for it to complete it's command (turn 4096 steps) before a press of button2 will run it's command. How can I override that first command with a second one (stop the loop)? Basically I want to simulate a limit switch.
From my research, I think I need to use the "break" function inside of a "while" loop. My coding abilities are infantile and it seems the more I search for answers, the more confused I get. Thanks
Using a Nano with buttons attached to pins 2 and 3. The buttons are wired to 5v/grd with 10k resistors going to ground (pull down?). I'm using a 28BYJ-48 motor w/driver board attached to pins 8-11 on the Nano.
I haven't played much with the stepper library yet, but it seems to me that if you need to have a limit switch, you have to move the stepper incrementally (one or a few steps with each call to moves()). Like:
I'd move to the AccelStepper library - that allows fully asynchronous motor control.
You make sure loop() runs often and put a call to stepper.run() in it - the AccelStepper
library does its stuff in that run() call as required (you don't wait for it, you can check
for other stuff)
You can call moveTo() to change the destination whenever you want and see how far it
is from the current destination using the distanceToGo() call.
Find AccelStepper library, install it and checkout the examples.
Thanks for your replies, guys.
I'm not sure how to reply to an individual reply, so I will address each in order:
aarg: "A comment like that is a sign that you should transfer the information into a symbolic constant..."
Actually, I put that comment in there so you guys would know what that number represents. It's part of the library's stepper.moves function (0 = cw, 1 = ccw).
MarkT: "I'd move to the AccelStepper library..."
I have downloaded this library already and tried it out. It did work with my motor but seemed a bit advanced for me so I opted for the unistep. I will revisit it.
econjack: "Take a look at Nick's introduction to interrupts, too"
Thanks for the link. The interrupt is what I'm looking for, I think. Now I just have to figure out how to implement it with my code.