Hi to all!
i just need to control the speed of my continuos 360 servo, the joystick is good because i can control the movements left or right, but now i would like to add a potentiometer to control the speed! Could somebody help me adding the code to my sketch?
// Arduino 07 - Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------
#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3 //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2 //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1 //X axis reading from joystick will go into analog pin A1
Servo myservo ;
void setup()
{
Serial.begin(9600);
pinMode(VOUT_JOY_PIN, OUTPUT) ; //pin A3 shall be used as output
pinMode(GROUND_JOY_PIN, OUTPUT) ; //pin A2 shall be used as output
digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
myservo.attach(9);
}
void loop()
{
delay(300);
int joystickXVal = analogRead(XJOY_PIN) ; //read joystick input on pin A1
Serial.print(joystickXVal); //print the value from A1
Serial.println(" = input from joystick"); //print "=input from joystick" next to the value
Serial.print((joystickXVal+520)/10); //print a from A1 calculated, scaled value
Serial.println(" = output to servo"); //print "=output to servo" next to the value
Serial.println() ;
myservo.write(((joystickXVal+570)/10)-13); //write the calculated value to the servo
}
but when i touch the joystick the servo starts to go faster..i just need to reduce or modulate the speed..because some times could be ok to go faster, but in other occasions i need to go slower!
i tried to join your code to mine but doesn't work..i assigned A0 as potpin but nothing happen..can you please post me the complete code with your changes? thanksss
So show us what you tried, including a diagram of how everything is connected, and we'll help you make it work. But we don't usually just write the complete program for you...you'll never learn anything like that.
robyr1, I think there are some things that you are not picking up.
a Joystick is a potentiometer. You are already reading its value. You are already sending various values to the servo.
continuous rotation servos often have little speed control. This varies by make and model. Some have a little speed control, and many have virtually no speed control.
The best way to determine how your servo behaves is with the knob tutorial. You can even use your joystick as the pot in the tutorial.
thanks guys! with a simple potentiometer and without the joystick the servo starts to rotate itself but i can set the speed..the problem is with a joystick only, i don't know how to add the code of the potentiometer and tune it..
I posted my code above! with this code i can control the 360 servo with a joystick, my question about the code of PaoulS is: where shoud i have to add his code with mine? and also i don't understand wich pin pot i have to use!
You can modify that to show the range of values you get out of the pot.
Then you can run similar code to show the range of values you get out of the joystick.
Once you have those 2 ranges, you can change the values used in the map() function and your joystick should provide the same behavior as the pot.
robyr1:
I posted my code above! with this code i can control the 360 servo with a joystick, my question about the code of PaoulS is: where shoud i have to add his code with mine? and also i don't understand wich pin pot i have to use!
You have one line of code that controls the servo. Replace that one line with the 4 lines of code I posted.
Connect the pot to whichever analog pin makes your heart go pitter-patter. Just use that same pin number in the analogRead() call.
PaulS:
You have one line of code that controls the servo. Replace that one line with the 4 lines of code I posted.
Connect the pot to whichever analog pin makes your heart go pitter-patter. Just use that same pin number in the analogRead() call.
Thanks PaulS, tried to follow your suggestions, can you please take a look to this code?
// Arduino 07 - Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------
#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3 //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2 //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1 //X axis reading from joystick will go into analog pin A1
Servo myservo ;
void setup()
{
Serial.begin(9600);
pinMode(VOUT_JOY_PIN, OUTPUT) ; //pin A3 shall be used as output
pinMode(GROUND_JOY_PIN, OUTPUT) ; //pin A2 shall be used as output
digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
myservo.attach(9);
}
void loop()
{
delay(300);
int joystickXVal = analogRead(XJOY_PIN) ; //read joystick input on pin A1
Serial.print(joystickXVal); //print the value from A1
Serial.println(" = input from joystick"); //print "=input from joystick" next to the value
Serial.print((joystickXVal+520)/10); //print a from A1 calculated, scaled value
Serial.println(" = output to servo"); //print "=output to servo" next to the value
Serial.println() ;
int aVal = analogRead(A0);
float rat = aVal / 1023;
myservo.write((aVal*((joystickXVal+570)/10)-13));
}
Nothing! i tried to change into "rat", but i still have the same problem: the servo turns it self!
#include <Servo.h>
// Arduino 07 - Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------
#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3 //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2 //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1 //X axis reading from joystick will go into analog pin A1
Servo myservo ;
void setup()
{
Serial.begin(9600);
pinMode(VOUT_JOY_PIN, OUTPUT) ; //pin A3 shall be used as output
pinMode(GROUND_JOY_PIN, OUTPUT) ; //pin A2 shall be used as output
digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
myservo.attach(9);
}
void loop()
{
delay(300);
int joystickXVal = analogRead(XJOY_PIN) ; //read joystick input on pin A1
Serial.print(joystickXVal); //print the value from A1
Serial.println(" = input from joystick"); //print "=input from joystick" next to the value
Serial.print((joystickXVal+520)/10); //print a from A1 calculated, scaled value
Serial.println(" = output to servo"); //print "=output to servo" next to the value
Serial.println() ;
int aVal = analogRead(A0);
float rat = aVal / 1023;
myservo.write((rat*((joystickXVal+570)/10)-13));
}