Go Down

Topic: precisiOn servo relay control (Read 641 times) previous topic - next topic


Hi all,

I am trying to use the arduino for precision control of a servomotor. I'm a bit new to servo controlling so I am hacking up the sample servomotor code from the ITP. What I'd like to be able to do is to send the Arduino data (e.g. keypresses) via Processing to position the servomotor.

So for example, if I press 'a', I want to position the servo to 0' and if I press 'z' I want it to be at about '180'

However, I can't get my head around how to send the proper pulses to the motor to get it to go to different positions and stay there. I can get it to increment via a loop and can increase the increments, or go to positions based on digital in, but that is about it, and I've just been guessing. Also any hints on sending data to Arduino via Processing would be cool!

Any ideas would be really helpful! Thanks!!

int servoPin = 3;     // Control pin for servo motor
int pulse = 0;        // Amount to pulse the servo
int counter=0;
int destination;
long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int inPin = 7;
int val=0;

void setup() {
 pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
 pinMode(inPin, INPUT);
 Serial.begin(19200);  // FOR DIAGNOSING

void loop() {  
 if (destination<1400){
 if (millis() - lastPulse >= refreshTime) {
   digitalWrite(servoPin, HIGH);   // Turn the motor on
   delayMicroseconds(pulse);       // Length of the pulse sets the motor position
   digitalWrite(servoPin, LOW);    // Turn the motor off
   lastPulse = millis();           // save the time of the last pulse
 if (val==1){
 } else {

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131