 # Arduino help for a novice :(

Hey guys, i really, really need help right now !!

I’ve been trying to figure out how to make a code so that my analog input (joystick) can control a continuous rotation servo. But … the trick is doing this for two servos. So two joysticks, one controls one controller’s speed and direction, and the other joystick does the same for the other joystick. Any help please i’ve searched around but everything that i have seen doesn’t include a joystick input in their codes. I would greeeeeeeeeeeeeatly appreciate it. You really dont know how much >.<

Do you have code to control one servo with one joystick? I'm not aware the arduino servo library can set servo speed (I've only used it once or twice). If you have code for one servo-joystick, post it (working or not).

yea !! I kinda put one together just now. The thing is, this is all “theoretical”. I dont have a continuous servo. Basically what i was asking was for a tested code that is working. The idea is to have it go reverse when the value is under 90 and forward when above. As far as i have it understood, the higher the value, the faster this goes. That’s how you get the speed control. Here’s what i have so far:

#include <Servo.h> // include a library that does all the hard work controlling the servo

Servo servo1; // declare servo #1
Servo servo2; // declare servo #2
int rightJS = 1; // input pin for vertical position
int leftPOS = 90; // initialize horizontal position variable to 90 degrees
int rightPOS = 90; // initialize vertical position variable to 90 degrees

void setup() {
Serial.begin(9600);
pinMode(horzIN, INPUT); // let Arduino know that this is an input
pinMode(vertIN, INPUT); // let Arduino know that this is an input
servo1.attach(10); // Use pin #10 for servo 1
servo2.attach(9); // Use pin #9 for servo 2
}

void loop() {
horzPos = analogRead(horzIN); // Horizontal joystick position value (between 0 and 1023)
vertPos = analogRead(vertIN); // Vertical joystick position value (between 0 and 1023)
horzPos = map(horzPos, 0,1023,0,180); // Map the values between 0 and 180 degrees
vertPos = map(vertPos, 0,1023,0,180);
Serial.print(horzPos); // Print out the values we just gathered via the serial port
Serial.print("\t");
Serial.println(vertPos);

servo1.write(horzPos); // Make Servo #1 go to angle defined by horizontal joystick position
servo2.write(vertPos); // Make Servo #2 go to angle defined by vertical joystick position

delay(15);
}

My understanding of a continuous rotation servo is that when you send it the "position" value (as you would have for a normal servo), that value is direction and position together. Normal servos go from 0 degrees to 180, with 90 in the middle. For continuous rotation, 90 means stop, 0 is full speed one way and 180 is full speed the other.

So it's really no different from the normal servo code in the servo knob tutorial: you control the continuous servo's speed and direction at the same time, just as if it was a normal servo's position.

Each axis of a joystick is just a potentiometer: so with an XY joystick you could control both servos from one joystick. Of course, you could use two if you want to, but you would only use half of each joystick: only one of the two axes.

Either way, you just duplicate the necessary parts of that linked code, changing and servo name and pin number where appropriate, for example:

``````Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
``````

might become:

``````Servo myservo1;  // create servo object to control a servo
Servo myservo2;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int val1;    // variable to read the value from the analog pin
int val2;

void setup()
{
myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
myservo2.attach(10)
}

// etc etc
``````

EDIT.... which is more or less what you posted meantime

huh, interesting. I'll write it both ways and see which one my professor likes best :D

thanks for the input !

Word of caution: I've never used a continuous servo, but from what I've read here the 90 degree value for stop is not necessarily precise. You may find yours varies a degree or two either side: maybe yours will be stopped at 88... time will tell.