Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:be447af0921d, 2011-11-25 (annotated)
- Committer:
- daveTshave
- Date:
- Fri Nov 25 14:32:50 2011 +0000
- Revision:
- 0:be447af0921d
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| daveTshave | 0:be447af0921d | 1 | #include "counter.h" |
| daveTshave | 0:be447af0921d | 2 | |
| daveTshave | 0:be447af0921d | 3 | //todo: implement 2k sample buffer and store results until client connects, implement real time clock |
| daveTshave | 0:be447af0921d | 4 | |
| daveTshave | 0:be447af0921d | 5 | Serial pc(USBTX, USBRX); |
| daveTshave | 0:be447af0921d | 6 | counter SF800 (p29); |
| daveTshave | 0:be447af0921d | 7 | Timer t_a, t_b; |
| daveTshave | 0:be447af0921d | 8 | |
| daveTshave | 0:be447af0921d | 9 | int main() { |
| daveTshave | 0:be447af0921d | 10 | |
| daveTshave | 0:be447af0921d | 11 | t_a.start(); |
| daveTshave | 0:be447af0921d | 12 | t_b.start(); |
| daveTshave | 0:be447af0921d | 13 | int a_init_pulses = 0; |
| daveTshave | 0:be447af0921d | 14 | int b_init_pulses = 0; |
| daveTshave | 0:be447af0921d | 15 | float flowrate = 0; |
| daveTshave | 0:be447af0921d | 16 | int time_window = 10000; |
| daveTshave | 0:be447af0921d | 17 | |
| daveTshave | 0:be447af0921d | 18 | bool a_b_flip_flop = true; // if true use window a |
| daveTshave | 0:be447af0921d | 19 | |
| daveTshave | 0:be447af0921d | 20 | pc.printf("\n\r*************\n\rMBED restarted\n\r*************\n"); |
| daveTshave | 0:be447af0921d | 21 | |
| daveTshave | 0:be447af0921d | 22 | while (1) { |
| daveTshave | 0:be447af0921d | 23 | wait(0.1); |
| daveTshave | 0:be447af0921d | 24 | |
| daveTshave | 0:be447af0921d | 25 | if (a_b_flip_flop) { |
| daveTshave | 0:be447af0921d | 26 | flowrate = (float)(SF800.getPulses() - a_init_pulses)/(6000 * t_a.read_ms() / 60000); |
| daveTshave | 0:be447af0921d | 27 | } else { |
| daveTshave | 0:be447af0921d | 28 | flowrate = (float)(SF800.getPulses() - b_init_pulses)/(6000 * t_b.read_ms() / 60000); |
| daveTshave | 0:be447af0921d | 29 | } |
| daveTshave | 0:be447af0921d | 30 | |
| daveTshave | 0:be447af0921d | 31 | if (t_a.read_ms() > time_window) { // switch to window B |
| daveTshave | 0:be447af0921d | 32 | |
| daveTshave | 0:be447af0921d | 33 | pc.printf("\rswitching to window B = %i pulses detected in window A\n", SF800.getPulses() - a_init_pulses); |
| daveTshave | 0:be447af0921d | 34 | |
| daveTshave | 0:be447af0921d | 35 | t_a.reset(); |
| daveTshave | 0:be447af0921d | 36 | a_b_flip_flop = false; |
| daveTshave | 0:be447af0921d | 37 | a_init_pulses = SF800.getPulses(); |
| daveTshave | 0:be447af0921d | 38 | } |
| daveTshave | 0:be447af0921d | 39 | |
| daveTshave | 0:be447af0921d | 40 | if (t_a.read_ms() > time_window/2 && !a_b_flip_flop) { // switch to window A |
| daveTshave | 0:be447af0921d | 41 | |
| daveTshave | 0:be447af0921d | 42 | pc.printf("\rswitching to window A = %i pulses detected in window B\n", SF800.getPulses() - b_init_pulses); |
| daveTshave | 0:be447af0921d | 43 | |
| daveTshave | 0:be447af0921d | 44 | t_b.reset(); |
| daveTshave | 0:be447af0921d | 45 | a_b_flip_flop = true; |
| daveTshave | 0:be447af0921d | 46 | b_init_pulses = SF800.getPulses(); |
| daveTshave | 0:be447af0921d | 47 | } |
| daveTshave | 0:be447af0921d | 48 | |
| daveTshave | 0:be447af0921d | 49 | pc.printf("\rflowrate = %f l/min", flowrate); |
| daveTshave | 0:be447af0921d | 50 | } |
| daveTshave | 0:be447af0921d | 51 | |
| daveTshave | 0:be447af0921d | 52 | } |