adafruit / Adafruit-PWM-Servo-Driver-Library

Adafruit PWM Servo Driver Library
Other
477 stars 311 forks source link

Fast-Continuous-Servo Example-Code #79

Closed ghost closed 4 years ago

ghost commented 4 years ago

/ Example-Code for Fast Continuous-Servos /

include

include

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

define USMIN 600

define USMAX 2400

define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

// All Servos on my board

uint8_t Servo1 = 0; uint8_t Servo2 = 1; uint8_t Servo3 = 2; uint8_t Servo4 = 3;

//Turn Forward and Backwards

uint16_t Forward = USMIN; uint16_t Backward = USMAX;

void setup() { Serial.begin(9600); Serial.println("8 channel Servo test!");

pwm.begin();

pwm.setOscillatorFrequency(27000000); pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates

delay(10); }

// You can use this function if you'd like to set the pulse length in seconds // e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise! void setServoPulse(uint8_t n, double pulse) { double pulselength;

pulselength = 1000000; // 1,000,000 us per second pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates Serial.print(pulselength); Serial.println(" us per period"); pulselength /= 4096; // 12 bits of resolution Serial.print(pulselength); Serial.println(" us per bit"); pulse *= 1000000; // convert input seconds to us pulse /= pulselength; Serial.println(pulse); pwm.setPWM(n, 0, pulse); }

void loop() {

pwm.writeMicroseconds(Servo1,Forward);
pwm.writeMicroseconds(Servo2,Forward);
pwm.writeMicroseconds(Servo3,Forward);
pwm.writeMicroseconds(Servo4,Forward);
delay(5000);

pwm.writeMicroseconds(Servo1, Backward);
pwm.writeMicroseconds(Servo2,Backward);
pwm.writeMicroseconds(Servo3,Backward);
pwm.writeMicroseconds(Servo4, Backward);
  delay(5000);

}