mbed school
/
ch5_mbed_dust_sensor
ch5_mbed_dust_sensor
Diff: main.cpp
- Revision:
- 0:859e0fd616ee
diff -r 000000000000 -r 859e0fd616ee main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jan 26 09:10:53 2015 +0000 @@ -0,0 +1,44 @@ +#include "mbed.h" + +Serial pc(USBTX, USBRX); +InterruptIn probe(p19); +DigitalOut led(LED1); + +Timer timer; +Ticker ticker; + +uint32_t low_time = 0; + +void down(); +void up(); + +void tick() +{ + pc.printf("%d us, %f %%\r\n", low_time, low_time / (30.0 * 1000000)); + low_time = 0; +} + +void down() +{ + probe.rise(&up); + led = 0; + timer.start(); +} + +void up() +{ + led = 1; + timer.stop(); + probe.fall(&down); + + low_time += timer.read_us(); + timer.reset(); +} + +int main() +{ + pc.baud(115200); + pc.printf("Dust Sensor test\r\n"); + probe.fall(&down); + ticker.attach(tick, 30); +} \ No newline at end of file