Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HELP: Code for controlling a servomotor

Status
Not open for further replies.

AdrianB

Newbie level 1
Newbie level 1
Joined
Mar 19, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,293
Hi there!

I'm working on a home-made pingpong trainer. The last thing I need is to control my servomotor in that way that it can rotate 90° back and forth several times. Being completely new to electronics, I had no idea where to start. I've gathered information and the needed stuff.

My servomotor is a TowerPro MG995, my microcontroller is a PicKit2 , and I'm wanting to write a code in MPLab v.8.90 to control it. I've been looking around the internet for information, and I've written a first shot (see below).

First of all, would this code work, or is there something wrong in it (apologies for eventual stupid errors) ?
Second: I've written 20ms in the delay() function because I read a servo needs an impuls every 20ms, is this right ? Could someone eventually explain why ?

If there is a better or more efficient way to go, please tell me your knowledge !

Any help will be much appreciated !

Regards,

Adrian




The "servo.h" library is a free library I found on Arduino, including the functions I use here. "servo301" is the name I wanted to give it.
(https://code.google.com/p/arduino/source/browse/trunk/libraries/Servo/Servo.h?r=1088 )

Code:
// Besturingscode servomotor.

#include "servo.h"
 
Servo servo301;
 
int pos = 0;
 
void setup()
{ 
  servo301.attach(7);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 90; pos += 1)
  {                                 
    servo301.write(pos);      
    delay(20);                 
  } 
  for(pos = 90; pos>=1; pos-=1)   
  {                                
    servo301.write(pos);         
    delay(20);
  } 
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top