MBED_Afstandsmeter Niek
Dependencies: mbed
main.cpp@0:4a7c5016ab03, 2018-04-25 (annotated)
- Committer:
- niek_beumer
- Date:
- Wed Apr 25 18:33:42 2018 +0000
- Revision:
- 0:4a7c5016ab03
afstandsmeter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
niek_beumer | 0:4a7c5016ab03 | 1 | |
niek_beumer | 0:4a7c5016ab03 | 2 | #include "mbed.h" |
niek_beumer | 0:4a7c5016ab03 | 3 | |
niek_beumer | 0:4a7c5016ab03 | 4 | enum State {READ_INPUT, WRITE_DISTANCE} state = READ_INPUT; |
niek_beumer | 0:4a7c5016ab03 | 5 | |
niek_beumer | 0:4a7c5016ab03 | 6 | Ticker timer; |
niek_beumer | 0:4a7c5016ab03 | 7 | DigitalIn push_button(D15); |
niek_beumer | 0:4a7c5016ab03 | 8 | InterruptIn reset_button(USER_BUTTON); |
niek_beumer | 0:4a7c5016ab03 | 9 | PwmOut LED(D3); |
niek_beumer | 0:4a7c5016ab03 | 10 | AnalogIn Distance(PA_1); |
niek_beumer | 0:4a7c5016ab03 | 11 | |
niek_beumer | 0:4a7c5016ab03 | 12 | Serial pc(USBTX, USBRX); |
niek_beumer | 0:4a7c5016ab03 | 13 | |
niek_beumer | 0:4a7c5016ab03 | 14 | void reset() |
niek_beumer | 0:4a7c5016ab03 | 15 | { |
niek_beumer | 0:4a7c5016ab03 | 16 | state = READ_INPUT; |
niek_beumer | 0:4a7c5016ab03 | 17 | } |
niek_beumer | 0:4a7c5016ab03 | 18 | |
niek_beumer | 0:4a7c5016ab03 | 19 | int main() |
niek_beumer | 0:4a7c5016ab03 | 20 | { |
niek_beumer | 0:4a7c5016ab03 | 21 | LED = 0; |
niek_beumer | 0:4a7c5016ab03 | 22 | int a=0; |
niek_beumer | 0:4a7c5016ab03 | 23 | reset_button.rise(&reset); |
niek_beumer | 0:4a7c5016ab03 | 24 | enum State next_state = state; |
niek_beumer | 0:4a7c5016ab03 | 25 | |
niek_beumer | 0:4a7c5016ab03 | 26 | while(1) { |
niek_beumer | 0:4a7c5016ab03 | 27 | switch(next_state) { |
niek_beumer | 0:4a7c5016ab03 | 28 | case READ_INPUT: |
niek_beumer | 0:4a7c5016ab03 | 29 | |
niek_beumer | 0:4a7c5016ab03 | 30 | LED=Distance.read(); |
niek_beumer | 0:4a7c5016ab03 | 31 | if(push_button == 1) { |
niek_beumer | 0:4a7c5016ab03 | 32 | next_state = WRITE_DISTANCE; |
niek_beumer | 0:4a7c5016ab03 | 33 | |
niek_beumer | 0:4a7c5016ab03 | 34 | } |
niek_beumer | 0:4a7c5016ab03 | 35 | break; |
niek_beumer | 0:4a7c5016ab03 | 36 | |
niek_beumer | 0:4a7c5016ab03 | 37 | case WRITE_DISTANCE: |
niek_beumer | 0:4a7c5016ab03 | 38 | while (a<1) { |
niek_beumer | 0:4a7c5016ab03 | 39 | pc.printf("De afstand is cm :%f\n\r",(1-Distance.read())*18); |
niek_beumer | 0:4a7c5016ab03 | 40 | a++; |
niek_beumer | 0:4a7c5016ab03 | 41 | } |
niek_beumer | 0:4a7c5016ab03 | 42 | break; |
niek_beumer | 0:4a7c5016ab03 | 43 | |
niek_beumer | 0:4a7c5016ab03 | 44 | } |
niek_beumer | 0:4a7c5016ab03 | 45 | } |
niek_beumer | 0:4a7c5016ab03 | 46 | } |