Simple Joystick - Servo demo

Some time ago I helped someone to write code to control a servo with a joystick in such a way that the servo moves when you push the joystick forwards or backwards and stays in position when the joystick returns to the centre.

I cannot now find the Thread for that project so I thought it would be useful to make the following short demo program.

I don't actually have a joystick so I have tested the code with a potentiometer.
If someone finds that it does not work with a joystick, please let me know.

// 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 = 130;
int servoMin = 45;
int servoMove = 3;  // number of degrees per movement

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;
		}
	}
}

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

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

There are some variables that can be adjusted to change the speed of reaction and the limits of movement.

To keep things simple this code does NOT increase the speed of movement if the joystick is pushed further. It would not be difficult to make that happen.

...R

Hi

Thx for this code.

Can you show how to get it to increase the speed of movement if the joystick is pushed further?

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);
}

this one works, though needs tidying up

#include <Servo.h>

#define mainArm 0


Servo servo[1];      //code used for attaching upto 4 servos

byte angle[1] = {90};       // middle point for servo angle

byte potPin[4] = {A0, A1};  // input pins to attach your potentiometers
byte servoPin[1] = {3};    // input pins to attach servos

void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");


  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(5);
}

void readPotentiometers() {
  int potVal;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    
    if (potVal < 200) {         // dead zone for the joystick I used is 200 to 550.
      angle[n] += 1;
      if (angle[n] > 160) {
        angle[n] = 170;
      }
    }
    
    if (potVal > 550) {         // deadzone upper value
      angle[n] -= 1;
      if (angle[n] < 10) {
        angle[n] = 10;
      }
    }

  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].write(angle[n]);
  }
}

yerr i know, im pretty new at this. you are welcome to fix it up for me :smiley:

i ment the code i put in

brunokc:
Can you show how to get it to increase the speed of movement if the joystick is pushed further?

Jeez. I've just spent time answering that question in your own Thread.

Please don't mess up my tutorial Thread when you have your own Thread.

...R

To demonstrate exactly what i need to do, i just found this video on youtube while looking for something similar and that exactly what i want my servo to do

please check the link ( variable speed test continuous servo - YouTube )

I am going to respond to this in your own Thread

...R

Hi Robin2, could you possibly share a simple code like this but that uses two servos and a joystick that moves one of the two servos based on an x or y input? Thank you.

1 Like

amencurley:
Hi Robin2, could you possibly share a simple code like this but that uses two servos and a joystick that moves one of the two servos based on an x or y input? Thank you.

I don't have a program like that to share.

However if you start your own Thread and in it post the code that represents your best attempt I will try to help.

...R