Help with 2 x Push Button Stepper Motor Operation

Hi - let me start by saying that I am a complete novice and besides basic LED and Servo controlling I have little to no knowledge about coding. I have spent the last 2 days trying to figure out what I know is basic code, but it doesn't seem to work.

I am making something that requires me to use a stepper motor (28BYJ-48). I want to be able to push one button and have the stepper motor turn X degrees CW and I want to push the other button and have it go the same degree but CCW. When nothing is pressed the motor does not move.

Currently i can get one button to work in a CW motion, but cannot get my second button working for CCW.

I am using a Mega2560 r3 at the moment but would like to use the code on a Nano also. Can anybody help me out? I'd appreciate it so much.... and you'd be saving me from losing anymore of my hair!!!

I would also like to know if anyone has any tips or best ways of learning ardunio?

#include <Stepper.h>

int buttonPressPin = 7;
int buttonPressPin2 = 6;

bool buttonPressed;
bool buttonTwoPressed;

const int stepsPerRevolution = 2048;

Stepper stepperName = Stepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {

  stepperName.setSpeed(10);
  
  pinMode(buttonPressPin, INPUT_PULLUP);
  pinMode(buttonPressPin2, INPUT_PULLUP);

  buttonPressed = true;
  buttonTwoPressed = true;


}

void loop() {

    buttonPressed = digitalRead(buttonPressPin);
    buttonTwoPressed = digitalRead(buttonPressPin2);

    
    if (buttonPressed == false) {
      stepperName.step(stepsPerRevolution / 16);
      delay(50);
      buttonPressed = true;}


    if (buttonTwoPressed == false) {
    stepperName.step(stepsPerRevolution / 16);
    delay(50);
    buttonPressed = true;}

}



Hello

Take a view into the example provided by the IDE for:

  1. state change detection
  2. BlinkWithOutDelay as base for timer functions
  3. something that I´ve forgoten

Take a peace of paper, a pencil and design based on the IPO model a program structure plan.
Identify functions and their depencies. Now start with coding and testing, function by function.
At the end merge all tested function together to get the final sketch.

Have a nice day and enjoy coding in C++.

Maybe a copy/paste problem?
The second if block does exactly the same as the first one. So both buttons let the motor move CW.

It doesn't make sense to set buttonPressed to true in the if block. It's directly after that set to the state of input pin again.

Thank you for for reply @paulpaulson, I will check that out.

Thanks @MicroBahner. So the button connected to pin 7 works and moves the motor CW, however the button connected to pin 6 does absolutely nothing.

Sorry - correction both buttons do perform the same function. Apologies

no, your doing a good job, keep it up..
here's my contrib..

#include <Stepper.h>

int buttonPressPin = 7;
int buttonPressPin2 = 6;

bool buttonPressed = false;
bool buttonTwoPressed = false;

const int stepsPerRevolution = 2048;

Stepper stepperName = Stepper(stepsPerRevolution, 8, 9, 10, 11);

unsigned long lastSample = 0;
int intervalSample = 100;


void setup() {

  stepperName.setSpeed(10);
  
  pinMode(buttonPressPin, INPUT_PULLUP);
  pinMode(buttonPressPin2, INPUT_PULLUP);



}

void loop() {

  //debounce the buttons a bit
  if (millis()-lastSample>= intervalSample)
  {  //update the timer var
    lastSample = millis();
    // ! reversing the reads so bool logic read correctly..
    buttonPressed = !digitalRead(buttonPressPin);
    buttonTwoPressed = !digitalRead(buttonPressPin2);

    
    if (buttonPressed) {
      stepperName.step(stepsPerRevolution / 16);
      delay(50);
      buttonPressed = false;}


    if (buttonTwoPressed) {
    stepperName.step(stepsPerRevolution / 16);
    delay(50);
    buttonPressed = false;}
  }
}
1 Like

Of course, because you tell them to do the same.

Change

to

    if (buttonTwoPressed == false) {
    stepperName.step(-stepsPerRevolution / 16); // must be negative steps to move CCW

Hi @qubits-us and thanks,

It's hard getting my head round this stuff... I'd love to do some sort of course. I know a lot of it comes down to hard work and persistence though.

I have actually just managed to solve the issue myself though!!! I am currently running around the room screaming.

I had previously tried to use "-stepsPerRevolution" but to not avail, but I've ended up changing it back and swapping the pole pins around on the arduino... and it's working brilliantly.

Thank you so much everyone. I'm a bloody idiot!!!

Final code here for future reference.

#include <Stepper.h>

int buttonPressPin = 7;
int buttonPressPin2 = 6;

bool buttonPressed;
bool buttonTwoPressed;

const int stepsPerRevolution = 2048;

Stepper stepperName = Stepper(stepsPerRevolution, 10, 11, 12, 13);

void setup() {

  stepperName.setSpeed(10);
  
  pinMode(buttonPressPin, INPUT_PULLUP);
  pinMode(buttonPressPin2, INPUT_PULLUP);

  buttonPressed = true;
  buttonTwoPressed = true;


}

void loop() {

    buttonPressed = digitalRead(buttonPressPin);
    buttonTwoPressed = digitalRead(buttonPressPin2);

    
    if (buttonPressed == false) {
      stepperName.step(stepsPerRevolution / 16);
      delay(50);
      buttonPressed = true;}


    if (buttonTwoPressed == false) {
    stepperName.step(-stepsPerRevolution / 16);
    delay(50);
    buttonPressed = true;}

}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.