microbit pca9685 demo
Dependencies: MicroBitPCA9685 microbit
Diff: main.cpp
- Revision:
- 0:a46cb73d2e3e
diff -r 000000000000 -r a46cb73d2e3e main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 13 21:15:52 2017 +0000 @@ -0,0 +1,69 @@ +/* Martin Cottrell v0.1 */ +/* Thanks for all the examples this is based on */ +/* This is an initial proof of concept using PCA9685 with microbit */ +/* Needs many refinements and I'll also looking at PWM 9110s next */ +/* Enjoy! */ + +#include "MicroBit.h" +#include "MicroBitPCA9685.h" +MicroBit uBit; + +int setServoPulse(MicroBitPCA9685& uPCA9685, uint8_t n, float pulse) { + float pulselength = 10000; // 10,000 units per seconds + pulse = 4094 * pulse / pulselength; + return uPCA9685.setPWM(n, 0, pulse); +} + +int setAllServoPulse(MicroBitPCA9685& uPCA9685, float pulse) { + float pulselength = 10000; // 10,000 units per seconds + pulse = 4094 * pulse / pulselength; + return uPCA9685.setAllPWM(0, pulse); +} + +void initServoDriver(MicroBitPCA9685& uPCA9685) { + uPCA9685.begin(); + uPCA9685.setPrescale(64); //This value is decided for 10ms interval. + uPCA9685.frequencyI2C(400000); //400kHz + } + +int main() +{ + // Initialise the micro:bit runtime. + uBit.init(); + MicroBitPCA9685 uPCA9685(uBit.i2c, 0x80); + //0x80 out the box PCA9685 address + + initServoDriver(uPCA9685); + while(true){ + uBit.display.scroll("All"); + setAllServoPulse(uPCA9685, 2300); + wait(0.5); + setAllServoPulse(uPCA9685, 1350); + wait(0.5); + + for (int mov = 550; mov < 2300; mov++){ + setAllServoPulse(uPCA9685, mov); + wait(0.001); + } + + for (int motor = 0; motor < 7; motor++){ + //I had 7 servos! + char buf[2]; + sprintf(buf, "%d", motor); + uBit.display.scroll(buf, 50); + + wait(0.2); + setServoPulse(uPCA9685, motor, 2300); + + wait(0.4);//delay necessary to perform the action + setServoPulse(uPCA9685, motor, 1350); + wait(0.4); + + for (int mov = 550; mov < 2300; mov++){ + setServoPulse(uPCA9685, motor, mov); + wait(0.001); + } + } + + } +} \ No newline at end of file