Keep motor running after momentary push button is released

I am very new to Arduino and am stuck with a but of code.

I am looking to have a motor keep rotating after I push a momentary start push button. What I have is a stepper motor with the PUL being controlled by a momentary push button and want it to keep rotating untill a stop button is pushed. I have the code done as far as the motor turns when the push button start is pressed but it is not latched so it turns off when the push button start is released.

Any help is appreciated.

Where is the code that you want help with?

Steve

Look at the StateChangeDetection example in the IDE

Use the 'start' button to set a boolean variable to true. Use the 'stop' button to reset the boolean to false. This is your latch. Use the boolean to control the motor.

1 Like

As it stands when you put power to the arduino, the motor spins.
When I press buttonPin2 it stops until i take my finger off, then it all starts again!
I know its bloody something simple but cant seem to get it right. any help would be appreciated!
Sorry didnt think of code, its as follows;

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
int buttonPin1 = 2; //Start button
int buttonPin2 = 3; //Stop button
int buttonStatus1 = 0;
int buttonStatus2 = 0;
boolean running = false;

void setup() {
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
digitalWrite(buttonPin1, HIGH);
digitalWrite(buttonPin2, HIGH);
}

void loop() {

buttonStatus1 = digitalRead(buttonPin1);
buttonStatus2 = digitalRead(buttonPin2);

if (digitalRead(buttonStatus1)==HIGH && buttonStatus2==HIGH)
{
for (int i=0; i<400; i++) //Forward 5000 steps
running = !running;
digitalWrite(PUL,running);
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
}

if (digitalRead(buttonStatus2)==LOW && buttonStatus1==HIGH){
{
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}

}

}

You never change the state of your boolean.

dougp:
Use the 'start' button to set a boolean variable to true. Use the 'stop' button to reset the boolean to false. This is your latch. Use the boolean to control the motor.

Read this response closer, you never change the boolean state in your code.

  if (digitalRead(buttonStatus1)==HIGH && buttonStatus2==HIGH)

Errr...buttonStatus1 is the result of a read on buttonPin1. Trying to read something from that result instead of from a pin isn't going to do anything useful.

Steve

Managed to get my stop button working with your advice on booleans, thanks! so now the motor runs when the arduino is turned on and stops when buttonStatus2 is pressed.

initial setup i put

boolean running = false;
boolean buttonStatus1;
boolean buttonStatus2;

Then in my main body:

if (buttonStatus2 ==false)running = true;
{
for (int i=0; i<400; i++) //Forward 5000 steps
running = !running;
digitalWrite(PUL,running);
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
}

I thought this would start the motor but infact its done the opposite, haha!
next step is to get the motor to start again when I press the start button (buttonStatus1).

int pulsePin = 7;
int directionPin = 6;
int enablePin = 5;
int startButtonPin = 2;
int stopButtonPin = 3;
boolean startButtonStatus = HIGH;
bool stopButtonStatus = HIGH;
bool runStatus = false;

bool pulseRunning = false;

void setup() {
pinMode (pulsePin, OUTPUT);
pinMode (directionPin, OUTPUT);
pinMode (enablePin, OUTPUT);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
}

void loop() {

startButtonStatus = digitalRead(startButtonPin);
stopButtonStatus = digitalRead(stopButtonPin);

if (startButtonStatus == LOW && stopButtonStatus == HIGH || runStatus == true) {
runStatus = true;
for (int i = 0; i < 400; i++) //Forward 5000 steps
pulseRunning = ! pulseRunning;
digitalWrite(pulsePin, pulseRunning);
digitalWrite(directionPin, LOW);
digitalWrite(enablePin, HIGH);
digitalWrite(pulsePin, HIGH);
delayMicroseconds(50);
}

if (startButtonStatus == HIGH && stopButtonStatus == LOW) {
runStatus = false;
digitalWrite(directionPin, LOW);
digitalWrite(enablePin, HIGH);
digitalWrite(pulsePin, LOW);
delayMicroseconds(50);
}

}

GrOnThOs thank you, sweet relief!

You didnt have to put that code up but I can see where I was going wrong now so thanks, so close yet so bloody far! You’ve saved me banging my head against a wall for a lot longer, haha! I just had to change the HIGH/LOW’s on buttonStatus1/2 to match the original state of the buttons I was using and hey presto stop start turning motor!

ukHeliBob, dougP, jbarth and slipstick thank you, I know i’m a noob so your patience with me was great. Also really appreciated that you didnt just fix it for me at the begininng, I had to go back and restudy some basics and its helped immensly!

Thanks!

It’s always super easy when you use separate push button for start stop. Τhe real challenge it’s when you have to use same push button for both acts (start stop)
you will need push button debounce solution and good state change code.