robotic arm help!

I got an arduino/genuino 101 for my birthday a couple days along with a robotic arm and i am quite new to it. They didnt have instructions to the robotic arm but i got it working. i uploaded the code and it moves. But when i move the arm and let go it slams right back into its default position. here is the code:

#include <Servo.h>

Servo servo1;
Servo servo2;

int JoyX = 0;
int JoyY = 1;

int JoyVal;

void setup() {
servo1.attach(3);
servo2.attach(5);

}

void loop () {

JoyVal = analogRead (JoyX);
JoyVal = map (JoyVal, 0, 1023, 0, 180);
servo1.write (JoyVal);

JoyVal = analogRead (JoyY);
JoyVal = map (JoyVal, 0, 1023, 0, 180);
servo2.write (JoyVal);
delay(15);

}

Is there anything i could do to have it hold its position.

Is there anything i could do to have it hold its position.

Sure thing. Don't let go of the joystick.

If you want some movements of the joystick (when YOU push it) to mean something, and other movements (when the spring pushes it back to center) to not mean anything, you have to have SOME means of telling the Arduino whether changes in the joystick values mean anything, or not.

PaulS:
Sure thing. Don't let go of the joystick.

If you want some movements of the joystick (when YOU push it) to mean something, and other movements (when the spring pushes it back to center) to not mean anything, you have to have SOME means of telling the Arduino whether changes in the joystick values mean anything, or not.

So what i want to do is to move the joystick and let go and still have the robotic arm stay in the same position when i let go of the joystick. How could i program the arduino to be able to have it stay. I know i could just program the servos to do certain tasks but I would also like to be able to connect the joystick for direct control.

Delta_G:
Instead of writing the values from the joystick directly to the servo, use some if statements to decide based on those values what to do with the value you are writing to the servo. Something like (for only one servo. I'm lazy, you add the other one.)

#include <Servo.h>

Servo servo1;

int JoyX = 0;

int JoyVal;

int servoPos;

void setup() {
servo1.attach(3);

}

void loop () {
 
  JoyVal = analogRead (JoyX);
  JoyVal = map (JoyVal, 0, 1023, -5, 5);  // Just picked some numbers, pick some different ones
 
  servoPos += JoyVal;

constrain(servoPos, 0, 180);

servo1.write(servoPos);
 
  delay(15);

}





Probably not exactly that, but you should get the picture. This way when the stick goes back to the middle, hopefully JoyVal will come out mapped to 0 and servoPos won't change.

i tried to add an if statement to the code:

JoyVal = analogRead (JoyX);
JoyVal = map (JoyVal, 0, 1023, 0, 180);
constrain(servoPos, 0, 180);
servo1.write (servoPos);
if(servoPos < 1) {
JoyVal == 0;
};

sorry if i am making a stupid and obvious mistake. Thank you for your help so far. I really hope i can get this arm working.

thank you so much. But now i have encountered another problem. i cant drive the arm back (facepalm). how could i do that? Sorry to be a pain. EDIT: Now i cant have it to hold its position again. Ill keep working on it.

You REALLY need to decide when a movement of the joystick should move the servo and when it shouldn't. The Arduino has NO clue whether it is you pushing or pulling on the joystick or the spring built into the joystick moving it.

Usually, there would be a switch connected to the joystick. While the switch is pressed, moving the joystick moves the servo. When the switch isn't pressed, moving the joystick does not move the servo.

Until then, you are just pissing into the wind.

Ok, thank you. Ill work on it today and try that out