servo control via ticker

Dependencies:   mbed Servo

Committer:
jasonberry
Date:
Tue Mar 16 12:20:58 2021 +0000
Revision:
1:2c6164968c7f
Parent:
0:5014bf742e9b
servo control via ticker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:5014bf742e9b 1 #include "mbed.h"
jasonberry 1:2c6164968c7f 2 #include "Servo.h"
jasonberry 1:2c6164968c7f 3
jasonberry 1:2c6164968c7f 4 Servo s1(p21);
jasonberry 1:2c6164968c7f 5 Servo s2(p22);
jasonberry 1:2c6164968c7f 6
mbed_official 0:5014bf742e9b 7
mbed_official 0:5014bf742e9b 8 Ticker flipper;
jasonberry 1:2c6164968c7f 9
mbed_official 0:5014bf742e9b 10 DigitalOut led1(LED1);
mbed_official 0:5014bf742e9b 11 DigitalOut led2(LED2);
mbed_official 0:5014bf742e9b 12
mbed_official 0:5014bf742e9b 13 void flip() {
mbed_official 0:5014bf742e9b 14 led2 = !led2;
jasonberry 1:2c6164968c7f 15 s1 = s1+0.005;
jasonberry 1:2c6164968c7f 16 s2 = s2+0.005;
jasonberry 1:2c6164968c7f 17
jasonberry 1:2c6164968c7f 18 if(s1 == 1)
jasonberry 1:2c6164968c7f 19 s1 = 0;
jasonberry 1:2c6164968c7f 20 if(s2 == 1)
jasonberry 1:2c6164968c7f 21 s2 =0;
mbed_official 0:5014bf742e9b 22 }
mbed_official 0:5014bf742e9b 23
mbed_official 0:5014bf742e9b 24 int main() {
jasonberry 1:2c6164968c7f 25
mbed_official 0:5014bf742e9b 26 led2 = 1;
jasonberry 1:2c6164968c7f 27 flipper.attach(&flip, 0.01); // the address of the function to be attached (flip) and the interval (2 seconds)
mbed_official 0:5014bf742e9b 28
jasonberry 1:2c6164968c7f 29 s1.calibrate(0.00105, 45.0);
jasonberry 1:2c6164968c7f 30 s2.calibrate(0.00105, 45.0);
jasonberry 1:2c6164968c7f 31
jasonberry 1:2c6164968c7f 32 s1=0;
jasonberry 1:2c6164968c7f 33 s2=0;
jasonberry 1:2c6164968c7f 34
mbed_official 0:5014bf742e9b 35 // spin in a main loop. flipper will interrupt it to call flip
mbed_official 0:5014bf742e9b 36 while(1) {
mbed_official 0:5014bf742e9b 37 led1 = !led1;
mbed_official 0:5014bf742e9b 38 wait(0.2);
mbed_official 0:5014bf742e9b 39 }
mbed_official 0:5014bf742e9b 40 }