Hey guys. I am working on programming an arduino uno with a stepper motor. It will act as a conveyor where it rotates one way for a set distance, wait, than return to its original position. I would like to have the loop hooked up to a button so when the button is pressed the conveyor will go down and back. I have seen a lot of programs where the button has to be held down, but I haven't seen anything that allows you to push the button once and have the conveyor cycle through the loops. Any suggestions? I have included my code below. Thanks!
Stepper motors usually have 4 or 6 wires, and it looks like you are only using 2 pins. Are you using some kind of stepper motor driver?
Also, be careful about plugging your stepper motor wires directly into the arduino. Stepper motors typically need a lot of current to run and your arduino may not be able to provide enough for it to turn
Yes, I am using an ST10-S Step Motor Driver. The while(1) was something quick I found online to use while testing the motor. I would like to be able to get out of the void loop once the conveyor goes down and back I just haven't got that far. I could use some suggestions for that as well if anyone has some.
void loop() {
readButton();
checkMotor();
motorMove();
}
void readButton() {
// code to read the button and save its value
}
void checkMotor() {
if (motorStopped == true) {
if (buttonPressed == true) {
if (motorDir == 'L') {
motorDir = 'R';
}
else {
motorDir = 'L';
}
motorStopped = false;
}
}
}
void motorMove(char dirn) {
if (motorStopped = false) {
motorPos = 0;
if (motorDir == 'R') {
// set direction
}
else {
// set other direction
}
if (timeForNextStep) { // pseudo code to represent checking if a step is due
motorStep(); // function to make the motor move 1 step
motorPos ++;
}
if (motorPos >= motorLimit) {
motorStopped = true;
}
}
The variables motorStopped and motorDir represent the different states of the system