Currently i've working code of controlling stepper motor with pushbuttons. So, basically i have to press two push button to run forward and backward. but the problem is , when i press 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 counter clock wise.

i realize that the program isn't detecting second push button after pressing 1st one. Tried different options to make it work. but failed.

any suggestions?

heres 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);  

void loop()

      int sensorReading = analogRead(A0);
      int motorSpeed = map(sensorReading, 0, 1023, 500, 1000);
      button1State = digitalRead(button1Pin);
      button2State = digitalRead(button2Pin);

     if (button1State == HIGH)

  if (button2State == HIGH)



Another problem is in this current code , it cannot detect the potentiometer input inside the if ( buttonstate ) {} loop.

it would be a great help pointing out my mistakes.

  • Hi can't it be done simply by coding? – Ihsan Ahmad Siddiquee Aug 11 at 7:09
  • What is your debug output saying? It's probably printing a lot. How often does it change between ok and ok2? Add a "stop" state (with Serial debug output), forget about the motor until you see the expected output lines properly. What should happen if both buttons are pressed? Should that kill the motor? – DataFiddler Aug 11 at 10:30
  • ` That means it cannot detect the press of another pushbutton to make it counter clock wise` Why is that? How did you connect the buttons. | Also, I would set the max speed in the setup function since you only have to do this once. – Swedgin Aug 12 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.