Keertan Ayamany
Published

Robot Arm with Leap Motion

A robot arm using three motors controlled with a Leap Motion controller. Using the Processing language to detect the Leap.

IntermediateFull instructions provided3 hours3,812
Robot Arm with Leap Motion

Things used in this project

Hardware components

DC motor (generic)
×3
Jumper wires (generic)
Jumper wires (generic)
×11
Cardboard
×1

Story

Read more

Schematics

Schematics for Arduino connections with Servos.

Code

Code for Arduino

Arduino
#include <VarSpeedServo.h> //VarSpeedServo library is used because it gradually moves the servo instead of snapping to position

char val;  //Variable to store input from Serial communicator which is coming from the Processing environment(Where Leap is running)


VarSpeedServo servo1; //declare Servos
VarSpeedServo servo2;
VarSpeedServo servo3;


void setup() {
  // put your setup code here, to run once:
  
servo1.attach(3);  // tell Arduino which pins the servos are attached to.
servo2.attach(5);
servo3.attach(10);
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly:


  if (Serial.available()){
    val = Serial.read ();
  }

      //8 positions saved according to the input from Serial Communicator
      
      if (val == '1'){
      servo1.write(130,50); //moves servo1 to 130 degrees with a speed of 50
      servo2.write(30,50);
      servo3.write(160,50);
    
      Serial.println(1);
  }

      if (val == '2'){
      servo1.write(30,50);
      servo2.write(150,50);
      servo3.write(30,50);
    
      Serial.println(2);
      }

      if (val == '3'){
      servo1.write(30,50);
      servo2.write(30,50);
      servo3.write(160,50);
      Serial.println(2);
      }

      if (val == '4'){
      servo1.write(130,50);
      servo2.write(150.50);
      servo3.write(30,50);
  
      Serial.println(2);
      }

      if (val == '5'){
      servo1.write(130,50);
      servo2.write(60,50);
      servo3.write(60,50);
    
      Serial.println(1);
      }

      if (val == '6'){
      servo1.write(30,50);
      servo2.write(120,50);
      servo3.write(120,50);
    
      Serial.println(2);
      }

      if (val == '7'){
      servo1.write(30,50);
      servo2.write(60,50);
      servo3.write(60,50);
      Serial.println(2);
      }

      if (val == '8'){
      servo1.write(130,50);
      servo2.write(120.50);
      servo3.write(120,50);
  
      Serial.println(2);
      } 

}

Code for Leap Motion on Processing

Processing
import de.voidplus.leapmotion.*; //call the leap motion library
import processing.serial.*;  // call the serial library to use serial communicator

String num; // variable to store output to send to arduino

String LeapX;
String LeapY;
String LeapZ;

int LX;
int LY;
int LZ;

LeapMotion leap;
Serial myPort;

void setup() {
  size(800, 500, OPENGL);
  leap = new LeapMotion(this);
  String portName = Serial.list()[0];
  myPort = new Serial (this,portName, 9600);
}

void draw() {
  background(255);
  for (Hand hand : leap.getHands ()) {
    PVector indexTip = hand.getIndexFinger().getRawPositionOfJointTip();

    text("x: " + nf(indexTip.x, 0, 1), 20, 40);
    text("y: " + nf(indexTip.y, 0, 1), 20, 60);
    text("z: " + nf(indexTip.z, 0, 1), 20, 80);
    
    LeapX = nf(indexTip.x, 0, 1); //location of fingertips in strings
    LeapY = nf(indexTip.y, 0, 1);
    LeapZ = nf(indexTip.z, 0, 1);
    
    
    LX = int(LeapX); //changes locations of fingertips to int variables
    LY = int(LeapY);
    LZ = int(LeapZ);
    
    
    
    if (LX>0) {    // check if hand is more than 0 on x-axis
      
        if (LZ>-100) { // check if hand is more than -100 on z axis
          
          if (LY< 265) { // check if hand is less than 265 on z axis
          num = "1";    // set num to 1, which is going to be position 1 on arduino
          println("1");
          }
          
              else { 
                  num = "5";
                  println("5");
              }
        } else {
          
          if(LY<265) {
          num ="2";
          println("2");
          }
          
          else {
          num="6";
          println("6");
          }
        }
      
      
    } else {
             if (LZ>-100) {
               if(LY<256) {
            num = "3";
          println("3");
               }
               else{
                num = "7";
                println("7");
               }
               
        } else {
          if (LY<265){
          num ="4";
          println("4");
          }
          else  {
           num="8";
           println("8");
          }
        }
      
    }
    
   
      
    myPort.write(num);  
    
    println("X:");
    println(LX);
    println("Y:");
    println(LY);
    println("Z:");
    println(LZ);

   
  }
}

Credits

Keertan Ayamany

Keertan Ayamany

0 projects • 1 follower

Comments