Adding a stop button for the loop

hello guys, I have this sketch below for my actuator and everything runs exactly how I wanted. But I am trying to add a button for emergency stop. I know that I would need to use 'break' to exit out the loop, but I would need to change my code into switch case. Is there any way I can make a switch case from what I have created?

int button_start = 2;
int button_stop = 3;
int limitswitch1 = 4;
int limitswitch2 = 5;
int limitswitch3 = 6;
int A_BACKWARD = 8;
int A_FORWARD = 7;
int B_BACKWARD = 10;
int B_FORWARD = 9;
 
void setup() {
  pinMode(button_start, INPUT_PULLUP);
  pinMode(button_stop, INPUT_PULLUP);
  pinMode(limitswitch1, INPUT_PULLUP);
  pinMode(limitswitch2, INPUT_PULLUP);
  pinMode(limitswitch3, INPUT_PULLUP);
  pinMode(A_BACKWARD, OUTPUT);
  pinMode(A_FORWARD, OUTPUT); 
  pinMode(B_BACKWARD, OUTPUT);
  pinMode(B_FORWARD, OUTPUT);
}

void loop() {
  if(digitalRead(button_start)==LOW && digitalRead(limitswitch1)==HIGH){
    digitalWrite(A_BACKWARD, HIGH);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if(digitalRead(limitswitch1)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, HIGH);
  }
  if(digitalRead(limitswitch2)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, HIGH);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if(digitalRead(limitswitch3)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, HIGH);
    digitalWrite(B_FORWARD, LOW);
  }
}

An emergency stop really ought to be cutting the power more often than not. But you can implement a start stop mechanism like this.

void doStuff() {
	if(digitalRead(limitswitch1)==HIGH){
		digitalWrite(A_BACKWARD, HIGH);
		digitalWrite(A_FORWARD, LOW);
		digitalWrite(B_BACKWARD, LOW);
		digitalWrite(B_FORWARD, LOW);
	}
	if(digitalRead(limitswitch1)==LOW){
		digitalWrite(A_BACKWARD, LOW);
		digitalWrite(A_FORWARD, LOW);
		digitalWrite(B_BACKWARD, LOW);
		digitalWrite(B_FORWARD, HIGH);
	}
	if(digitalRead(limitswitch2)==LOW){
		digitalWrite(A_BACKWARD, LOW);
		digitalWrite(A_FORWARD, HIGH);
		digitalWrite(B_BACKWARD, LOW);
		digitalWrite(B_FORWARD, LOW);
	}
	if(digitalRead(limitswitch3)==LOW){
		digitalWrite(A_BACKWARD, LOW);
		digitalWrite(A_FORWARD, LOW);
		digitalWrite(B_BACKWARD, HIGH);
		digitalWrite(B_FORWARD, LOW);
	}
}

void stop() {
	digitalWrite(A_BACKWARD, LOW);
	digitalWrite(A_FORWARD, LOW);
	digitalWrite(B_BACKWARD, LOW);
	digitalWrite(B_FORWARD, LOW);
}

void loop() {

	static bool isRunning = false;

	// TODO implement startPressed()
	if (startPressed()) {
		isRunning = true;
	}

	// TODO implement stopPressed()
	if (stopPressed()) {
		isRunning = false;
	}

	if (isRunning){
		doStuff();
	} else {
		stop();
	}
}

Edit:
Didn't see the "digitalRead(button_start)==HIGH" in the if statements

Edit 2: There was another one

1 Like
#define button_start  2
#define button_stop  3
#define limitswitch1  4
#define limitswitch2  5
#define limitswitch3  6
#define A_BACKWARD  8
#define A_FORWARD  7
#define B_BACKWARD  10
#define B_FORWARD  9
 
void setup() {
  pinMode(button_start, INPUT_PULLUP);
  pinMode(button_stop, INPUT_PULLUP);
  pinMode(limitswitch1, INPUT_PULLUP);
  pinMode(limitswitch2, INPUT_PULLUP);
  pinMode(limitswitch3, INPUT_PULLUP);
  pinMode(A_BACKWARD, OUTPUT);
  pinMode(A_FORWARD, OUTPUT); 
  pinMode(B_BACKWARD, OUTPUT);
  pinMode(B_FORWARD, OUTPUT);
}

void loop() {
  while(digitalRead(button_stop)==LOW ){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  
  if(digitalRead(button_start)==LOW && digitalRead(limitswitch1)==HIGH){
    digitalWrite(A_BACKWARD, HIGH);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if(digitalRead(limitswitch1)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, HIGH);
  }
  if(digitalRead(limitswitch2)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, HIGH);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if(digitalRead(limitswitch3)==LOW && digitalRead(button_start)==HIGH){
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, HIGH);
    digitalWrite(B_FORWARD, LOW);
  }
}
1 Like

I assume the emergency stop button is not momentary since you are checking for the state of it and not the change in state. Do you want the system to resume after resetting the emergency stop button? If so, your code will work as intended. If not, you should set a flag and never reset it so a restart of the system if forced.

Can you clarify whether you want a start and stop button or a single start/stop button?

I want it to be 2 separate button, one is to start the loop, and one is to stop it.

That would be fine if it resume after pressing the emergency button

I have test this sketch and I would need to hold down the emergency button to stop the movement, but is it possible to just press the e button once instead of holding it down?

изображение
this is emergency button. it will fixed if you hit them. you need to pull red mushroom to release the emergency state.
in your case you need stop button. ok, here:

#define button_start  2
#define button_stop  3
#define limitswitch1  4
#define limitswitch2  5
#define limitswitch3  6
#define A_BACKWARD  8
#define A_FORWARD  7
#define B_BACKWARD  10
#define B_FORWARD  9
boolean isStart = false;

void setup() {
  pinMode(button_start, INPUT_PULLUP);
  pinMode(button_stop, INPUT_PULLUP);
  pinMode(limitswitch1, INPUT_PULLUP);
  pinMode(limitswitch2, INPUT_PULLUP);
  pinMode(limitswitch3, INPUT_PULLUP);
  pinMode(A_BACKWARD, OUTPUT);
  pinMode(A_FORWARD, OUTPUT);
  pinMode(B_BACKWARD, OUTPUT);
  pinMode(B_FORWARD, OUTPUT);
}

void loop() {
  if (digitalRead(button_start ) == LOW && !isStart ) {
    isStart = true;
    digitalWrite(A_BACKWARD, HIGH);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if (digitalRead(button_stop) == LOW ) {
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
    isStart = false;
  }
  if (digitalRead(limitswitch1) == HIGH &&  isStart) {
    digitalWrite(A_BACKWARD, HIGH);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if (digitalRead(limitswitch1) == LOW && isStart) {
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, HIGH);
  }
  if (digitalRead(limitswitch2) == LOW && isStart) {
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, HIGH);
    digitalWrite(B_BACKWARD, LOW);
    digitalWrite(B_FORWARD, LOW);
  }
  if (digitalRead(limitswitch3) == LOW && isStart) {
    digitalWrite(A_BACKWARD, LOW);
    digitalWrite(A_FORWARD, LOW);
    digitalWrite(B_BACKWARD, HIGH);
    digitalWrite(B_FORWARD, LOW);
  }
}

Repeating : A stop should be hard wired and not operate through software . Releasing the stop button should not automatically restart a machine .
If this is a an industrial type machine , check local regulations and take specialist advice ( not from here !)

right. "start" button enable a movement. "stop" - stop and disable everything

1 Like

Yes, my mistake of calling it emergency button. I just want a button to stop everything. From the code you provide, I am not able to run it like before, my limitswitch 3 somehow is not working.

What code are you using?

I was using the one @kolaha provide. Your code I dont know how to declare "startPressed" and "stopPressed"

It would be something like.

bool startPressed() {
	return !digitalRead(button_start);
}

bool stopPressed() {
	return !digitalRead(button_stop);
}

It's just a statement that determines whether the start or stop button is pressed.

Edit:
Removed static modifiers since it's probably just confusing

I think a clear definition of start and stop buttons would help:

Is the "start" button momentary? If so, does it have to stay pressed for motion to continue?
Is the "stop" button momentary or latched? If latched can motion restart once it returns to the off position? If momentary does a transition from off to on cause motion to stop? Can motion restart when the start button is pressed?

Also, you have all inputs declared as INPUT_PULLUP so why are you checking for HIGH on the start switch? Wouldn't LOW be pressed?