demonstration program for the library created for PCA9685 PWM controller

Dependencies:   PCA9685PWM mbed

main.cpp

Committer:
dreamworker
Date:
2015-07-24
Revision:
1:9fd2d2f5184e
Parent:
0:305237ecbbe5

File content as of revision 1:9fd2d2f5184e:

/*PCA9685 library example code
 *test library performed with two servomotors, attached in channel 0 and 1,  that rotate to the right and to the left
 *servo in chanel 0: Tower Pro SG90
 *servo in chanel 1:Goteck GS9025 MG
 */ 
#include"PCA9685.h"
#include"mbed.h"



PCA9685 pwm(D14,D15);
 
void setServoPulse(uint8_t n, float pulse) {
    pwm.setPWM(n, 0, pulse);
}
 
void initServoDriver() {
    pwm.begin();
    pwm.setPrescale(121);     // set 20ms for generic servos
    pwm.frequencyI2C(400000); //400kHz fast I2C comunication
}
 
int main() {

    while(1){
    initServoDriver();
    setServoPulse(0, 308); //0 degree chanel 0
    setServoPulse(1, 495); //-90 degree chanel 1    
    wait(1);
    setServoPulse(0, 135); //90 degree chanel 0
    setServoPulse(1, 308); //0 degree chanel 1
    wait(1);
    setServoPulse(1, 135); //90 degree chanel 1 
    setServoPulse(0, 509); //0 degree chanel 1
    wait(1);           
    for (int mov = 495; mov > 135; mov--){
    setServoPulse(1, mov);
    wait(0.003);}
    for (int mov = 115; mov < 509; mov++){
    setServoPulse(0, mov);
    wait(0.003);    
    }           
    }
}