#include <Stepper.h>
int stepsPerRev = 2048;
Stepper stepper (stepsPerRev, 11,9,10,8); // ( IN4,IN2,IN3,IN1)
int btn1 = 7;
int btn2 = 6;
void setup() {
stepper.setSpeed(15);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
}
void loop() {
if (digitalRead(btn1)==HIGH){
stepper.step(1);
}
if (digitalRead(btn2)==HIGH){
stepper.step(-1);
}
}
I want to control the stepping motor using two buttons.
If I keep pressing one button, the motor rotates to one side.
If I keep pressing the other button, the motor rotates in the opposite direction.
I want this kind of control.
But The motor rotates little by little even though I didn't press the button.
I want to solve these problems.
Geniuses, please help me.
The Stepper motor is 28BYJ-48 and Driver is ULN2003
If the buttons are connected to the input pin and ground, change the mode to INPUT_PULLUP. If the buttons are connected to the input pin and +5, make sure there is a pull0down resistor between the input pins and ground. What you are seeing is likely the result of a floating pin that is neither a solid high nor a solid low.
Just as the spring mechanically pushes open the momentary button when you remove your finger, and without that spring the button may or may not open or close, you have to electrically hold the pin in one state and look for a state change to act on... so as Picky said, you pull them up or down with a resistor and then the button pulls them the other way
This is a typical push-button circuit that pulls the port pin LOW when the button is pressed. It uses the 10k ohm pullup to keep the state high when the button is open. You can omit the cap and diode. they are optional to protect the input from damage. You can also use INPUT_PULLUP instead of an external pullup resistor. And you can optionally invert the logic using an external pull-down resistor and the button will be tied instead to 5 volts.