/** 03_pwm_servo * This program moves the servo back and forth * within the full range of pulse width = 1.0 - 2.0 ms. * * Hardware requirements: * - FRDM-KL25Z board * - Servo connected to D3, +5V and GND */

Dependencies:   mbed

/ 03_pwm_servo

  • This program moves the servo back and forth
  • within the full range of pulse width = 1.0 - 2.0 ms.
  • Hardware requirements:
  • - FRDM-KL25Z board
  • - Servo connected to D3, +5V and GND
  • /
Committer:
icserny
Date:
Thu Oct 22 12:54:29 2015 +0000
Revision:
0:fd545c26a4bf
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:fd545c26a4bf 1 /** 03_pwm_servo
icserny 0:fd545c26a4bf 2 * This program moves the servo back and forth
icserny 0:fd545c26a4bf 3 * within the full range of pulse width = 1.0 - 2.0 ms.
icserny 0:fd545c26a4bf 4 *
icserny 0:fd545c26a4bf 5 * Hardware requirements:
icserny 0:fd545c26a4bf 6 * - FRDM-KL25Z board
icserny 0:fd545c26a4bf 7 * - Servo connected to D3, +5V and GND
icserny 0:fd545c26a4bf 8 */
icserny 0:fd545c26a4bf 9
icserny 0:fd545c26a4bf 10 #include "mbed.h"
icserny 0:fd545c26a4bf 11 PwmOut servo(D3);
icserny 0:fd545c26a4bf 12
icserny 0:fd545c26a4bf 13 int main() {
icserny 0:fd545c26a4bf 14 servo.period_ms(20); //Period = 20 ms (f=50 Hz)
icserny 0:fd545c26a4bf 15 while(true) {
icserny 0:fd545c26a4bf 16 for(int pw=1000; pw <= 2000; pw=pw+20) {
icserny 0:fd545c26a4bf 17 servo.pulsewidth_us(pw); //Set new servo position
icserny 0:fd545c26a4bf 18 wait_ms(200);
icserny 0:fd545c26a4bf 19 }
icserny 0:fd545c26a4bf 20 wait_ms(1000); //Wait before reverse direction
icserny 0:fd545c26a4bf 21 for(int pw=2000; pw >= 1000; pw=pw-20) {
icserny 0:fd545c26a4bf 22 servo.pulsewidth_us(pw); //Set new servo position
icserny 0:fd545c26a4bf 23 wait_ms(200);
icserny 0:fd545c26a4bf 24 }
icserny 0:fd545c26a4bf 25 wait_ms(1000);
icserny 0:fd545c26a4bf 26 }
icserny 0:fd545c26a4bf 27 }