Currently, I've worked on code for controlling stepper motor with pushbuttons. So, basically, I have to press two push button to run forward and backwards. but the problem is when I press the 1st pushbutton it detects that pushbutton and run motor CW or CCW, That means it cannot detect the press of another pushbutton to make it counterclockwise.
I realize that the program isn't detecting the second push button after pressing 1st one. Tried different options to make it work. but failed.
any suggestions?
Here's my code for reference:-
#include <AccelStepper.h>
const int button1Pin = A2 ;
const int button2Pin = A1;
int button1State = 0;
int button2State = 0;
boolean flag1 = 0;
boolean flag2 = 0;
AccelStepper stepper = AccelStepper(1, 8, 9);
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
Serial.begin(9600);
}
void loop()
{
int sensorReading = analogRead(A0);
int motorSpeed = map(sensorReading, 0, 1023, 500, 1000);
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
stepper.setMaxSpeed(2000);
stepper.setSpeed(motorSpeed);
if (button1State == HIGH)
{
stepper.setSpeed(motorSpeed);
Serial.print("ok");
stepper.runSpeed();
}
if (button2State == HIGH)
{
Serial.print("ok2");
stepper.setSpeed(-motorSpeed);
stepper.runSpeed();
}
}
Another problem is in this current code, it cannot detect the potentiometer input inside the if ( button state ) {} loop.
it would be a great help pointing out my mistakes.
setup
function since you only have to do this once.