Create a emergency stop!!

Hi,

I would like to create a emergency stop function when in process, is there anyway to do that?

//Pins
int ledPin1 = 11;
int ledPin2 = 12;
int dirpin = 3;
int steppin = 4;
int laser = 10;

//Variables
long range = 10000;
int incomingByte = 0; // store incoming serial data

void setup()
{
Serial.begin(9600);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(laser, OUTPUT);
}

void loop()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();

Serial.print("received input");
Serial.println(incomingByte);
}
delay(100);

if (incomingByte == 83)
{
Serial.println("START");
Serial.println("Forwarding");
digitalWrite(dirpin, LOW);
digitalWrite(ledPin1, HIGH);
digitalWrite(laser, HIGH);
delay(200);

int i;
for (i = 0; i<range; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(1500);

}
digitalWrite(ledPin1, LOW);
digitalWrite(laser, LOW);
delay(1000);

Serial.println("Backwarding");
digitalWrite(dirpin, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(laser, HIGH);
delay(200);

for (i = 0; i<range; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(1500);
}
digitalWrite(ledPin2, LOW);
digitalWrite(laser, LOW);
delay(500);
}
}

Using one of the two external interrupt input pins is one way to 'take over' a running sketch. You don't say exactly what must happen in your sketch to perform an emergency stop. If this is for personal safety features one should always at least consider external hardwired interlock wiring, etc over software safety generated functions.

Lefty

I read from other post that the interrupt input need to be selected either pin 2 or pin 3 , and connecting to a physical input. And my sketch is planning to start when a incomingByte == 83 is received from serial port. Once a emergency stop either physical or by typing a incomingByte ==84 to stop the sketch to run instantly. and everything off.

When you say everything off, do you mean you want the entire system to shut down? Why not physically wire the e stop as a normally closed switch on the mains. That way when the e stop is hit, it disconnects power.

If this will solve the physical e stop issue, then let me know and I'll suggest a method as to shutting down the system through a software trigger.

If not then please clarify as to what you exactly you need from the e stop. ie. stop the current task but leave the system powered.

Actually, I want to stop the void loop to run when the emergency stop is pressed physically or by input a string from serial port. Then, after pressed a "resume" button or a "return" string from serial port, the stepper motor will return to it inital position. Below is the sequence..

Input "Start" string -- void loop start -- input "stop" string -- stop sketch immediately -- "input "return" string -- motor back to initial position -- all thing reset.

That all can be accomblished with code within your existing sketch. Remember you can have program loops inside of the main sketch loop that just wait for something to happen before returning to the main loop.

Lefty

After repeating trying , I am success to stop the loop if the input string is entered. But is there anyway to interrupt the loop before if run the full cycle? Is there any simple sample can show?

post your code please.

Hi ,

here is my code, I failed to stop the loop immediately when "t" is pressed, it only stop after the loop cycle is completed. Actually, I need below function

  1. press "s" first time , start the scanning cycle
  2. press "s" second time, pause the scanning cycle, press "s" third time resume the cycle .. and so on..
  3. press "t" stop the loop immediately and then run a loop to send the motor to it inital position (in here, I set i =0 for initial position) and reset all thing. I have try to make the code for above sequence a few weeks but I think it is hard to complete it. Is there any suggest I can try? Thank you!

//Pins
int ledPin1 = 11; // Forward LED
int ledPin2 = 12; // Backward LED
int dirpin = 3; // Direction signal control of stepper motor
int steppin = 4; // Stepper signal input
int laser = 7; // Laser pin

//Variables
long range = 5000; // the range of distance to go
incomingByte = 0; // store incoming serial data

void setup()
{
Serial.begin(9600);
pinMode(dirpin, OUTPUT); // sets the digital pin as output
pinMode(steppin, OUTPUT); // sets the digital pin as output
pinMode(ledPin1, OUTPUT); // sets the digital pin as output
pinMode(ledPin2, OUTPUT); // sets the digital pin as output
pinMode(laser, OUTPUT); // declare laser as output
}

void loop()
{
if (Serial.available() > 0) // read the incoming byte
{
incomingByte = Serial.read();

Serial.print("received input"); // indicate input
Serial.println(incomingByte);
}
delay(100);

while (incomingByte == 115) //when "s" is received from serial port, START scanning circle
{
Serial.println("START"); // Start scanning circle
Serial.println("Forward");
digitalWrite(dirpin, LOW); // Start forward direction.
digitalWrite(ledPin1, HIGH); // forward LED on
digitalWrite(laser, HIGH); // laser on
delay(100);

int i;
for (i = 0; i<range+1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(1500);
}

digitalWrite(ledPin1, LOW); // forward LED off
digitalWrite(laser, LOW); // laser off
delay(1000);

Serial.println("Backward"); // backward scanning
digitalWrite(dirpin, HIGH); // Change to backward direction.
digitalWrite(ledPin2, HIGH);
digitalWrite(laser, HIGH);
delay(200);

for (i = range; i>0-1; i--)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(1500);
}

digitalWrite(ledPin2, LOW);
digitalWrite(laser, LOW);
delay(500);

if (incomingByte == 116) //when "t" is received from serial port, STOP scanning circle
{
incomingByte = 0;
}

}
}