I want to make a robot arm Emergency stop code.. help

Hello everyone.. I bought a 5-axis robot arm kit, made it all, and tried to operate it, but I want to do emergency braking by attaching an additional sensor. But I'm not very good at it, so I don't know what to do..

The robot arm uses an Arduino Uno-based board made at the place where it was purchased, and the sensor I want to use is a self-made sensor, which is an analog sensor that outputs a voltage when a human touches it. This analog signal was connected to the Arduino Uno board and converted into a digital signal, and then the signal was put on the robot arm driving board.

If the robot arm normally collides with another object, I want to make a code that stops and repeats the original motion at the stop again after a certain period of time!!

const int sensorPin = A1; // sensor analog input
const int ledPin = 12; // OUTPUT digital pin
int val = 0; 

void setup() {
  Serial.begin(115200);
  pinMode(ledPin,OUTPUT);
}

void loop() {
  val = analogRead(sensorPin);

if (val>600) // If the analog signal is 600 or more, 1 output as a digital signal
  {
    digitalWrite(ledPin,1);
    delay(50);
    Serial.println(1);
  }
  else
  {
    digitalWrite(ledPin,0);
    delay(50);
    Serial.println(0);
  }
}
#include <MsTimer2.h> // interrupt 
#include <Servo.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET    4
#include <Sleep_n0m1.h>  // for emergency stop 
Adafruit_SSD1306 display(128,64,&Wire,OLED_RESET);
const int ledPin = 8; // input digital signal
Sleep sleep; 
unsigned long sleepTime;  // for emergency stop time

Servo base,arm1, arm2, wrist, gripper;
Servo servo[5] = {base,wrist,arm2,arm1,gripper};

int angle = 0;
int i=0;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin,INPUT);      // for OLED
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // for OLED
  display.setTextColor(WHITE);  // for OLED
  display.clearDisplay();   // for OLED
  base.attach(9);   
  base.write(0);
  wrist.attach(6); 
  wrist.write(90);
  arm2.attach(5);
  arm2.write(50);
  arm1.attach(3);
  arm1.write(90);
  gripper.attach(11);
  gripper.write(90);
  MsTimer2::set(100,interrupt_sensor);  // interrupt 
  MsTimer2::start();  
  sleepTime = 5000;   // stop time
}

void loop() {
    display.setTextSize(2); // set the font size 
    display.setCursor(25,25); //set the display location
    display.print("ASD Lab."); //string displayed
    display.display();// began to show     
  for (i; i<40; i++) 
  {
      arm1.write(120); 
      delay(50);

      for(angle=0 ; angle < 140 ; angle++) // base drive control
      {
        base.write(angle);
        delay(15);
      }
      for(angle = 50; angle>10;angle--) // arm2 drive control
      {
        arm2.write(angle);
        delay(15);
      }
      for(angle=90; angle > 35 ; angle--)// gripper drive control
      {
        gripper.write(angle);
        delay(15);
      }
      for (angle = 10; angle < 50; angle++) // arm2 drive control
      {
        arm2.write(angle);
        delay(15);
      }
      for(angle = 140; angle > 0 ; angle--)//base drive control
     {
      base.write(angle);
      delay(15);
     }
     for (angle=50 ; angle > 10; angle--) // arm2 drive control
     {
      arm2.write(angle);
      delay(15);
     }
     for(angle=35; angle < 90; angle++) // gripper drive control
     {
      gripper.write(angle);
      delay(15);
     }
     for (angle=10 ; angle < 50 ; angle++) //arm2 drive control
     {
      arm2.write(angle);
      delay(15);
     }
    }
}
void interrupt_sensor () {

int val = digitalRead(ledPin); //digital signal
Serial.println(val);

if (val == 1){                      // If the digital input is detected as 1, the robot arm stops immediately.
  Serial.println("Emergency STOP!");
  arm1.write(120); 
  sleep.pwrDownMode();
  sleep.sleepDelay(sleepTime);
}
}

How can I make sure it works and gives better results?
please help me....

Hello
If you are able to post the sketch in code tags, than do it, please.

i changed it .. do you know how i should write the code?

Sleeping perhaps is not the best method for an emergency stop. What does the arm if it does not receive servo signals any more?

If you want to retract the arm on an alarm to the previous position then have an array of moves that describe any desired movement. Each move can be described by a servo number, starting and ending position, speed or delay. Add an interpreter that can execute the moves held in that array one by one. This allows to add more movements later without changing the code, with movement data from a SD card or a CAD program. All that can be implemented and tested without the emergency sensor. You can add a reverse function that returns to the starting position of the entire current movement.

Then let the interpreter check at every step the emergency sensor. On alarm retract the current servo to the starting position of the current move and wait for a continuation signal or timeout. Afterwards continue execution from the current (aborted) move.

And that is why emergency stops should not rely on code.

Please remember to use code tags when posting code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.