AhmedAzouz
Published © LGPL

PC Controlled Robotic Arm

A try to simulate the automation of modern manufacturing technologies specially the robotic arms that used in all fields of manufacturing.

AdvancedFull instructions providedOver 2 days40,838
PC Controlled Robotic Arm

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Servos (Tower Pro MG996R)
×4

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Printable SKP file

Schematics

Circuit Diagram

Code

Task.ino

Arduino
int delayT = 350;

void Home() //Call this fucntion when u want to set arm in home position
  {
    MyServo0.write(valGripper);   // Gripper
    delay(15);
    MyServo.write(valBase);   // base
    delay(30);
    MyServo2.write(valShoulder);  // shoulder
    delay(30);
    MyServo3.write(valElbow); // elbow
    delay(delayT);
  }

void Pick() // This is fixed pick place.
  {
   MyServo.write(4);   // base
   delay(delayT);
  
   MyServo3.write(125); // elbow 
   MyServo2.write(95);  // shoulder
   delay(delayT);
   
   MyServo0.write(50);   // Gripper open wide
   delay(delayT);
   
   MyServo0.write(2);   // Gripper close
   delay(delayT);
  
   MyServo2.write(60);  // shoulder up little
   MyServo3.write(80);  // elbow up little
   delay(delayT);
   
   Serial.println("Object Pickeded");
  }


void Drop() // This is fixed Drop place.
  { 
   MyServo.write(145);   // base
   delay(delayT);
   
   MyServo2.write(80);  // shoulder
   delay(15);
   
   MyServo3.write(115); // elbow
   delay(delayT);
  
   MyServo0.write(40);   // Gripper open wide
   delay(delayT);
  
   MyServo3.write(90);  // elbow up little
   delay(delayT);
   
   Serial.println(F("Object Dropped"));
   }

void  Playback()
  {
    // https://arduino.stackexchange.com/questions/1013/how-do-i-split-an-incoming-string
     // input MUST be array ( servoId : Position & servoId : Position & servoId : Position )
      
    //      String phrase;
    //      phrase = String(phrase + ByteReceived); // convert the char input to stirng can be split

    // Read each command pair 
    char* command = strtok(ByteReceived, "&");
    while (command != 0)
    {
        // Split the command in two values
        char* separator = strchr(command, ':');
        if (separator != 0)
        {
            // Actually split the string in 2: replace ':' with 0
            *separator = 0;
            int servoId = atoi(command);
            ++separator;
            int angle = atoi(separator);
    
            // Do something with servoId and angle
            
            if (servoId = 1)
            {
               MyServo.write(angle);
                delay(delayT);
            }
            else if (servoId = 2)
            {
               MyServo2.write(angle);
                delay(delayT);
            }
            else if (servoId = 3)
            {
              MyServo3.write(angle);
                delay(delayT);
            }
            
        }
        // Find the next command in input string
        command = strtok(0, "&");
    }
    
  }


  

RobotArm.ino

Arduino
// written by AhmedAzouz
#include <Servo.h>

Servo MyServo,MyServo2,MyServo3,MyServo0;

char ByteReceived ;

int MyPosition,MyPosition2,MyPosition3,MyPositionG;
int runCounter = 0;
int ledPin = A5;

// set the default home servo positions 
int valGripper = 30;
int valBase = 70;
int valShoulder = 25;
int valElbow = 150;

void setup()
{
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
 digitalWrite(13,LOW); 

 MyServo0.attach(12); // Gripper  attach
 MyServo.attach(11); // base  attach
 MyServo2.attach(13); //shoulder  attach
 MyServo3.attach(10); //Elbow attach


// Set startup positions

 MyServo0.write(valGripper);   // Gripper
 delay(15);
 MyServo.write(valBase);   // base
 delay(30);
 MyServo2.write(valShoulder);  // shoulder
 delay(30);
 MyServo3.write(valElbow); // elbow
 delay(300);
}
 
void loop()
{

    Serial.print("Base : ");
    Serial.print(valBase);
    Serial.print("\t");
    Serial.print("Shou : ");
    Serial.print(valShoulder);
    Serial.print("\t");
    Serial.print("Elbow : ");
    Serial.print(valElbow);
    Serial.print("\t");
    Serial.print("Grip : ");
    Serial.println(valGripper);


  //if(Serial.available())
  //Byte=Serial.read(); //read it
  //{

  while (Serial.available() > 0 ) { 
    
    ByteReceived = Serial.read();

  switch (ByteReceived) {

    // ************* LED
    case 'n':
      digitalWrite(ledPin,HIGH);            //switch LED On
        Serial.println("LED is on");
      
      break;

    case 'f':
      digitalWrite(ledPin,LOW);            //switch LED off
        Serial.println("LED is off");
      
      break;

    // ************* Base
    case 'l':
      MyPosition = MyServo.read() + 2;    // L for move base left
      MyServo.write(MyPosition);

      Serial.print("Base");
      Serial.print("\t");
      Serial.println(MyServo.read()); 
      delay(15);
      
      break;

    case 'r':
      MyPosition = MyServo.read() - 2;    // R for move base right
      MyServo.write(MyPosition);
      
      Serial.print("Base");
      Serial.print("\t");
      Serial.println( MyServo.read()); 
      delay(15);
      
      break;
      
      // ************* Elbow
      case 'u':
      MyPosition2 = MyServo2.read() + 2;   // U for move Elbow up
      MyServo2.write(MyPosition2);

      Serial.print("Elbow");
      Serial.print("\t");
      Serial.println(MyServo2.read()); 
      delay(15);
      
      break;

      case 'd':
      MyPosition2 = MyServo2.read() - 2;   // D for move Elbow down
      MyServo2.write(MyPosition2);

      Serial.print("Elbow");
      Serial.print("\t");
      Serial.println(MyServo2.read()); 
      delay(15);
      
      break;

      // ************* Shoulder
      case 'w':
      MyPosition3 = MyServo3.read() + 2;  // W for move Shoulder down
      MyServo3.write(MyPosition3);

      Serial.print("Shoulder");
      Serial.print("\t");
      Serial.println(MyServo3.read()); 
      delay(15);
      
      break;

      case 's':
      MyPosition3 = MyServo3.read() - 2;  // S for move Shoulder up
      MyServo3.write(MyPosition3);

      Serial.print("Shoulder");
      Serial.print("\t");
      Serial.println(MyServo3.read()); 
      delay(15);
      
      break;

      // ************* Gripper
      case 'o':
      MyServo0.write(30);                // O to open Gripper
      Serial.print("Gripper Open");
      Serial.print("\t");
      Serial.println(MyServo0.read()); 
      delay(15);
      
      break;

      case 'c':                          // C to open Gripper
      MyServo0.write(1);
      Serial.print("Gripper Close");
      Serial.print("\t");
      Serial.println(MyServo0.read()); 
      delay(15);
      
      break;

      // ************* Default Dropped position (quick drop position)
      case 'q':
      
       digitalWrite(ledPin,HIGH); 
       Serial.println("Default Dropped activated!");  
       Pick();
       Drop();
       Home();
       digitalWrite(ledPin,LOW);
      
      break;

      // ************* Force Stop and go home position
      case 'h':
      
       Home();
       digitalWrite(ledPin,LOW);
      
      break;

       // ************* Automatic mood runs Playback function
       // serial read Must be in this format: 1:90&2:80&3:180
       // pickup place is fixed, this function will automate the drop position.
      default:

         digitalWrite(ledPin,HIGH); 
         Serial.println("Automatic mood activated!");  
         Pick();
         Playback();
         Home();
         digitalWrite(ledPin,LOW);  
      
      break;
  }

}

}

Credits

AhmedAzouz

AhmedAzouz

10 projects • 153 followers
High qualified software engineer, Creativity and Technology have always been a big part of my life.

Comments