Hello everyone. I am following this servo arm tutorial video for a school project. I have little to no knowledge about Arduino, let alone coding for it. Right now, my project looks exactly like the video, but I want to add a second joystick for a more precise control of the arm. So one joystick will be controlling the up-down movement and another controlling left-right. How can I achieve this?
I have attached the servos and joysticks to the pins in the image below. Thank you very much.
this sketch is from beginner. such joystick modules contain two potentiometers. if you connect an output of second joystick (or any other analog source) to A1 it works as you asked.
They way you use the servos, by angle, and the way you change their setting, limits your precision to one degree out of 180.
You could help the operator by limiting those steps to only being able to happen only a few times a second. Now they fire off so fast it is not possible easy to ,and on an exact degree.
Or, you could code the joystick to make only one step each time it went into the region where a step could be taken. Perhaps a mode could be controlled by a "fine tuning" button that when pressed woukd make the joysticks react to the change, rather than the condition.
See Files/Examlkes/02.Digital/StateChangeDetection on offer in the IDE, and google for the Arduino article on the subject.
If you switched to using pulse length in your servo calls, your range would be something like 1000..2000 microseconds, and might let a better servo reach a more exact position.
Either way, degrees or pukse length, will benefit from coding that allows single steps of whatever size to be easily ordered.
OK, so you are losing any help the joystick would be by treating it like multiple switches.
Get the joystick to increase or decrease the servo setting value, as you are now, but use its analog nature. I can't test the code form right here just now, so this may only be close:
float preciseValue = 90.0;
# define STEP 5 // fastest motion
void setup() {
Serial.begin(115200);
Serial.println("Joy to the World!\n);");
}
void loop() {
int jValue = analogRead(A0);
int increment = map(jValue, 0, 1023, -100, 100);
preciseValue += STEP * increment / 100.0;
if (preciseValue < 0.0) preciseValue = 0.0;
if (preciseValue > 180.0) preciseValue = 180.0;
int sValue = preciseValue;
Serial.print(jValue); Serial.print(" ");
Serial.print(increment); Serial.print(" ");
Serial.print(preciseValue); Serial.print(" ");
Serial.print(sValue); Serial.print(" ");
Serial.println("");
delay(50); // limit to 20 steps / second no matter
}
This maintains the intended servo position as a floating point number, and uses the joystick to slowly or rapidly accumulate movement in the direction desired.
Cheap joysticks often have a build-in deadband. If yours do not, you may need to code for one or your servo may creep one direction or another when the stick is not beong moved off center.