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