I want to create an Arduino program for a system that has three buttons and one stepper motor. If button 1 is pressed, the stepper should go for example 50 steps forward. If button 2 is pressed, the stepper should go 50 steps backward. If button 3 is pressed, the stepper should go 50 steps forward after that 50 steps backward.
I used Arduino's stepper library and wrote the following code. The functions Forward()
, Backward()
and Continuous()
implement the actions to be performed for each button. Each function moves the motor step by step and logs the action on a serial output.
But I could not achieve my desired results: The stepper does not go backward but only goes forward. More precisely:
Forward()
works as expectedBackward()
produces the expected logging output (counting the steps up to 50), but the motor only moves forward instead of backward.Continuous()
function is not working either: after 50 step forward (moving and logging the step count), it continues moving forward logging just 1 for the counter.
I need your help. How can I make the motor going backwards in Backward()
? And how to correct Continuous()
to achieve move forward and backward, and producing the right backward step count?
Here my code:
#include <Stepper.h>
int forward_button = 2;
int backward_button = 3;
int cont_button = 4;
int button_cond1;
int button_cond2;
int button_cond3;
int del = 50;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
int steps;
void Forward() { // should go forward by 50 steps
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Forward steps :");
Serial.println(stepCount);
}
}
void Backward() { // should go backward by 50 steps
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Backward steps :");
Serial.println(stepCount);
}
}
void Continuous() { // should go forward by 50 steps, then backwards
int stepCount = 0;
while (stepCount < 50) {
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Continuous steps :");
Serial.println(stepCount);
}
while (50 < stepCount <= 200) {
int stepCount = 0;
steps = 1;
myStepper.step(steps);
stepCount++;
delay(del);
Serial.print("Continuous steps :");
Serial.println(stepCount);
}
}
void setup() {
// initialize the serial port:
Serial.begin(9600);
pinMode(forward_button, INPUT_PULLUP);
pinMode(backward_button, INPUT_PULLUP);
pinMode(cont_button, INPUT_PULLUP);
//myStepper.setSpeed(60);
}
void loop() {
// step one step:
button_cond1 = digitalRead(forward_button);
button_cond2 = digitalRead(backward_button);
button_cond3 = digitalRead(cont_button);
if ((button_cond1 == LOW) && (button_cond2 == HIGH) && (button_cond3 == HIGH)) {
Forward();
}
else if ((button_cond1 == HIGH) && (button_cond2 == LOW) && (button_cond3 == HIGH)) {
Backward();
}
else if ((button_cond1 == HIGH) && (button_cond2 == HIGH) && (button_cond3 == LOW)) {
Continuous();
}
}