![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
servo
Diff: main.cpp
- Revision:
- 3:2848f736b75a
- Parent:
- 2:10498697e83b
diff -r 10498697e83b -r 2848f736b75a main.cpp --- a/main.cpp Fri Sep 03 12:16:55 2021 +0000 +++ b/main.cpp Mon Sep 05 19:34:06 2022 +0000 @@ -1,21 +1,33 @@ -// https://os.mbed.com/questions/1680/Analog-In-PWM-Out-to-control-rc-servo/ -// 2021-08-26 - #include "mbed.h" -PwmOut servo(D9); - -int main() - { - servo.period_us(20000); //20ms period, typical for analog RC servo - int servopulsewidth=1500; - servo.pulsewidth_us(servopulsewidth); //centers the servo. Usually a RC servos range is from 1000 to 2000us. - while(1) - { - //servo.pulsewidth_us(1500); //increments servopulsewidth with 1 - //wait(0.5); - servo.pulsewidth_us(1000); //increments servopulsewidth with 1 - wait(0.5); - servo.pulsewidth_us(2000); //increments servopulsewidth with 1 - wait(0.5); +//Program to 'sweep' test a 'standard RC type servo +//Define some parameters using compiler directive '#define' +//Check Servo DATA if 0.75ms to 2.25ms then use min=750 and max=2250 +//NB be values in microseconds (Following are generic values) +#define MID 1500 +#define MIN 1000 +#define MAX 2000 +#define STEP 50 +//Time delay between steps in milliseconds +#define TIME 400 + +DigitalOut myLed(LED1); +DigitalIn myButton(USER_BUTTON); + +PwmOut myServo(D11); + +int main() { + + myServo.period_ms(20); + myServo.pulsewidth_us(MID); //NB in microseconds + + while(true) { + for (int i=MIN;i<=MAX;i+=STEP){ + myServo.pulsewidth_us(i); + wait_ms(TIME); + } + for (int i=MAX;i>=MIN;i-=STEP){ + myServo.pulsewidth_us(i); + wait_ms(TIME); + } } } \ No newline at end of file