Arduino Robot Arm controlled by Joystick

ahh can you explain what you just said again in more simpler terms? I dont think i got what he fully ment?
I was thinking the joystick has limited actions so i had to use buttons to compensate?

It'd take me ages to try to write it but imagine it would be something like:

on x axes + if button 2 is on then move wrist right else move shoulder right

on x axes- if button 2 is on then move wrist left else move shoulder left

on y axis - if button 1 is on then move elbow down if button 2 is on then close hand/claw else move shoulder down

on y axis + if button 1 is on then move elbow up if button 2 is on then open hand/claw else move shoulder up

ah i dont get this :s - think i'll have to start my project and see it from there and then..? this is confusing :(

Example: You can fill in the rest.

X = analogRead(A0);
Y = analogRead(A1);
B1 = analogRead(A2); // can be done also with digitalRead too, but I was just copying and pasting. 
B2 = analogRead(A3); //-------------------------------------------------------------------------

if( B1 == HIGH ) // button 1 is being held down.
{
  // move elbow servo based on Y axis +/-
}

else if( B2 == HIGH )
{
  // Open and Close claw servo based on Y axis +/-
  // Move wrist servo based on X axis +/-
}

else 
{
  // rotate base or shoulder (Left / Right) based on X axis +/-
  // Raise / Lower shoulder (Up / Down) based on Y axis +/-
}

If you don't want to make your own joystick from the small joysticks, then you may have to get a joystick like below and seriously hack it to get access to the pots and switches. Where you live may make a difference as to what hardware will be available to you.

http://www.walmart.com/ip/Thrustmaster-2960623-USB-Joystick-for-PC/21054374

If you go the hardware hack route, usually the electronics are in the base with wires running to it from the assorted buttons, switches and potentiometers (variable resistors). Although it will vary from device to device how easy it is to bypass the electronics and wire direct to the buttons etc. If they connect via wires - piece of cake. If they are mounted directly on the pcb then you might have to carefully desoulder and remove them or solder your wires onto the soldered contacts for the switch etc.

If you are using a usb one, it is posible to get a shield with usb socket and if you could get data on the signals it sends to the pc you could read those and use that (way beyond my programming skills though)

Just actually looked at the video. The pots in the joystick appear to just be used as switches, and the joystick button appears to sequence thru conditions/cases. The current condition/case status could be displayed by two or three LEDs instead of the computer monitor. The servo movement control could be a variation of the servo sweep example code. The servo with a particular button pressed could be commanded to an end position (like 0 or 180), and when the button is released, the servo.read() function could be used to determine current command position and command the servo to stop moving at its current position. If you have a servo, you should be able to start working on your project just using five wires rigged as buttons.

As already noted, in the video, the joystick button toggles a mode setting; in each mode the joystick controls a different pair of servos. If your teacher insists, you could certainly replicate this, but it seems like it would be clunky to do fine maneuvering if you have to keep switching between modes. Two joysticks would be much nicer.

I got one better. It may seem a little over the top for you, but it is actually very simple. Keep the one joystick, but in addition, use an accelerometer to control the Base and Arm. This way, the controls should be more fluid and the joystick will only be in charge of two functions instead of all three.

i dont know what an accelerometer is :( - I like the idea bout using the leds lights and onl one joystick - but i need to be able to finish the project and has to be moderately easy so if i have to use 2 joystick i dont mind - i still have no clue or visual image in my head of how to connect the servos to the joystick i bough through a breadboard and the arduino - the joystick has 5 pin and is facing in a weird way as you can see from the link

The servos will only give you about 5-6 inches of slack, so as in the video, you will need to make a lengthy cable of 5 or more wires. This will connect the joystick to the Arduino. As for the servos, you will need to have a power supply that can provide 6 volts! This will be enough to power the Arduino and the servos. Mainly the servos because they need 6 volts to work properly. Now if you use two different power supplies, you need to make sure the [u]grounds[/u] are connected. (Look that up if needed)

That should be enough to give you some idea to start with.

Basic two button code for a servo. You can use this to test your button setup, servo power supply, and such.:

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

@ZoomKat:

He will be using the joystick to control the servos, not the buttons. The buttons will determine which servos will move. Do you have any sample codes along those lines?

-Thanks for the codes :)

i know the basic of how everything is connected but actually knowing where to place and connect everything? like the joystick has 5 pin? and i am using 5 servos (and out of the 5, 2 servo are acting as one - im gonna use a y-cable to do this) but as far as where everything goes? how do i link the joystick's 5 pin to me breadboard and arduino? etc? like an image would help if any can provide please?

No don’t use a Y-cable for any of the servos, that will not work the way you want it to. Simply connect all five of the servos data pins to the Arduino’s digital pins (2 - 6). The wires are usually color coded. Yellow or blue is usually the data pin, and red/ black or red/ brown are for the power. Red being positive.

He will be using the joystick to control the servos, not the buttons. The buttons will determine which servos will move. Do you have any sample codes along those lines?

Looking at the video, the pots in the joystick do not appear to control the arm in the usual joystick pan/tilt setup. The servos do not appear to follow any absolute positioning of the joystick. When the joystick returns to the neutral position, the servos do not follow. I haven’t tinkered with my servos in probably two years, but I think the below is supposed to put the servo sweep code inside a condition based on a button push.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo myservo;
int pos = 0;    // variable to store the servo position 

void setup()
{
  pinMode(button1, INPUT);
  myservo.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
  }
}

okay i wont use that cable and connect them as you instructed - i wont be needing abreadboard? - how will i then link the joystick to the servos?

and zoomkat you see the codes you just sent - what are they and how do they work?

??? How else would you go about connecting the joystick to the servos? You can use a breadboard, unless you want to make your own PCB.

The codes ZoomKat provided work like this. If you press a button, the servo will move to a certain position, press another button and it will move to a different position.

what are they and how do they work?

Time for you to actually doing some testing and see how the code and servos work. Your instructor should be able to help you get the project started. Below is some basic servo test code where servo positions are sent from the serial monitor.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

hazardmind - im confused on how to wire everything up because the joystick has 5 pin - do i just stick them onto a breadboard? and then whats the next move? having already wired up the servos into the arduino inputs.. what next?

zoomkat - okay ill be starting some next soon & to confirm these codes are for one joystick and two button controllin g 5 servos?