how to move 2 servo's using a Joystick

Hello,

I have a problem with the program I wrote.
Whene I move the joystick the servo move's but whene I let it go the servo move's back to his starting
position and that's not what I want.
I want it to stay in that position.

can someone help me? Please?
Thanks in advance.

This is the program.

#include <Servo.h>

const int SERVOX=9;
const int SERVOY=10;
const int POTX=0;
const int POTY=1;

Servo myServox;
Servo myServoy;
int valX=0;
int valY=0;
void setup()
{
myServox.attach(SERVOX);
myServoy.attach(SERVOY);
}

void loop()
{
valX=analogRead(POTX);
valX =map(valX,0,1023,0,179);
myServox.write(valX);
delay(20);

valY=analogRead(POTY);
valY=map(valY,0,1023,0,179);
myServoy.write(valY);
delay(20);
}

Add a "lock" switch to your system, that you hit when the servos have been positioned?
Or an "enable" switch that only passes servo writes when the switch is pressed?

I want it to stay in that position.

Add a switch as suggested or maybe time how long the joystick has stayed in the position offset from the centre and when the required time has passed lock the servo in that position. How will the program know that you want to move the servo again ?

I have a problem with the program I wrote.

You have some problems with your thread posting techniques, too.

  1. You should always post code within
[code]code tags[/code]
  1. You should never double-post. how to move 2 servo's using a Joystick

I'm sorry
i still don't undersand it

Code tags - they look like this [code][/code] .

Or was it the cross-posting you don't understand?

You should turn your joystick reading into a signed "velocity":

  int xvel = map (analogRead (POTX), 0, 1023, -100, +100) ;
  int yvel = map (analogRead (POTY), 0, 1023, -100, +100) ;

Then if the velocity is large enough, regularly add it to the setpoint for the servos,
suitable scaled to keep the speed within reason? Something like

  static long xsetpoint = 0 ;
  static long ysetpoint = 0 ;
  if (abs (xvel) > 10)
    xsetpoint += xvel ;
  if (abs (yvel) > 10)
    ysetpoint += yvel ;
  myServox.write (constrain (xsetpoint / 100 + 90, 0, 180)) ;
  myServoy.write (constrain (ysetpoint / 100 + 90, 0, 180)) ;
  delay (20) ;

You should probably constrain the setpoints directly though.

I think this demo illustrates what you want

...R

Your program is perfect!!
But can you help me write if for 2 servo's please?
I'm bad at programming and make a lot off mistakes and it does not want to work
Thanks in advance

Wouter_H:
But can you help me write if for 2 servo's please?

Help? Yes.
But before, read the code carefully and understand what it does. It should be relatively easy to adapt it for 2 (or more) servos.

Wouter_H:
I'm bad at programming and make a lot off mistakes and it does not want to work

What exactly does not work? Are there error messages? What does your code look like? Don't forget to use code tags.

lg, couka

I did it but it wont work there are arror messages at moveservo1
can you please look at it Thanks in advance

#include <Servo.h>

byte servoPin1 = 6;  // servo signal connection
byte servoPin2 = 9;
Servo myServo1;
Servo myServo2;
int servoPos1 = 90;
int servoPos2 = 90;
int servoMax1 = 130;
int servoMax2 = 130;
int servoMin1 = 45;
int servoMin2 = 45;
int servoMove1 = 3;  // number of degrees per movement
int servoMove2 = 3;

byte potPin1 = A0;   // center pin of joystick or potentiometer connected here
byte potPin2 = A1;
int potValue1 = 0;
int potValue2 = 0;
int potCentre1 = 512; // adjust to suit your joystick
int potCentre2 = 512;
int potDeadRange1 = 50; // movements by this much either side of centre are ignored
int potDeadRange2 = 50;

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

void setup() {
  Serial.begin(9600);
  Serial.println("Starting JoystickServo.ino");
  
  myServo1.attach(servoPin1);
  myServo1.write(servoPos1);
  myServo2.attach(servoPin2);
  myServo2.write(servoPos2);
}

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

void readJoystick() {
      // check the time
  if (curMillis - lastReadMillis >= readIntervalMillis) {
    lastReadMillis += readIntervalMillis;
      // read the joystick
    potValue = analogRead(potPin1);
      // figure if a move is required
    if (potValue1 > potCentre + potDeadRange) {
      servoPos1 += servoMove1;
    }
    if (potValue1 < potCentre1 - potDeadRange1) {
      servoPos1 -= servoMove1;
    }
      // check that the values are within limits
    if (servoPos1 > servoMax1) {
      servoPos1 = servoMax1;
    }
    if (servoPos1 < servoMin1) {
      servoPos1 = servoMin1;
    }
     if (curMillis - lastReadMillis >= readIntervalMillis) {
    lastReadMillis += readIntervalMillis;
      // read the joystick
    potValue = analogRead(potPin2);
      // figure if a move is required
    if (potValue2 > potCentre2 + potDeadRange2) {
      servoPos2 += servoMove2;
    }
    if (potValue2 < potCentre2 - potDeadRange2) {
      servoPos2 -= servoMove2;
    }
      // check that the values are within limits
    if (servoPos2 > servoMax2) {
      servoPos2 = servoMax2;
    }
    if (servoPos2 < servoMin2) {
      servoPos2 = servoMin2;
    }
  }
}

void moveServo() {
  myServo1.write(servoPos1);
  myServo2.write(servoPos2);
}

void showPosition() {
  Serial.print("PotValue1 ");
  Serial.print(potValue1);
  Serial.print("   ServoPos1 ");
  Serial.println(servoPos1);
  Serial.print("PotValue2 ");
  Serial.print(potValue2);
  Serial.print("   ServoPos2 ");
  Serial.println(servoPos2);
}

I think this this is it

#include <Servo.h>

byte servoPin1 = 6;  // servo signal connection
byte servoPin2 = 9;
Servo myServo1;
Servo myServo2;
int servoPos1 = 90;
int servoPos2 = 90;
int servoMax1 = 130;
int servoMax2 = 130;
int servoMin1 = 45;
int servoMin2 = 45;
int servoMove1 = 3;  // number of degrees per movement
int servoMove2 = 3;

byte potPin1 = A0;   // center pin of joystick or potentiometer connected here
byte potPin2 = A1;
int potValue1 = 0;
int potValue2 = 0;
int potCentre1 = 512; // adjust to suit your joystick
int potCentre2 = 512;
int potDeadRange1 = 50; // movements by this much either side of centre are ignored
int potDeadRange2 = 50;

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

void setup() {
  Serial.begin(9600);
  Serial.println("Starting JoystickServo.ino");
  
  myServo1.attach(servoPin1);
  myServo1.write(servoPos1);
  myServo2.attach(servoPin2);
  myServo2.write(servoPos2);
}

void loop() {

void readJoystick();
      // check the time
  if (curMillis - lastReadMillis >= readIntervalMillis); 
    lastReadMillis += readIntervalMillis;
      // read the joystick
    potValue1 = analogRead(potPin1);
      // figure if a move is required
    if (potValue1 > potCentre1 + potDeadRange1) {
      servoPos1 += servoMove1;
    }
    if (potValue1 < potCentre1 - potDeadRange1) {
      servoPos1 -= servoMove1;
    }
      // check that the values are within limits
    if (servoPos1 > servoMax1) {
      servoPos1 = servoMax1;
    }
    if (servoPos1 < servoMin1) {
      servoPos1 = servoMin1;
    }
     if (curMillis - lastReadMillis >= readIntervalMillis) {
    lastReadMillis += readIntervalMillis;
      // read the joystick
    potValue2 = analogRead(potPin2);
      // figure if a move is required
    if (potValue2 > potCentre2 + potDeadRange2) {
      servoPos2 += servoMove2;
    }
    if (potValue2 < potCentre2 - potDeadRange2) {
      servoPos2 -= servoMove2;
    }
      // check that the values are within limits
    if (servoPos2 > servoMax2) {
      servoPos2 = servoMax2;
    }
    if (servoPos2 < servoMin2) {
      servoPos2 = servoMin2;
    }
  }
}

void moveServo() {
  myServo1.write(servoPos1);
  myServo2.write(servoPos2);
}

void showPosition() {
  Serial.print("PotValue1 ");
  Serial.print(potValue1);
  Serial.print("   ServoPos1 ");
  Serial.println(servoPos1);
  Serial.print("PotValue2 ");
  Serial.print(potValue2);
  Serial.print("   ServoPos2 ");
  Serial.println(servoPos2);
}

  void readJoystick();Why is this line in the program ?

void loop() {

void readJoystick();

You are not allowed to define a function inside another function.

...R

....but you are allowed to put a function prototype there.

Whether it makes sense to do so is another matter.

Amen.