Leo Veenstra
/
PWM
main.cpp@0:95ac8e26a28f, 2012-03-19 (annotated)
- Committer:
- SED9008
- Date:
- Mon Mar 19 15:46:08 2012 +0000
- Revision:
- 0:95ac8e26a28f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SED9008 | 0:95ac8e26a28f | 1 | #include "mbed.h" |
SED9008 | 0:95ac8e26a28f | 2 | |
SED9008 | 0:95ac8e26a28f | 3 | AnalogIn control(p18); |
SED9008 | 0:95ac8e26a28f | 4 | PwmOut servo(p21); |
SED9008 | 0:95ac8e26a28f | 5 | |
SED9008 | 0:95ac8e26a28f | 6 | int main() { |
SED9008 | 0:95ac8e26a28f | 7 | servo.period(0.020); // servo requires a 20ms period |
SED9008 | 0:95ac8e26a28f | 8 | float input = control.read(); |
SED9008 | 0:95ac8e26a28f | 9 | |
SED9008 | 0:95ac8e26a28f | 10 | while (1) { |
SED9008 | 0:95ac8e26a28f | 11 | input = control.read()/1000; |
SED9008 | 0:95ac8e26a28f | 12 | servo.pulsewidth(0.001 + input); // servo position determined by a pulsewidth between 1-2ms |
SED9008 | 0:95ac8e26a28f | 13 | wait(0.25); |
SED9008 | 0:95ac8e26a28f | 14 | |
SED9008 | 0:95ac8e26a28f | 15 | } |
SED9008 | 0:95ac8e26a28f | 16 | } |