-1

I have a problem on how to keep my stepper motor moving when button is released. It should only stop moving if I press the button again.

For now, stepper motor is only moving IF the button is constantly pressed.

//Constants won't change. They're used here to set pin numbers:
const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int dirPin = 8;        // the number of the motor direction pin
const int stepPin = 9;         // the number of the motor step pin

//Variables will change:
boolean flag = true;
int buttonState = 0;         // variable for reading the pushbutton status
int distance = 0;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(dirPin, OUTPUT);  //YELLOW
  pinMode(stepPin, OUTPUT); //ORANGE

  digitalWrite(buttonPin, LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, HIGH);
}

void loop() {
  buttonState = digitalRead(buttonPin); // Read the state of the pushbutton 

  if (digitalRead(buttonPin) == LOW) {  // If pushbutton is pressed, buttonState is HIGH:
    digitalWrite(ledPin, HIGH);
    digitalWrite(dirPin, LOW);          // LOW=FORWARD DIRECTION
    digitalWrite(stepPin, HIGH);        // stepPin = MOTOR ON/OFF
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
    distance++;
    if(distance > 21000){
      distance = 0;
      delay (3000);
      reverse();
    }
  } 

  if (digitalRead(buttonPin) == HIGH){ // turn LED off:
    digitalWrite(ledPin, LOW);
    digitalWrite(dirPin, LOW);  //LOW = FORWARD DIRECTION
    digitalWrite(stepPin, LOW); //stepPin = MOTOR ON/OFF
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
}

void reverse() {
  digitalWrite(dirPin,HIGH); //HIGH = REVERSE DIRECTION
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(100);
  digitalWrite(stepPin,LOW);
  delayMicroseconds(100);
  distance++;
    if(distance > 21000){
      distance = 0;
      delay(3000);
      return;
    }
  reverse();   
}
  • 1.) Please format your code. Left-aligned code is hard to read. It should be indented properly. That will also make it easier for you to spot problems. 2.) Use variables in order to name pins, e.g. pin 8 and 9. You have already done so for pin 4 and 13. Why not for 8 and 9? With names, people don't have to remember what number has what purpose – Thomas Weller Feb 16 '19 at 17:42
  • 1
    3.) if you want two different actions when a button is pressed, then you need to keep records of the current state. So you will need a vaiable like bool isMoving=true and toggle that on each button press. – Thomas Weller Feb 16 '19 at 17:46
1

N.b. your code is unreadable, so I won't attempt to debug your program, but I will present some pseudo-code that could help.

It should only stop moving if I press the button again ... [as of] now, [the] stepper motor is only moving IF the button is constantly pressed.

So what you need to do is save the state of your Motor, (MOVING, STOPPED, etc.) and base your code on that state.

You will also need to de-bounce your switch.

Your loop will have to:

  • Read & de-bounce the switch
  • Update the state based on switch
  • Run the stepper based on state

pseudo-code:

// include libraries etc.
#include <Stepper.h> 

// set up your buttons pins etc.
int button = <pin>;
Stepper = step(...); // pins etc.

void setup()
{
   // do setup stuff...
}

enum MOTORSTATES {
  STOPPED = 1,
  FORWARD = 2,
}

MOTORSTATES state = STOPPED;

// forward declare your functions
bool isPressedDebounce(int pin);
void doStep(Stepper&, MOTORSTATES);
MOTORSTATES changeState(MOTORSTATES);

// loop is now simple
void loop()
{
  if(isPressedDebounce(button))
    state = changeState(state);

  doStep(step, state);
}

// because we have broken our problem into pieces
bool isPressedDebounce(int pin)
{
  // proccess and de-bounce the switch
  // implementation left as exercise for reader
}

void doStep(Stepper& step, MOTORSTATES state)
{
  // based on the state move the stepper
  // implementation left as exercise for reader
  switch(state)
  {
    case STOPPED:
      // DO IT
      break;
    case FORWARD:
      // DO IT
      break;
  }
}

MOTORSTATES changeState(MOTORSTATES state)
{
  // n.b. I used this pattern because it is the beginning of a state machine!
  switch(state)
  {
    case STOPPED:
      return FORWARD;
      break;
    case FORWARD:
      return STOPPED;
      break;
  }
}

I structured the pseudo code such that it contains a simple state machine, and can be easily extended. State machine code is very useful for programming Arduinos!

0

There are two issues:

  1. You SHOULD pull-up or pull-down the input pin when using pushbutton.
  2. You should detect the "press" and "release" event.

Two above issues are mentioned in this tutorial

Your Answer

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

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