| 
View
 

servo motor change direction

Page history last edited by Kristin Lucas 17 years, 8 months ago

Go to an exact "randomly generated" spot in the rotation of your motor.

This code includes a Serial.print command.

 

int servoPin = 9;     // Control pin for servo motor

int minPulse = 500;   // Minimum servo position

int maxPulse = 2500;  // Maximum servo position

int pulse = 0;        // Amount to pulse the servo

long lastPulse = 0;    // the time in milliseconds of the last pulse

int refreshTime = 20; // the time needed in between pulses

int counter = 0;

 

void setup() {

 pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin

 pulse = minPulse;           // Set the motor position value to the minimum

 Serial.begin(9600); 

}

 

void loop() { 

 counter ++;

 if (counter > 10000){

   counter = 0;

   Serial.println(counter);

   pulse = random(500,2500);

 }

 // pulse the servo again if the refresh time (20 ms) have passed:

 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

 }

 

Comments (0)

You don't have permission to comment on this page.