Moving a Servo with an Analog Pot in the same direction

(deleted)

The number you put into the write(pos) is the POSITION that you want the servo to go to. Which way it moves will depend on where it starts from. Usually 0 is far left, 90 is centre and 180 is far right but some servos are the reverse of that.

Can you be a bit clearer about what you're aiming for? When joystick is positioned far left where should the servo be? When the joystick is in the centre where should the servo be? And when the joystick is over to the right?

Steve

(deleted)

You didn't answer the question. Are you saying that when you move the joystick right the servo should move right and when you move the joystick left the servo should still move to the right? How far should the servo move from its starting position?

Steve

(deleted)

Try something like

if (val > 500 && val <525) servo.write(0);

else if (val >= 525) 
{
pos = map(val,525, 1023, 0,180);
servo.write(pos);
}
else if (val <= 500)
{
pos = map(val, 500, 0, 0, 180);
servo.write(pos);
}

And make sure that you understand what it is doing because you'll probably need to adjust the values. Joysticks often don't cover the full range 0 to 1023 and may not centre very close to 512.

Steve

(deleted)

  1. You are not posting your code as code (use </> not the table entry)
  2. You have not posted any of the error messages you are getting.

But it's mainly because you haven't declared a variable pos. You have to do some of the thinking for yourself.

And you need to take out your initial map() and write(val). My (untested) suggestions were in place of them not as well as them.

Steve

(deleted)

Sorry but I am not just going to write your code for you. If you don't understand what I'm trying to show you then ask some questions. But don't just thrash about making random changes that you don't understand.

But honestly isn't it obvious that if you map "val" so it is between 0 and 180 then there's no point testing it to see if it's greater than 525. How can it be? It's between 0 and 180.

Steve

(deleted)

Some time ago I said -

slipstick:
And you need to take out your initial map() and write(val).

Did you understand this? You certainly haven't done it. If you didn't understand it why didn't you ask about it? Being new to programming is not a very good excuse for not trying to learn things.

Can you find the first line in your program with a map(val,.....) command in it? Can you find the first line with a servo.write(val) in it? Can you delete or comment out those two lines? Does the program do anything different now?

Steve

(deleted)

Try this; a modified version of the 'knob' example code included with the IDE. I haven't tested it (other than that it compiles ok) but the comments might help explain what it does. You could do the maths in fewer steps, but I left it like this to make it easier to follow...

#include <Servo.h>

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
int minimum = 1024; // minimum value seen since reset
int maximum = 0; // maximum value seen since reset
int range;  // half the difference between Maximum and Minimum

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

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  if (val < minimum) minimum = val;    // update minimum if a lower value has been read
  if (val > maximum) maximum = val;    // update maximum if a higer value has been read
  range = (maximum - minimum)/2;       // update the range from the centre point
  if (range > 0) {                     // to prevent div by zero errors 
    val = val - minimum;                 // zero correction
    val = val - range;                   // distance from centre
    val = abs(val);                      // absolute distance from centre 
    val = map(val, 0, range, 0, 180);    // scale it to use it with the servo (value between 0 and 180)
    myservo.write(val);                  // sets the servo position according to the scaled value
  }
  delay(15);                           // waits for the servo to get there
}

Move the pot through the full range to set the max and min. Until you do that the behaviour might be a little odd.

I've now tried this with a 10K Linear pot and it seems to work as expected. If your joystick centre position isn't in the middle then I'd write out the value from analogRead to the serial port, then hard code suitable values into the sketch.

(deleted)