Arduino Forum

Using Arduino => Motors, Mechanics, and Power => Topic started by: DrBwts on May 02, 2018, 03:18 pm

Title: Two pots controlling 2 servos — odd behavior
Post by: DrBwts on May 02, 2018, 03:18 pm
I'm trying to control 2 servos with 2 pots. I thought I could just amend the tutorial code by adding in the extra pot on a1 and extra servo on d8 (am using a Nano).

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

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

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

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

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val_2 = analogRead(potpin_2);
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  val_2 = map(val_2, 0, 1023, 0, 180);
  myservo.write(val);                  // sets the servo position according to the scaled value
  myservo_2.write(val_2);
  delay(15);                           // waits for the servo to get there
}



...but the servos only respond to one of the pots (on a0) & both move to the same position regardless of what value is on a1.

Am I missing something really obvious? Why is the value on a1 ignored & why is the value mapped from a0 sent to both d8 & d9?

I have swapped out the pot on a1 just in case it was faulty but I get the same behavior.
Title: Re: Two pots controlling 2 servos — odd behavior
Post by: bos1714 on May 02, 2018, 06:12 pm
Hi there!

I looked up the tech specs for the Nano board you are using. It says that out of the 22 digital pins, six of them can use to send a PWM signal, which is a way to get an analog voltage out of a digital pin. With most Arduino boards that have six of these PWM pins, such as your Nano, pins 3, 5, 6, 9, 10, and 11 are the six pins listed as PWM pins. I see in your code that you have pin 8 attached to your second servo, which is not a PWM pin. Instead of pin 8, try again using pin 6 and let me know.

As for why the second potentiometer value is not being used as you want, I'm not entirely sure yet.

Can you also upload a picture of your setup with wirings and everything?

Thanks
Title: Re: Two pots controlling 2 servos — odd behavior
Post by: slipstick on May 02, 2018, 08:08 pm
If you had looked up the specification of the Servo library you would have seen that it does not use or need PWM pins. It works with any digital pin. Servos are not driven with conventional PWM.

The code looks fine to me. Either that's not the code that is loaded or the wiring is wrong.

Steve
Title: Re: Two pots controlling 2 servos — odd behavior
Post by: Tofer on May 02, 2018, 08:19 pm
Just use another var like: servocontrol2 = map(val_2, 0, 1023, 0, 180);

And please show your scheme.
Title: Re: Two pots controlling 2 servos — odd behavior
Post by: vinceherman on May 02, 2018, 08:20 pm
Print out the values of val and val_2 to see what the results of the analogRead are.