1

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.

5
  • 1
    See Arduino - button does not work Aug 11, 2020 at 6:24
  • 1
    Hi can't it be done simply by coding? Aug 11, 2020 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? Aug 11, 2020 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, 2020 at 8:55
  • this looks great, is there anything else i need to do to add a stepper, im struggling a bit, but with some friendly makers helping i know ill get this sorted. as im new to this site, G'Day FROM AUSTRALIA!!!!!!
    – Big Al 62
    Mar 9, 2021 at 10:05

1 Answer 1

1

I implemented your code here on a UNO using 2 pushbuttons (with the stepper motor functions commented out) and it seems to work fine. When I press button1 I get a lot of "ok" printouts for as long as the button is held down. When I press button2, the same thing happens except with "ok2" instead of "ok".

Here's the code I used:

/*
    Name:       TwoPushButtonTest.ino
    Created:    9/28/2020 2:36:07 PM
    Author:     FRANKNEWXPS15\Frank
*/



//#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(115200);
}

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)
  {

    Serial.print("ok");
    //stepper.setSpeed(motorSpeed);
    //stepper.runSpeed();


  }

  if (button2State == HIGH)
  {

    Serial.print("ok2");
    //stepper.setSpeed(-motorSpeed);
    //stepper.runSpeed();

  }
}

and here's the wiring diagram:

enter image description here

Note the two 10K pull-down resistors. If these are not present, then the A2/A3 inputs are indeterminate whenever the buttons are in the 'disconnected' state, and anything can happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.