Hi , new to all this , worked on this code to operate three servos through a joystick and for the most part its all good. But I need to control the speed at which these servos travel preferably at a slow speed. Prefer a motion similar to the sweep (0-180). Can anyone guide me on this.
sketch_Analog_stick.ino (1.03 KB)
Edit#2: Go ahead and read the rest on my post, but I made some guesses on what you are seeing and what you are trying to do.
Rather than rely on my guesses, I want you to better describe the situation for us.
Tell us what you expect the sketch to do.
Tell us what the sketch actually does an how that differs from your expectations.
Your sketch confuses me.
Here it is posted using code tags so we can all see it without downloading. (read through the "How to use this forum - please read." post for more tips)
#include <Servo.h>
//deine our servos
Servo servoR;
Servo servoL;
Servo servoR2;
// define Joystick pins (Aanlog)
int joyY = 0;
int joyX = 1;
// variable to read the values rom the analog pins
int joyVal;
void setup()
{
// attaches our servos on pin 2-5
servoR.attach(2);
servoL.attach(3);
servoR2.attach(4);
}
void loop()
{
// read the values of servos (between 0-1023)
joyVal = analogRead(joyY);
joyVal = analogRead(joyX);
joyVal = map (joyVal, 0, 1023, 25, 220); // sevo value between 0-180
servoR.write(joyVal); // set the servo position according to the joystick value
delay (15);
joyVal = analogRead(joyY);
joyVal = analogRead(joyX);
joyVal = map (joyVal, 0, 1023, 10, 185); // sevo value between 0-180
servoL.write(joyVal); // set the servo position according to the joystick value
delay (15);
joyVal = analogRead(joyY);
joyVal = map (joyVal, 0, 1023, 10, 280); // sevo value between 0-180
servoR2.write(joyVal); // set the servo position according to the joystick value
delay (60);
}
You read joyY and then throw it away by immediately reading joyX. Why? Remove the first read.
Edit: Better yet, Read both joyX and joyY into different variables, once at the top of the loop, and then decide what to do with the values.
As I read this, 2 servos follow joyX and one follows joyY. Is that right?
And you want the servos to move slower. Right?
How much slower? They follow the joystick. If you move the joystick slowly, the servo moves slowly, right?
And a fast joystick move creates a fast servo move, right?
What do you want to change? Cap the speed to some maximum?
I'm not clear exactly what you're trying to achieve but you might want to have a look at VarSpeedServo.h instead of Servo.h. That allows you to associate a speed parameter with your servo writes.
Steve
Right on all guesses. I have two servos following a 0-90 degree path and one 0-180. Yes when I move the stick its movement corresponds, slow but is jerky. What I want is a smoother transition between the angles.
Is it possible to cap the speed by some parameter if so what command can I use. Also fixed the code. now two motors are on X and one on Y.
Thank you Steve for mentioning VarSpeedServo.h , I will do some digging around.
#include <Servo.h>
//deine our servos
Servo servoR;
Servo servoL;
Servo servoR2;
// define Joystick pins (Aanlog)
int joyY = 0;
int joyX = 1;
// variable to read the values rom the analog pins
int joyVal;
void setup()
{
// attaches our servos on pin 2-5
servoR.attach(2);
servoL.attach(3);
servoR2.attach(4);
}
void loop()
{
// read the values of servos (between 0-1023)
joyVal = analogRead(joyX);
joyVal = map (joyVal, 0, 1023, 25, 220); // sevo value between 0-180
servoR.write(joyVal); // set the servo position according to the joystick value
delay (15);
joyVal = map (joyVal, 0, 1023, 10, 185); // sevo value between 0-180
servoL.write(joyVal); // set the servo position according to the joystick value
delay (15);
joyVal = analogRead(joyY);
joyVal = map (joyVal, 0, 1023, 10, 280); // sevo value between 0-180
servoR2.write(joyVal); // set the servo position according to the joystick value
delay (15);
}
If it were me, I would get rid of the delays.
I would use millis as is done in Robin2's Several things at the same time tutorial.
On each instance where the proper number if millis have passed, read the joysticks.
Map the joystick readings into servo commands.
If the difference between the last servo command and the new one is more than your capped speed, change the new servo command to be the old command + the capped value.
Send to command to the servo.