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 Servos and coding
  • 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
  • Replies 4 replies
  • Subscribers 402 subscribers
  • Views 531 views
  • Users 0 members are here
  • coding
  • tutorial
  • maker
  • arduino_month
  • openhardware
  • rotation
  • help
  • hardware
  • animatronic
  • robots
  • delay
  • robot
  • make
  • led
  • Design
  • servo
  • prototyping
  • code
  • platform
  • arduino_development_environment
  • engineering
  • arm
  • electrical
  • motor
  • arduino
  • hand
  • opensource
Related

Servos and coding

dirtdiver
dirtdiver over 14 years ago

Hi, so I'm new to the Arduino, I've done only one project so far and now -with my second..I ran into some trouble-

 

I am tring to make my robot arm do  something repeatedly - like a Factory robotic arm - assembling stuff.The problem is that I have 2 servos on 1 axis and I cant make them rotate at the same direction. here's the code I made (I used the library)

 

void loop()
{
  for(pos = 0; pos < 180; pos += 1)

  {                                  
    myservo.write(pos);
    myservo1.write(pos);           
    delay(15);                     
  }

}

 

how to reverse one of them?

 

also this wont work at all :

 


void loop()
{
 

  myservo.write(90);

   
  myservo1.write(90);
    
  myservo.write(0);
 
  myservo1.write(0);
 
}

 

and when I add the delay it works but not at the same time.

 

Thanks in advance!

  • Sign in to reply
  • Cancel
Parents
  • cookieglitch
    cookieglitch over 14 years ago

    Reversing them is just a matter of switching the values around and decrementing rather than incrementing. As such, it will look something like:

     

     

    void loop ()
    {
         for(int pos=180; pos > 0; pos--)
         {
              myservo.write(pos);
              myservo1.write(pos);
              delay(15);
         }
    }

     

    For your other piece of code, it may be worth inserting a delay between each statement. Telling a servo to move to a position takes a small amount of time and as a result will not happen at the same time, there will always be a little delay between the two. This is why on the example code, it has delay(15) after each write.

     

    Hope this helps!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dirtdiver
    dirtdiver over 14 years ago in reply to cookieglitch

    well I want the servos doing this:

     

    servo 1 - from 0 to 180

    servo 2 -from 180 to 0

    no delay between them

     

    what would that look like?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • cookieglitch
    cookieglitch over 14 years ago in reply to dirtdiver

    I would use something like this. Having no delay is relatively hard to do due to the write delays. PWM sadly isn't an instant thing. The best you can hope for (Without having a fancy distributed system) is reducing it to a tiny display that you won't notice too much.

     

    void loop()
    {
         int pos = 0; //Number of times the loop has run
         int serPos1 = 0; //Position of servo1
         int serPos2 = 180; //Position of servo2
    
         while(pos < 180)              //While the loop has been done less than 180 times...
         {
              myservo.write(serPos1);
              myservo1.write(serPos2);
              serPos1++;               //Increment position of servo1
              serPos2--;               //Decrement position of servo2
              pos++;                   //Increment loop counter.
    
              delay(15);               //Write delay. Can always try without, but no guarantees it would work
         }
    }

     

    This is by no means the only way to do it, but should give you an idea.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • dirtdiver
    dirtdiver over 14 years ago in reply to cookieglitch

    Thanks for the idea! I will try that.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • dirtdiver
    dirtdiver over 14 years ago in reply to cookieglitch

    Thanks for the idea! I will try that.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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