Robot arm problems with joysticks and base

Hi, I'm making a 6 d.o.f. robot arm (exactly like a human arm), and have no idea what I'm doing wrong. My objective is to make a servo move when a joystick is at a certain position, ex: I move the joystick up and the servo moves in one direction until I put the joystick back in neutral position. I'm using an arduino due, if you're confused about the pin names. By the way, this is my first arduino project, so excuse me if I have stupid things in here, and please don't tell me to "start with something easier" or whatever, I want to learn so please help me with that. The other issue I'm having is that I don't know how to make a base that can rotate, so that all the weight of the arm isn't on the servo, the servo merely makes it turn. Thanks in advance

Here is my code:

#include <Servo.h>

Servo claw; //it's named claw because it will be the servo that operates the claw

int clawpos = 0; //to store the position of the servo

int onex = A0; //the x-axis reading from the thumbstick, on pin A0

void setup () {
  
  claw.attach (2);
  
  pinMode (onex, INPUT);

}

void loop () {
  
  for (onex = 0; onex < 400; clawpos += 1);
  claw.write (clawpos);
  delay (20);
  
  for (onex = 0; onex > 500; clawpos -= 1);
  claw.write (clawpos);
  delay (20);
  
}

Here is my setup:

I'm using this joystick: Thumb Slide Joystick - COM-09426 - SparkFun Electronics

3.3V and 5V produce different values when you read the pin. Make sure you calibrate it first, see what the maximum and minimum values are when you read the pin @ 3.3V.

Well, before the servo just went in one direction as far as it could go and stayed there, now the servo stays at 90 degrees. Not sure if that's progress or not.

your for-loop looks strange...

Any idea what's strange about it?

for (onex = 0; onex < 400; clawpos += 1);
claw.write (clawpos);
delay (20);

for (onex = 0; onex > 500; clawpos -= 1);
claw.write (clawpos);
delay (20);

When does onex get incremented or decremented?

Does your servo/pot combo work using the "knob" example in the IDE?

I modelled it off of the "sweep" example

HazardsMind:

for (onex = 0; onex < 400; clawpos += 1);
claw.write (clawpos);
delay (20);

for (onex = 0; onex > 500; clawpos -= 1);
claw.write (clawpos);
delay (20);

When does onex get incremented or decremented?

When the analog value of the thumbstick is more then 450, and when the value is less than 750. I realize the flaw there, so I'll try vice versa tomorrow. Thanks

Here's my latest code. I don't know if the brackets around the claw.write stuff is necessary but they're there in the "sweep" example in the ide.

#include <Servo.h>

Servo claw; //it's named claw because it will be the servo that operates the claw

int clawpos = 0; //to store the position of the servo

int onex = A0; //the x-axis reading from the thumbstick, on pin A0

void setup () {
  
  claw.attach (2);
  
  pinMode (onex, INPUT);

}

void loop () {
  
  for (onex = 0; onex < 450; clawpos += 1); //says that if the value of the thumbstick is less than 450 the position of the servo should be increased by 1
  
  {
  claw.write (clawpos); //writes the updated position to the servo
  delay (200); //gives it time to get there without jitters
  }
  
  for (onex = 0; onex > 750; clawpos -= 1);
  
  {
  claw.write (clawpos);
  delay (200);
  }
  
}

You still didn't fix the issue and you added an unneeded semicolon.

for (onex = 0; onex < 450; clawpos += 1);
for (onex = 0; onex > 750; clawpos -= 1);

I modelled it off of the "sweep" example

Maybe not the best choise. The knob example uses a pot to control a servo. Below is some test code for a two pot joystick.

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservoS1;
Servo myservoS2;

int potpinS1 = 0;  //analog input pin A0
int potpinS2 = 1;

int newvalS1, oldvalS1;
int newvalS2, oldvalS2;

void setup() 
{
  Serial.begin(9600);  
  myservoS1.attach(2);  
  myservoS2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newvalS1 = analogRead(potpinS1);           
  newvalS1 = map(newvalS1, 0, 1023, 0, 179); 
  if (newvalS1 < (oldvalS1-2) || newvalS1 > (oldvalS1+2)){  
    myservoS1.write(newvalS1);
    Serial.print("1- ");
    Serial.println(newvalS1);
    oldvalS1=newvalS1;
  }

  newvalS2 = analogRead(potpinS2);
  newvalS2 = map(newvalS2, 0, 1023, 0, 179);
  if (newvalS2 < (oldvalS2-2) || newvalS2 > (oldvalS2+2)){  
    myservoS2.write(newvalS2);
    Serial.print("2- ");    
    Serial.println(newvalS2);
    oldvalS2=newvalS2;
  }
  delay(50); //slow down looping to better read serial monitor 
}

Maybe not the best choice. The knob example uses a pot to control a servo. Below is some test code for a two pot joystick.

@zoomkat I modelled it off of the sweep example because when I move the joystick in a direction, I want the servo to move in one direction until I put the joystick back in to neutral position, ex: I move he joystick up, and the servo moves in one direction, until I put the joystick back to the centre. So: If the value of one of the joysticks analog out put is less than 450, I want the joy stick to move in one direction until it reaches its end point, or until the value of the same analog output changes to more than 750, which would cause it to move in the other direction. If the value is somewhere in between 450 and 750, then I want the servo to stay at the position it's at. I just don't know how to put that in to code. Thanks for all your help so far. By the way, I tried your code and it was exactly like the "knob" example in the ide. Move the joystick up, servo moves, move the joystick back to neutral, servo goes back to neutral.

You still didn't fix the issue and you added an unneeded semicolon.

@HazardsMind I don't see how the example you gave is different from what I have in my code..?

I made the below code a while back to test sweeping a servo using buttons. I haven't gotten to try it yet, but probably could be adapted for using a pot.

//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 6; //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;
int pos = 90; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  servo1.write(pos); //starting position
  digitalWrite(6, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
  Serial.println("servo button sweep test 12-23-2013");
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    pos=(pos+1);
    if(pos>180) pos=180; //limit upper value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement 
  }    

  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    pos=(pos-1);
    if(pos<0) pos=0; //limit lower value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement
  }
}

It should be

for (clawpos = 0; clawpos < 450; clawpos += 1);
for (clawpos = 0; clawpos > 750; clawpos -= 1);

Because you never increment or decrement onex in the for loops.

But upon closer inspection, onex is a reading from the analog input A0, so why are you using FOR loops at all and not mapping the value?

clawpos = map( onex, 0, 1023, 0, 180);

@HazardsMind I'm using for loops instead of mapping the value because I want the servo to move continuously in a direction, unless the servo is in neutral position, ex: I move the joystick up, and the servo moves to the right until I put the joystick back to its center position. Much like telling a continuous servo to go to a certain position, and then it doesn't stop until you stop telling it to go there.

You dont need a for loop to do that, just tell it where to go and unless the value changes, the servo will stay the same.

Ok, imagine the "sweep" program, except you are able to control which direction it sweeps in, and once the servo reaches it's endpoint it stays there. Now, imagine that the servo doesn't always rotate to its endpoint, but moves in steps of lets say 5 degrees, so it doesn't necessarily move to its endpoint unless it's told to do so. Now, imagine that a joystick, when in its up position makes the servo move in one direction, and that when you move the joystick in the other direction, the servo moves in the other direction. So exactly like "sweep" except it only moves as far as it can in one direction and doesn't rebound and move in the opposite direction, as well as only moves when the joystick tells it to, meaning it doesn't just move from end to end but can can stop at any position. Hope that's a bit clearer and doesn't confuse you more. Try reading this in reverse after reading it there right way, will help you understand.

You just want the servo to move in increments when some condition exist. Pot value greater than neutral, increase servo value, pot value less than neutral, decrease pot value. Pot value neutral, keep current servo value.