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@0:005922ecb765, 2018-02-14 (annotated)
- Committer:
- 2236693B
- Date:
- Wed Feb 14 10:05:03 2018 +0000
- Revision:
- 0:005922ecb765
- Child:
- 1:d1e89afbe50c
Start
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| 2236693B | 0:005922ecb765 | 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 | 0:005922ecb765 | 8 | int const MAX_B = 20; | 
| 2236693B | 0:005922ecb765 | 9 | int const delta = 0.5; | 
| 2236693B | 0:005922ecb765 | 10 | int sample_buffer[MAX_B] = {}; | 
| 2236693B | 0:005922ecb765 | 11 | int local_buffer[MAX_B] = {}; | 
| 2236693B | 0:005922ecb765 | 12 | int read = 0; | 
| 2236693B | 0:005922ecb765 | 13 | int write = 0; | 
| 2236693B | 0:005922ecb765 | 14 | int sum = 0; | 
| 2236693B | 0:005922ecb765 | 15 | int prev | 
| 2236693B | 0:005922ecb765 | 16 | |
| 2236693B | 0:005922ecb765 | 17 | Ticker sampler; | 
| 2236693B | 0:005922ecb765 | 18 | Timer t; | 
| 2236693B | 0:005922ecb765 | 19 | |
| 2236693B | 0:005922ecb765 | 20 | void sampling() { | 
| 2236693B | 0:005922ecb765 | 21 | unsigned int sample = Ain.read_u16(); | 
| 2236693B | 0:005922ecb765 | 22 | sample_buffer[write++]=sample; | 
| 2236693B | 0:005922ecb765 | 23 | |
| 2236693B | 0:005922ecb765 | 24 | write = write%MAX_B; | 
| 2236693B | 0:005922ecb765 | 25 | } | 
| 2236693B | 0:005922ecb765 | 26 | |
| 2236693B | 0:005922ecb765 | 27 | int get_trend(int current) { | 
| 2236693B | 0:005922ecb765 | 28 | int trend; | 
| 2236693B | 0:005922ecb765 | 29 | |
| 2236693B | 0:005922ecb765 | 30 | return trend; | 
| 2236693B | 0:005922ecb765 | 31 | } | 
| 2236693B | 0:005922ecb765 | 32 | |
| 2236693B | 0:005922ecb765 | 33 | void data_process() { | 
| 2236693B | 0:005922ecb765 | 34 | int trend = get_trend(); | 
| 2236693B | 0:005922ecb765 | 35 | int range = max(data)-min(data); | 
| 2236693B | 0:005922ecb765 | 36 | } | 
| 2236693B | 0:005922ecb765 | 37 | |
| 2236693B | 0:005922ecb765 | 38 | int filter (int x, int y) { | 
| 2236693B | 0:005922ecb765 | 39 | return delta*(x)+ (1-delta)*y; | 
| 2236693B | 0:005922ecb765 | 40 | } | 
| 2236693B | 0:005922ecb765 | 41 | |
| 2236693B | 0:005922ecb765 | 42 | |
| 2236693B | 0:005922ecb765 | 43 | int main() { | 
| 2236693B | 0:005922ecb765 | 44 | sampler.attach(&sampling, 0.0125); //Sample at 80Hz | 
| 2236693B | 0:005922ecb765 | 45 | //t.start(); | 
| 2236693B | 0:005922ecb765 | 46 | //t.wait(0.1); | 
| 2236693B | 0:005922ecb765 | 47 | |
| 2236693B | 0:005922ecb765 | 48 | while(1) { | 
| 2236693B | 0:005922ecb765 | 49 | //t.reset(); | 
| 2236693B | 0:005922ecb765 | 50 | //start = t.read(); | 
| 2236693B | 0:005922ecb765 | 51 | if (write-read > MAX_B/2 || read-write > MAX_B/2) { // |write-read| > MAX_B/2 | 
| 2236693B | 0:005922ecb765 | 52 | for(int i = 0; i < MAX_B/2; i++) { | 
| 2236693B | 0:005922ecb765 | 53 | local_buffer[i] = sample_buffer[read]; | 
| 2236693B | 0:005922ecb765 | 54 | read = (++read) % MAX_B; | 
| 2236693B | 0:005922ecb765 | 55 | } | 
| 2236693B | 0:005922ecb765 | 56 | } | 
| 2236693B | 0:005922ecb765 | 57 | data_process(); | 
| 2236693B | 0:005922ecb765 | 58 | |
| 2236693B | 0:005922ecb765 | 59 | } | 
| 2236693B | 0:005922ecb765 | 60 | } | 
| 2236693B | 0:005922ecb765 | 61 | |
| 2236693B | 0:005922ecb765 | 62 | |
| 2236693B | 0:005922ecb765 | 63 | 

