István Cserny
/
Lab02_pwm_servo
Moving a servo back and forth by using a PWM channel.
main.cpp@0:4887ccec9d82, 2021-10-13 (annotated)
- Committer:
- cspista
- Date:
- Wed Oct 13 14:00:26 2021 +0000
- Revision:
- 0:4887ccec9d82
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cspista | 0:4887ccec9d82 | 1 | #include "mbed.h" |
cspista | 0:4887ccec9d82 | 2 | PwmOut servo(D3); |
cspista | 0:4887ccec9d82 | 3 | |
cspista | 0:4887ccec9d82 | 4 | int main() { |
cspista | 0:4887ccec9d82 | 5 | servo.period_ms(20); //Period = 20 ms (f=50 Hz) |
cspista | 0:4887ccec9d82 | 6 | while(true) { |
cspista | 0:4887ccec9d82 | 7 | for(int pw=1000; pw <= 2000; pw=pw+20) { |
cspista | 0:4887ccec9d82 | 8 | servo.pulsewidth_us(pw); //Set new servo position |
cspista | 0:4887ccec9d82 | 9 | wait_ms(200); |
cspista | 0:4887ccec9d82 | 10 | } |
cspista | 0:4887ccec9d82 | 11 | wait_ms(1000); //Wait before reverse direction |
cspista | 0:4887ccec9d82 | 12 | for(int pw=2000; pw >= 1000; pw=pw-20) { |
cspista | 0:4887ccec9d82 | 13 | servo.pulsewidth_us(pw); //Set new servo position |
cspista | 0:4887ccec9d82 | 14 | wait_ms(200); |
cspista | 0:4887ccec9d82 | 15 | } |
cspista | 0:4887ccec9d82 | 16 | wait_ms(1000); |
cspista | 0:4887ccec9d82 | 17 | } |
cspista | 0:4887ccec9d82 | 18 | } |