Go Down

Topic: Is this possible? (Read 335 times) previous topic - next topic

davida1234

Hi everybody,

Total new to Arduino and forums.

My goal is to drive two rc servos (#1 and #2) with one cable connected pot to Arduino.

Let's say I call the servo positions as -max (one end of travel), 0 (center) and +max (other end of travel).

At the middle position of the pot (wiper in the middle), servo #1 is at 0 (middle) and servo#2 is -max (one end) position.

When I move the pot about +20 % in one direction, servo #1 moves to +max, servo #2 does nothing and stays in -max position.

When I move the pot about -20 % in the other direction, servo #1 moves to -max, servo #2 still does nothing and stays in -max position.

When I move the pot beyond 20% of range in either direction, servo #1 stays at the +/- max positions, but servo #2 starts to move proportionally from -max to +max.

This is for water tank measurements of a prototype model gas boat with forward/back transmission, where you move a single lever control a little forward or backward to engage the transmission forward or backward while the engine is running at idle and when you push the stick further forward or backward then the engine throttle gets opened up.

Thank you for your help,
David A

mem

#1
Oct 01, 2009, 08:58 pm Last Edit: Oct 01, 2009, 09:03 pm by mem Reason: 1
I think this may be close to what you want:

Code: [Select]
#include <Servo.h>

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

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

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

}


void loop()
{
 val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)  
 Servo1Pos = map(val, 460, 661, 0, 180);     // scale plus/minus 20%  0 and 180
 if(val < 460)  
   Servo2Pos = map(val, 0, 460, 0, 89);
 else if( val > 661)
   Servo2Pos = map(val, 661, 1023, 91, 180);  
 myservo1.write(val);                  // sets the servo position according to the scaled value
 myservo2.write(val);
 delay(20);                           // Servo updates every 20ms
}


The servo library constrains the value written between 0 and 180 so its ok to give it out of range values (as will happen with servo1Pos).
You may need to adjust the actual physical movement using the attach method,  not all servos can actually move 180 degrees.

Have fun  

Go Up