element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
Arduino
  • Products
  • More
Arduino
Arduino Forum Servos and coding
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 4 replies
  • Subscribers 117 subscribers
  • Views 105 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 11 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!

  • Reply
  • Cancel
  • Cancel
  • cookieglitch
    cookieglitch over 11 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
    • Up 0 Down
    • Reply
    • Cancel
  • dirtdiver
    dirtdiver over 11 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
    • Up 0 Down
    • Reply
    • Cancel
  • cookieglitch
    cookieglitch over 11 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
    • Up 0 Down
    • Reply
    • Cancel
  • dirtdiver
    dirtdiver over 11 years ago in reply to cookieglitch

    Thanks for the idea! I will try that.

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
Element14

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 © 2022 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

  • Facebook
  • Twitter
  • linkedin
  • YouTube