Pages: [1]   Go Down
Author Topic: precisiOn servo relay control  (Read 412 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
-Bagder

THE CODE:
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){
    pulse=500;
  }else{
    pulse=1500;
  }
    if(counter<destination){
  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
  }
  counter++;
  }
  counter=0;  
  
  // THIS CODE TO BE REPLACED BY KEYPRESS CODE AT SOME POINT
  val=digitalRead(inPin);
  if (val==1){
    destination=2400;
  } else {
    destination=400;
  }
  Serial.println(destination);
}
Logged

Pages: [1]   Go Up
Jump to: