Arduino Knob using to 2 Encoder Sensors and 2 servo motors

I’m new to Arduino. Sorry If I am asking silly question! :wink:

I want to control 2 servo motors using 2 Encoder Sensor Potentiometers .
The Servo motors should move with respect to Encoder Sensor Knob . The project is very similar to the Knob example that’s given in Arduino Library . But it only shows how to control one Servo , My aim is to control 2 with 2 separate knob switches. Any help will be appreciated ! Thanks

The code I found in the Library is as follows :-
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott

modified on 8 Nov 2013
by Scott Fitzgerald


#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

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

void loop()
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 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

What is stopping you?

Please read the two posts at the top of this Forum by Nick Gammon on posting guidelines, especially when posting source code using code tags. Also, I'm not exactly sure what this means:

...2 servo motors using 2 Encoder Sensor Potentiometers .

Encoders and potentiometers are two different things. Your code suggests using pots, not encoders. Is that correct?

@ Econjack
Thank you for replying

Here is the example what I want to do but this guy has used only one servo , I want to use two servos . I am not sure but I think he is using Rotary sensor. Kindly check the below Youtube link . I am unable to understand his language so not able to make out what he is saying.

@Delta G . I dont’t know what changes should I make to code , What lines to add ?


Shakoor: @Delta G . I dont't know what changes should I make to code , What lines to add ?

OK, the first thing you need to do is to learn to understand the code you've got here. Once you understand what each line is doing, changing it to do so twice will be trivially easy. As long as you are seeing "magic words" on the screen, then you aren't ready yet.

So let's focus on understanding the code for one servo. What there is confusing you?

My understanding of German is close to zero, but in the video it appears he is wiring a pot, not an encoder, plus the wiper is being connected to an analog pin which supports the idea that it's a pot. What this means is that your two pots need to work through the analog pins. Also, pin 0 on the Arduino is normally used for Serial object communications. Are you sure the code doesn't attach the potpin to A0, not 0?

int potpin = 0;

This is OK so long as you only use it for :

val = analogRead(potpin);

The code in analogRead will add 14 (or whatever depending on the board) to the pin number you pass to it if the number is smaller than the first analog pin.

I like using A0 better too as it makes it more obvious what you mean. But the way he has it is technically correct.

@Delta_G: You're right. The analogRead() makes it associate to the correct pin.