Connect with us

Problem with sketch controlling two servos

Discussion in 'Microcontrollers, Programming and IoT' started by bigone5500, Oct 19, 2014.

Scroll to continue with content
  1. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I have modified a sketch to attempt to control 2 servos using separate pots. The servos just jitter around and only one will move when both are connected. If I disconnect the one attached to A0, then they both move simultaneously.

    Code:
    // Controlling a servo position using a potentiometer (variable resistor)
    // by Michal Rinott 
    
    #include <Servo.h>
    Servo myservo0;  // create servo object to control a servo
    Servo myservo1;
    int potpin0 = 0;  // analog pin used to connect the potentiometer
    int val0;
    int potpin1 = 1;
    int val1;  // variable to read the value from the analog pin
    void setup()
    {
    myservo0.attach(9);  // attaches the servo on pin 9 to the servo object
    myservo1.attach(10);
    }
    void loop()
    {
    val0 = analogRead(potpin0);  // reads the value of the potentiometer (value between 0 and 1023)
    val0 = map(val0, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
    myservo0.write(val0);  // sets the servo position according to the scaled value
    val1 = analogRead(potpin1);
    val1 = map(val1, 0, 1023, 0, 179);
    myservo1.write(val1);
    delay(15);
    }
    
    
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,305
    2,738
    Jan 21, 2010
    Is this a reasonable translation of what you're seeing?
     
  3. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    That's pretty much what I said...in so many words.
     
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,305
    2,738
    Jan 21, 2010
    I thought that was the reasonable translation. You could have meant that if you disconnect one of the servos both servos move together.

    What I suggest you do is to try with one pot disconnected and it's input (analog 0) tied to ground.

    Then try with the other pot disconnected and it's input (analog 1) tied to ground.

    What is the behaviour with both?

    Are you powering the servos from the same power supply as the arduino?
     
  5. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    After some in-depth troubleshooting, it turns out that one of my potentiometers is bad. I replaced it with a good one and the sketch works just fine...except for when you turn the pot all the way or turn it slowly, the servo that you do not want moving jitters. This is a step in the right direction I think.
     
    Last edited: Oct 20, 2014
  6. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    I would freak out if that happened...LOL!
     
  7. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,305
    2,738
    Jan 21, 2010
    Yeah, but imagine if you had some sort if short between the two servos that somehow linked their control connection. The behaviour you see would be entirely consistent with that assuming the alternate reading of your text.

    Whilst I considered that to be extremely unlikely, I wanted to rule it out. It's important that the problem I think you have is the same one that you actually have :D
    Hahaha, it's sometimes the simplest of things. I wouldn't have suggested a faulty pot high up on my list of possibilities.

    Congrats on getting the main problem resolved.

    It sounds like it's still not behaving exactly like you expect though. Is that true?
     
  8. bigone5500

    bigone5500

    712
    120
    Apr 9, 2014
    Yes, it is true. When the pots are not being turned, the servos do not move...which is great. But when you turn either one, be it slowly or fast, then the servo opposite the one you are controlling will have a bit of motion to it. Although it is just a slight motion of maybe 2 to 3 degrees in either direction. I am in the process of getting a video of the servos on here...stay tuned.
     
  9. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,305
    2,738
    Jan 21, 2010
    It sounds like it could be due to interference. Try placing 0.1uF caps across the power connections to the servos.
     
  10. Harald Kapp

    Harald Kapp Moderator Moderator

    9,667
    2,019
    Nov 17, 2011
    It says here:
    Try changing your loop to:
    Code:
    void loop()
    {
    delay(7); // split 15ms delay into 2*7ms delay before each analog read
    val0 = analogRead(potpin0);  // reads the value of the potentiometer (value between 0 and 1023)
    val0 = map(val0, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
    myservo0.write(val0);  // sets the servo position according to the scaled value
    
    delay(7); // split 15ms delay into 2*7ms delay before each analog read
    val1 = analogRead(potpin1);
    val1 = map(val1, 0, 1023, 0, 179);
    myservo1.write(val1);
    }
    
    Another means of reducing noise on the analog measurement would be using some averaging. Your delay statements show that you have ample headroom for that. E.g. like this:
    Code:
    delay(7); // split 15ms delay into 2*7ms delay before each analog read
    val0 = analogRead(potpin0);
    val0 += analogRead(potpin0);
    val0 += analogRead(potpin0);
    val0 += analogRead(potpin0);
    val0 = val0 >>2; // divide by 4 to average over 4 samples
    
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-