PrasadSampathkumar
Published © GPL3+

Joystick Controlled Robot Arm

Simple robot arm project with joystick control.

BeginnerShowcase (no instructions)2,233
Joystick Controlled Robot Arm

Things used in this project

Story

Read more

Schematics

breadboard pic

will update with circuit diag shortly

Code

joystick controlled Robot arm

Arduino
4DOF robot arm with joystick control
#include <Servo.h>

Servo myservo1;  // gripper //
Servo myservo2; // rotating base //
Servo myservo3; // arm motor right //
Servo myservo4; // arm motor left //


int pos = 0;    // variable to store the servo position
int horz = analogRead(A2);
int vert = analogRead(A1);
int push = analogRead(A0);
bool gripperState = true;
bool gripperStateLast = true;
int setPointvert;
int setPointhorz;
int i = 0;
int gripperOpen = 100;
int gripperClose = 65;
int forwardStop = 110;
int backwardStop = 15;
int leftStop = 30;
int rightStop = 150;

void setup() {
  myservo1.attach(3);  // control for gripper
  myservo2.attach(5); // control for base //
  myservo3.attach(6); // control for right arm //
  myservo4.attach(9); // control for left arm //
  pinMode(A0, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  push = analogRead(A0);
  //Serial.println("push " + String(push));
  if (push < 100) {
    myservo1.attach(3);
    myservo1.write(gripperOpen)
  }
  else {
    myservo1.attach(3);
    myservo1.write(gripperClose);     
  }

  //horizontal movement control//
  horz = analogRead(A2); // base rotate response to joystick
  // setPointhorz = map(horz, 0, 1023, rightStop, leftStop);
  // Serial.println("horz " + String(horz) + "//" + "leftStop " + String(leftStop) + "setPointhorz " + String(setPointhorz));
  if (horz > 950) {
    rightRotate();
  }
  else {
    if (horz < 100) {
      leftRotate();
    }
    else {
      if ((horz >= 100 && horz <= 490) || (horz <= 950 && horz >= 515)) {
        toCenter();
      }
    }
  }

  // vertical arm movement//
  vert = analogRead(A1); // base rotate response to joystick
  if (vert > 970) {
    forward();
  }
  else if (vert < 400) {
    backward();
  }
  else {
    myservo3.write(setPointvert);
  }
}

// forward movement control//
void forward() {
  myservo3.attach(6);
  pos = myservo3.read();
  if (pos != forwardStop) {
    for (setPointvert = pos; setPointvert <= forwardStop; setPointvert += 1) {
      myservo3.write(setPointvert);
      //Serial.println("setPointvert " + String(setPointvert) + "//" + "vert " + String(vert));
      delay(20);
      if (setPointvert > 60 && setPointvert < 80) {
        myservo1.write(gripperOpen);
      }
      if (setPointvert == forwardStop) {
        setPointvert = myservo3.read();
        myservo3.detach();
      }
    }
  }
  else {
    myservo3.write(pos);
  }
}

// backward movement control//
void backward() {
  myservo3.attach(6);
  pos = myservo3.read();
  if (pos != backwardStop) {
    for (setPointvert = pos; setPointvert >= backwardStop; setPointvert -= 1) {
      myservo3.write(setPointvert);
      //Serial.println("setPointvert " + String(setPointvert) + "//" + "vert " + String(vert));
      delay(20);
      if (setPointvert == backwardStop) {
        myservo3.detach();
      }
    }
  }
  else {
    myservo3.write(pos);
  }
}

// center to right movement control//
void rightRotate() {
  myservo2.attach(5);
  delay(10);
  pos = myservo2.read();
  if (pos != rightStop) {
    for (setPointhorz = pos; setPointhorz <= rightStop; setPointhorz += 1) {
      myservo2.write(setPointhorz);
      delay(20);
      if (setPointhorz == rightStop) {
        myservo2.detach();
      }
    }
  }
  else {
    myservo2.write(pos);
  }
}

// center to left movement control//
void leftRotate() {
  myservo2.attach(5);
  pos = myservo2.read();
  if (pos != leftStop) {
    for (setPointhorz = pos; setPointhorz >= leftStop; setPointhorz -= 1) {
      myservo2.write(setPointhorz);
      delay(20);
      //Serial.println("setPointhorz " + String(setPointhorz) + "//" + "pos " + String(pos));
      if (setPointhorz == leftStop) {
        myservo2.detach();
      }
    }
  }
  else {
    myservo2.write(pos);
  }
}

// left to center movement control//
void toCenter() {
  myservo2.attach(5);
  pos = myservo2.read();
  if (pos > 90 && (horz >= 100 && horz <= 480)) {
    for (setPointhorz = pos; setPointhorz >= 90; setPointhorz -= 1) {
      myservo2.write(setPointhorz);
      delay(20);
      if (setPointhorz == 90) {
        myservo2.detach();
      }
    }
  }
  else {
    if (pos < 90 && (horz <= 950 && horz >= 520) ) {
      for (setPointhorz = pos; setPointhorz <= 90; setPointhorz += 1) {
        myservo2.write(setPointhorz);
        delay(20);
        if (setPointhorz == 90) {
          myservo2.detach();
        }
      }
    }
  }
}

Credits

PrasadSampathkumar

PrasadSampathkumar

1 project • 0 followers

Comments