need help controlling stepper with push button

hi, i’m trying to move a stepper an 1/8 of a turn when i press the button and stay at that spot till i release the button then go back an 1/8 of a turn and stay there till i press it again. this is what i’ve managed to piece together so far. i starting with dronebot workshops code and messed with it till the motor was going back and forth an 1/8 of a turn:

/*
Stepper Motor Demonstration 1
Stepper-Demo1.ino
Demonstrates 28BYJ-48 Unipolar Stepper with ULN2003 Driver
Uses Arduino Stepper Library

DroneBot Workshop 2018

*/

//Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants
const int buttonPin = 7; // the number of the pushbutton pin

// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;

// Amount of Gear Reduction
const float GEAR_RED = 64;

// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;

// Define Variables
int buttonState = 0; // variable for reading the pushbutton status

// Number of Steps Required
int StepsRequired;

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing

Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

void setup() {
pinMode(buttonPin, INPUT);
}

void loop()
{

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
StepsRequired = STEPS_PER_OUT_REV / 8;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);

} else {

StepsRequired = - STEPS_PER_OUT_REV / 8;
steppermotor.setSpeed(1000);
steppermotor.step(StepsRequired);

}
}

now after adding the button stuff, the motor moves ccw continuously then i press the button it goes cw continuously till i release it, then i goes ccw again. which was a victory for sure, after hours of it not responding at all. can anyone tell me whats wrong, or point me in the direction of more info on using a button this way. any help at all would be killer. thank you for taking the time.

It’s much easier for people to help you if you use the code button </> for posting code. See How to use the Forum

/*
    Stepper Motor Demonstration 1
    Stepper-Demo1.ino
    Demonstrates 28BYJ-48 Unipolar Stepper with ULN2003 Driver
    Uses Arduino Stepper Library

    DroneBot Workshop 2018
    https://dronebotworkshop.com
*/

//Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants
const int buttonPin = 7;     // the number of the pushbutton pin

// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;

//  Amount of Gear Reduction
const float GEAR_RED = 64;

// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;

// Define Variables
int buttonState = 0;         // variable for reading the pushbutton status

// Number of Steps Required
int StepsRequired;

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing

Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

void setup() {
    pinMode(buttonPin, INPUT);
}

void loop()
{
    
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
        StepsRequired  =  STEPS_PER_OUT_REV / 8;
        steppermotor.setSpeed(1000);
        steppermotor.step(StepsRequired);
        
    } else {
        
        StepsRequired  =  - STEPS_PER_OUT_REV / 8;
        steppermotor.setSpeed(1000);
        steppermotor.step(StepsRequired);
        
    }
}

I think you have overlooked the fact that loop() repeats over and over so that the code for the button being HIGH keeps repeating.

Rather than checking if the button is HIGH you need to check if it has changed from LOW to HIGH. And the same thing for the transition from HIGH to LOW for the other direction. Something like

prevButtonState  = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH and prevButtonState == LOW) {
   // code for forward
}

else if (buttonState == LOW and prevButtonState == HIGH) {
  // code for reverse
}

…R

1 Like

that worked thank you so much for all your help. will make sure to read how to use the forum before i post again.thank you , thank you, thank you