element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum ~ ANSWERED ~ If this is a repeat, my apologies - 4 servos all turning at once with one potentiometer...This is NOT the way the project is to work.  Please help
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 67 replies
  • Answers 3 answers
  • Subscribers 406 subscribers
  • Views 4425 views
  • Users 0 members are here
  • answered
Related

~ ANSWERED ~ If this is a repeat, my apologies - 4 servos all turning at once with one potentiometer...This is NOT the way the project is to work.  Please help

Former Member
Former Member over 12 years ago

Hello all,

I am working on a project with 4 servos, and 4 potentiometers.

I will include my coding momentarily.  Here is my dilemma.

As the title states: "4 servos all turning at once with one potentiometer...This is NOT the way the project is to work."

The project is to turn each servo unit with its own potentiometer (dial style).  I have compiled my coding and it all comes out ok, I then upload it with no issue.

I then attempt to turn the pot and all 4 turn, and if I may add, with quite a bit of jitterness.

Can you please take a moment, look at the code and advise where you think the problem is.

This code was the KNOB example from the Arduino UNO example library.  I removed the name of the person as I have continued to add to the program.

You will see the things I have added as I have notated them.

 

Thanks in advance,

Anna

 

Now the code:

 

#include <Servo.h>

Servo myservo0;  // create servo object to control a servo
Servo myservo1;  // i added
Servo myservo2;  // i added
Servo myservo3;  // i added

 

int potpin0 = 0;  // analog pin used to connect the potentiometer (i added the zero to the potpin before the equal sign)
int val0;    // variable to read the value from the analog pin

int potpin1 = 1;  // i added
int val1;         // i added

 

int potpin2 = 2;  // i added
int val2;         // i added 

 

int potpin3 = 3;  //  i added
int val3;         //  i added


void setup()
{
  myservo0.attach(3);  // attaches the servo on pin 3 to the servo object --- I changed it to 3 from previously 9 - Anna
  myservo1.attach(5);  // i added
  myservo2.attach(6);  // i added
  myservo3.attach(9);  // i added
}

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
  delay(15);                             // waits for the servo to get there

 

val1 = analogRead(potpin1);            // i added
  val1 = map(val1, 0, 1023, 0, 179);     // i added
  myservo1.write(val1);                  // i added 
  delay(15);

 

  val2 = analogRead(potpin2);            // i added
  val2 = map(val2, 0, 1023, 0, 179);     // i added
  myservo2.write(val2);                  // i added
  delay(15);

 

  val3 = analogRead(potpin3);            // i added
  val3 = map(val3, 0, 1023, 0, 179);     // i added
  myservo3.write(val3);                  // i added
  delay(15);
}

 

Message was edited by: Annasta Brandon My project question was answered, now on to part 2... http://www.element14.com/community/thread/24030

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 12 years ago in reply to billabott +2
    I had a look at this discussion last night, but unfortunately our IT department does some weird java filtering, so I can't reply. Thanks Bill for the invite. The suggestions made so far look correct, but…
  • terrydark
    terrydark over 12 years ago in reply to mcb1 +2
    Hi Just to confirm Mark's statements... "The Arduino reference has mixed messages regarding the name to use for the Analogue inputs. A0, 0 all seem to work BUT it has to know its doing an analogue reading…
  • Former Member
    Former Member over 12 years ago +1
    Annasta, Did you intend for server0 and potpin3 to both use pin 3?
Parents
  • billabott
    0 billabott over 12 years ago

    @Anna

     

    Do you have an adequate separate power supply for the servos?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to billabott

    Billabott -

    Yes.  I have a seperate power 5 volt power supply with common ground.

    Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to Former Member

    @Chris, so your basically saying if I dont have a pot connected to the other 3 areas the unused power will flow out to the other PWM connections and activate the servos.

    Kind of like, a flood - once the water reaches an impass, it moves to an open area....oh dah...the path of least resistance image

    ~Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 12 years ago in reply to Former Member

    Hi Annasta,

     

    That might be the issue then.

     

    The official word from Arduino.cc is:

    "Note

    If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

    "

     

    (see http://arduino.cc/en/Reference/AnalogRead)

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to ntewinkel

    @Nico -

    I was really pondering the suggestion that Tony gave (not that anyone elses suggestions are not being taken into consideration), but being that I know how to run cat5 cable, for those that still wish to use hard wire computer connections, the number 1 rule for me is that if there are florecent lights - do not run the wires near them.  I tend to stay at least 12" (.30 meters) away from them.  That way the electric forces do not impede the connection speeds.

    ~Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 12 years ago in reply to Former Member

    Hi Anna,

     

    I was actually replying to the bit about not having anything connected to the other analog inputs (your recent reply to Chris Marshall) - according to the "Note" the value returned from AnalogRead of unconnected pins is unpredictable and likely based on the values of the analog inputs that you do have connected.

    So it's not that unconnected pins cause power to flow to PWM pins to activate the servos, it's that the unconnected analog pins are giving incorrect values, which then is interpreted by your code to activate the servos.

     

    So it's likely that turning the pot on the one input will cause AnalogRead on the not-connected pins to return values based on the one pot that is connected.

     

    Not sure if you had already cleared that up though (by hooking the unused *edit: I mean used, but not connected to pots* analog pins to ground, for example).

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to ntewinkel

    So both of you gentlemen are stating i need to take the anolog pins out of the bb and connect them to the ground row?

     

    ~Anna

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 12 years ago in reply to Former Member

    Hi Anna,

     

    Yes, input pins should always be electrically connected to something.

     

    For testing you could connect them to GND to always give a zero value input.

     

    Then add the other 3 pots once you know that part works.

     

    Or if you want to test with just one pot, just comment out the chunk of code that handles reading and setting pots and servos for the other 3 sets so that you're not reading from not-connected pins.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to ntewinkel

    Your advice is technically correct, Nico.  But, when were the inputs left floating?  That was not the original stated condition.  The impression I had was that 4 servos were connected and 4 pots were connected as specified in the sketch.

     

    @Anna

    The reuse of pin 5 is an issue, still.

    int potpin3 = 5;    // i added
    int val3;               // i added

     

    void setup()

    {

      myservo0.attach(5);    

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to Former Member

    Anna,

    I've been out for the evening and it looks like things may have moved on a bit, but what I originally meant to say was that if you do not connect a suitable driving signal to an active analog input the readings you get will be unpredictable, as has been stated now by others.

    Because of the internal workings of the A/D converter it is likely that this will bear some relationship to other inputs that are being driven correctly. Your code then takes these readings and applies them to the servos -after conversion to the appropriate PWM. To get all four inputs working predictably they must all be connected to a suitable signal, either from four independant pots, or for test purposes connect three of them to a fixed voltage, ground would be fine, or better still the expected mid point from a pot which can be simulated with a couple of resistors- also suggested. Only when you think you have all four inputs correctly driven and still getting cross coupling should you start looking for shorts or suspecting your code. If you have a DVM measure the voltages at the four inputs. You want the situation to be that when you move the pot only one input changes voltage, the other three remain the same. Until you have that situation you can't start to think about whether the code is right or not. If it comes down to the code- which I doubt it will- then others can help you better. I have used AVR's in commercial designs of my own but the code was written in pure C. I don't have an Arduino so I'm not familiar with language and patrticularly the special functions available.

     

    (Apologies for not referencing those who have now suggested some of these things already- It would get kinda messy)

     

    Chris

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to John_H

    @Mark

     

    Do you know what exactly is the problem with the UNO ADC?  timing?  proximity?  I don't get it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to billabott

    William

    No sorry I don't.

    It's proably something that either didn't get tested, or they don't bother with.

    After all why be interested in the adc reading if the pin is floating?

     

    As an ADC input you don't use the pullup resistor (you can but you get modified results) so the input will be VERY high impedance.

     

    Never the less I've learnt something for the future, and I'm sure it will come up again, so hopefully we can be quick to spot it.

     

    Someone else who is more capable (and has more time than me) can probably work out why, BUT can we fix it.??

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • mcb1
    0 mcb1 over 12 years ago in reply to billabott

    William

    No sorry I don't.

    It's proably something that either didn't get tested, or they don't bother with.

    After all why be interested in the adc reading if the pin is floating?

     

    As an ADC input you don't use the pullup resistor (you can but you get modified results) so the input will be VERY high impedance.

     

    Never the less I've learnt something for the future, and I'm sure it will come up again, so hopefully we can be quick to spot it.

     

    Someone else who is more capable (and has more time than me) can probably work out why, BUT can we fix it.??

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube