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.
Fork of p2_analoginrty by
hello.cpp@0:00529fca1abf, 2015-09-29 (annotated)
- Committer:
- Sersd
- Date:
- Tue Sep 29 16:53:55 2015 +0000
- Revision:
- 0:00529fca1abf
fr
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Sersd | 0:00529fca1abf | 1 | // Flash an LED while a DigitalIn is true |
| Sersd | 0:00529fca1abf | 2 | |
| Sersd | 0:00529fca1abf | 3 | #include "mbed.h" |
| Sersd | 0:00529fca1abf | 4 | |
| Sersd | 0:00529fca1abf | 5 | AnalogIn ain(p15); |
| Sersd | 0:00529fca1abf | 6 | Serial pc(USBTX, USBRX); // tx, rx |
| Sersd | 0:00529fca1abf | 7 | |
| Sersd | 0:00529fca1abf | 8 | float media=0; |
| Sersd | 0:00529fca1abf | 9 | float alpha=0.99; |
| Sersd | 0:00529fca1abf | 10 | float actual=0; |
| Sersd | 0:00529fca1abf | 11 | float offset=0.001; |
| Sersd | 0:00529fca1abf | 12 | |
| Sersd | 0:00529fca1abf | 13 | int contador=0; |
| Sersd | 0:00529fca1abf | 14 | |
| Sersd | 0:00529fca1abf | 15 | char state =0; |
| Sersd | 0:00529fca1abf | 16 | char prev_state=0; |
| Sersd | 0:00529fca1abf | 17 | int timeini; |
| Sersd | 0:00529fca1abf | 18 | int timefi; |
| Sersd | 0:00529fca1abf | 19 | Timer timer; |
| Sersd | 0:00529fca1abf | 20 | int main() { |
| Sersd | 0:00529fca1abf | 21 | |
| Sersd | 0:00529fca1abf | 22 | media= ain.read(); |
| Sersd | 0:00529fca1abf | 23 | timer.start(); |
| Sersd | 0:00529fca1abf | 24 | while(1) { |
| Sersd | 0:00529fca1abf | 25 | actual=ain; |
| Sersd | 0:00529fca1abf | 26 | // pc.printf("actual %f \n\r",actual); |
| Sersd | 0:00529fca1abf | 27 | // pc.printf("media %f \n\r",media); |
| Sersd | 0:00529fca1abf | 28 | media=media*alpha+(1-alpha)*actual; |
| Sersd | 0:00529fca1abf | 29 | if((actual-media) > offset){ |
| Sersd | 0:00529fca1abf | 30 | state=1; |
| Sersd | 0:00529fca1abf | 31 | if(prev_state==0){ |
| Sersd | 0:00529fca1abf | 32 | if(contador==0){ |
| Sersd | 0:00529fca1abf | 33 | timeini = timer.read_us(); |
| Sersd | 0:00529fca1abf | 34 | contador++; |
| Sersd | 0:00529fca1abf | 35 | }else if(contador==1){ |
| Sersd | 0:00529fca1abf | 36 | timefi = timer.read_us(); |
| Sersd | 0:00529fca1abf | 37 | pc.printf("Periode %d \n\r",timefi-timeini); |
| Sersd | 0:00529fca1abf | 38 | contador=0; |
| Sersd | 0:00529fca1abf | 39 | } |
| Sersd | 0:00529fca1abf | 40 | } |
| Sersd | 0:00529fca1abf | 41 | }else if((actual-media) < offset){ |
| Sersd | 0:00529fca1abf | 42 | state=0; |
| Sersd | 0:00529fca1abf | 43 | |
| Sersd | 0:00529fca1abf | 44 | } |
| Sersd | 0:00529fca1abf | 45 | prev_state = state; |
| Sersd | 0:00529fca1abf | 46 | wait(0.0002); |
| Sersd | 0:00529fca1abf | 47 | } |
| Sersd | 0:00529fca1abf | 48 | } |
