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.
Dependencies: mbed
main.cpp@2:29c3d03c9231, 2018-02-14 (annotated)
- Committer:
- 2236693B
- Date:
- Wed Feb 14 11:08:48 2018 +0000
- Revision:
- 2:29c3d03c9231
- Parent:
- 1:d1e89afbe50c
- Child:
- 3:c9f4c9c84e36
First update;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 2236693B | 1:d1e89afbe50c | 1 | //Rango says, hey there Conor! |
| 2236693B | 0:005922ecb765 | 2 | |
| 2236693B | 0:005922ecb765 | 3 | #include "mbed.h" |
| 2236693B | 0:005922ecb765 | 4 | |
| 2236693B | 0:005922ecb765 | 5 | DigitalOut myled(LED1); |
| 2236693B | 0:005922ecb765 | 6 | AnalogIn Ain(PTB1); |
| 2236693B | 0:005922ecb765 | 7 | |
| 2236693B | 2:29c3d03c9231 | 8 | int const MAX_B = 10; |
| 2236693B | 0:005922ecb765 | 9 | int const delta = 0.5; |
| 2236693B | 2:29c3d03c9231 | 10 | int const AVG_LEN = 160; |
| 2236693B | 2:29c3d03c9231 | 11 | |
| 2236693B | 2:29c3d03c9231 | 12 | int sample_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 13 | int local_buffer[2*MAX_B] = {}; |
| 2236693B | 2:29c3d03c9231 | 14 | int avg_buffer[AVG_LEN] = {}; |
| 2236693B | 0:005922ecb765 | 15 | int read = 0; |
| 2236693B | 0:005922ecb765 | 16 | int write = 0; |
| 2236693B | 0:005922ecb765 | 17 | |
| 2236693B | 2:29c3d03c9231 | 18 | int avg_write = 0; |
| 2236693B | 2:29c3d03c9231 | 19 | |
| 2236693B | 2:29c3d03c9231 | 20 | int avg_sum; |
| 2236693B | 2:29c3d03c9231 | 21 | bool first ; |
| 2236693B | 2:29c3d03c9231 | 22 | |
| 2236693B | 2:29c3d03c9231 | 23 | Ticker sampler; |
| 2236693B | 2:29c3d03c9231 | 24 | |
| 2236693B | 2:29c3d03c9231 | 25 | void sampling () { |
| 2236693B | 2:29c3d03c9231 | 26 | unsigned int sample = Ain.read_u16(); |
| 2236693B | 2:29c3d03c9231 | 27 | sample_buffer[write++] = sample; |
| 2236693B | 2:29c3d03c9231 | 28 | |
| 2236693B | 2:29c3d03c9231 | 29 | write = write%(2*MAX_B); |
| 2236693B | 2:29c3d03c9231 | 30 | |
| 2236693B | 2:29c3d03c9231 | 31 | } |
| 2236693B | 2:29c3d03c9231 | 32 | |
| 2236693B | 2:29c3d03c9231 | 33 | void avg() { |
| 2236693B | 2:29c3d03c9231 | 34 | if (first) { |
| 2236693B | 2:29c3d03c9231 | 35 | for (int i =0; i <MAX_B; i++) { |
| 2236693B | 2:29c3d03c9231 | 36 | avg_buffer[avg_write] = local_buffer[i]; |
| 2236693B | 2:29c3d03c9231 | 37 | avg_write = (++avg_write) % AVG_LEN; |
| 2236693B | 2:29c3d03c9231 | 38 | } |
| 2236693B | 2:29c3d03c9231 | 39 | if (write == 0) { |
| 2236693B | 2:29c3d03c9231 | 40 | first = false; |
| 2236693B | 2:29c3d03c9231 | 41 | } |
| 2236693B | 2:29c3d03c9231 | 42 | } |
| 2236693B | 2:29c3d03c9231 | 43 | else { |
| 2236693B | 2:29c3d03c9231 | 44 | for (int i =0; i <MAX_B; i++) { |
| 2236693B | 2:29c3d03c9231 | 45 | int data = local_buffer[i]; |
| 2236693B | 2:29c3d03c9231 | 46 | avg_sum = avg_sum + data - avg_buffer[avg_write]; |
| 2236693B | 2:29c3d03c9231 | 47 | |
| 2236693B | 2:29c3d03c9231 | 48 | avg_buffer[avg_write] = data; |
| 2236693B | 2:29c3d03c9231 | 49 | avg_write = (++avg_write) % AVG_LEN; |
| 2236693B | 2:29c3d03c9231 | 50 | //get_trend(); |
| 2236693B | 2:29c3d03c9231 | 51 | |
| 2236693B | 2:29c3d03c9231 | 52 | } |
| 2236693B | 2:29c3d03c9231 | 53 | } |
| 2236693B | 2:29c3d03c9231 | 54 | |
| 2236693B | 2:29c3d03c9231 | 55 | } |
| 2236693B | 2:29c3d03c9231 | 56 | |
| 2236693B | 0:005922ecb765 | 57 | void data_process() { |
| 2236693B | 2:29c3d03c9231 | 58 | avg(); |
| 2236693B | 0:005922ecb765 | 59 | } |
| 2236693B | 0:005922ecb765 | 60 | |
| 2236693B | 0:005922ecb765 | 61 | int main() { |
| 2236693B | 0:005922ecb765 | 62 | sampler.attach(&sampling, 0.0125); //Sample at 80Hz |
| 2236693B | 0:005922ecb765 | 63 | |
| 2236693B | 0:005922ecb765 | 64 | while(1) { |
| 2236693B | 2:29c3d03c9231 | 65 | if (write-read > MAX_B || read-write > MAX_B) { // |write-read| > MAX_B/2 |
| 2236693B | 0:005922ecb765 | 66 | for(int i = 0; i < MAX_B/2; i++) { |
| 2236693B | 0:005922ecb765 | 67 | local_buffer[i] = sample_buffer[read]; |
| 2236693B | 0:005922ecb765 | 68 | read = (++read) % MAX_B; |
| 2236693B | 0:005922ecb765 | 69 | } |
| 2236693B | 0:005922ecb765 | 70 | } |
| 2236693B | 0:005922ecb765 | 71 | data_process(); |
| 2236693B | 0:005922ecb765 | 72 | } |
| 2236693B | 0:005922ecb765 | 73 | } |
| 2236693B | 0:005922ecb765 | 74 | |
| 2236693B | 0:005922ecb765 | 75 | |
| 2236693B | 0:005922ecb765 | 76 |

