Simple example to drive a servo on pin 1. A serial input at 57600 baud send a percentage from 0 to 100 (followed by whitespace) and a pulse goes out to drive an RC servo to one end of travel to another. Example for my DDJ blog at http://www.ddj.com/embedded.
meter.cpp
- Committer:
- wd5gnr
- Date:
- 2014-05-12
- Revision:
- 0:10d20e0f582e
File content as of revision 0:10d20e0f582e:
#include "mbed.h" PwmOut out0(P0_8); Serial host(USBTX, USBRX); int main(void) { host.baud(57600); // TODO read a digital input to select high/low baud out0.period_ms(20); out0.pulsewidth_us(2600); while(1) { unsigned int n=200; host.scanf("%d",&n); // read from PC if (n>100) continue; // make sure in range 0-100 n=((unsigned int) (1600.0*n/100.0)); // get a number from 0 to 1600 n=1600-n; // reverse motion direction so now it is 1600 to 0 out0.pulsewidth_us(n+1000); // add 1000 so that total number is 1000 to 2600 (or 1mS to 2.6mS) } }