joystick and robot arm

Hi

I would like to try making a joystick to control a robot arm with a joystick so that the more I push the joystick forward the faster the servo on the arm forward, and the same in the opposite direction.

but I can not really figure out how I programere it.

Hope you can help me.

this is the parts i am using

joystick

Robot arm

You have to increment or decrement the servo positions in regular time intervals (see BlinkWithoutDelay). For a variable servo speed the joystick inputs (analogRead, map) determine either the update rate of the servos, or the amount of the increments. Start with mapping the input values into some speed indication, that can be used to modify the update interval or increments.

The code in this demo may be what you want, or close to it.

...R

thx.

i tryed to make my the demo so it regulate the speed, but i dont know if this is the right way to do that.

// Simple demonstration to show how to move a servo with a joystick 
//   or potentiometer so that the servo does not lose its position
//   when the joystick centres itself

#include <Servo.h>

byte servoPin = 7;  // servo signal connection
Servo myServo;
int servoPos = 90;
int servoMax = 180;
int servoMin = 0;
 int servoMaxspeed = 20;  // adjust Max degrees per movement
int servoMinspeed = 1;  // adjust Min degrees per movement

int servoMove = 0;

byte potPin = A0;   // center pin of joystick or potentiometer connected here
int potValue = 0;
int potCentre = 512; // adjust to suit your joystick
int potDeadRange = 50; // movements by this much either side of centre are ignored

unsigned long curMillis;
unsigned long readIntervalMillis = 100;
unsigned long lastReadMillis;

void setup() {
  Serial.begin(9600);
  Serial.println("Starting JoystickServo.ino");
  
  myServo.attach(servoPin);
  myServo.write(servoPos);
}

void loop() {
  curMillis = millis();
  readJoystick();
  moveServo();
  showPosition();
}

void readJoystick() {
      // check the time
  if (curMillis - lastReadMillis >= readIntervalMillis) {
    lastReadMillis += readIntervalMillis;
      // read the joystick
    potValue = analogRead(potPin);
      // figure if a move is required
    if (potValue > potCentre + potDeadRange) {
      servoPos += servoMove;
    }
    if (potValue < potCentre - potDeadRange) {
      servoPos -= servoMove;
    }
      // check that the values are within limits
    if (servoPos > servoMax) {
      servoPos = servoMax;
    }
    if (servoPos < servoMin) {
      servoPos = servoMin;
    }
  }

  if ( potValue<462){

   servoMove = map(potValue, 462, 0, servoMinspeed, servoMaxspeed);
  }

  if ( potValue>562){

   servoMove = map(potValue, 562, 1023, servoMinspeed, servoMaxspeed);
  }
}

void moveServo() {
  myServo.write(servoPos);
}

void showPosition() {
  Serial.print("PotValue ");
  Serial.print(potValue);
  Serial.print("   ServoPos ");
  Serial.print(servoPos);
  Serial.print("   servoMove ");
  Serial.println(servoMove);
}

Are you using a rotary or a linear servo?

To properly reflect the required move:

if (potValue > potCentre + potDeadRange/2)
 servoMove = map(...);
else if (potValue < potCentre - potDeadRange/2)
 servoMove = map(...);
else
 servoMove = 0;

myServo.write(servoMove);

brunokc:
i tryed to make my the demo so it regulate the speed, but i dont know if this is the right way to do that.

This piece of your code

  if ( potValue<462){

   servoMove = map(potValue, 462, 0, servoMinspeed, servoMaxspeed);
  }

  if ( potValue>562){

   servoMove = map(potValue, 562, 1023, servoMinspeed, servoMaxspeed);
  }

seems to undo everything that my program tried to do.

I think you should be using the value of servoPos as calculated by my code to control the speed of your motor.

…R

ok, i have used all day to come up with that idea. i don't know how to do what you are saying.

DrDiettrich:
Are you using a rotary or a linear servo?

To properly reflect the required move:

if (potValue > potCentre + potDeadRange/2)

servoMove = map(…);
else if (potValue < potCentre - potDeadRange/2)
servoMove = map(…);
else
servoMove = 0;

myServo.write(servoMove);

right now i am using a linear servo, but i will use a joystick when i comes.

brunokc: ok, i have used all day to come up with that idea. i don't know how to do what you are saying.

The Servo library has no concept of servo speed. It just sends a signal to a servo and the servo moves to the new position as fast as it can. Different servos will move at different speeds.

You can create the appearance of slowing a servo by getting it to move (say) 1 degree in each of 10 steps with a short interval between the moves in contrast to moving the full 10 degrees in one go at the full servo speed.

I am finding it difficult to get my head around the idea of your servo moving at different speeds because there is such a short distance for the servo to move. However I can understand that you might want it to move more slowly than its natural speed.

If you look at the servo code in Several Things at a Time you will see how to make the servo move slowly. You could use the value from the variable servoMove in my joystick demo to change the value of the variable servoInterval in the severalThings demo and that would change the sweep speed.

...R