Opzet reactietester
Dependencies: mbed
main.cpp@0:c9b07a2f3bcb, 2018-04-03 (annotated)
- Committer:
- lennartgroen
- Date:
- Tue Apr 03 22:33:53 2018 +0000
- Revision:
- 0:c9b07a2f3bcb
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lennartgroen | 0:c9b07a2f3bcb | 1 | /* |
lennartgroen | 0:c9b07a2f3bcb | 2 | Namen: Lennart Groen |
lennartgroen | 0:c9b07a2f3bcb | 3 | Klas: EMT1PSa |
lennartgroen | 0:c9b07a2f3bcb | 4 | Opdracht: MBED - Stoplicht |
lennartgroen | 0:c9b07a2f3bcb | 5 | Samengewerkt met: ... |
lennartgroen | 0:c9b07a2f3bcb | 6 | Datum: 02-04-2018 |
lennartgroen | 0:c9b07a2f3bcb | 7 | Inspiratie uit: |
lennartgroen | 0:c9b07a2f3bcb | 8 | */ |
lennartgroen | 0:c9b07a2f3bcb | 9 | |
lennartgroen | 0:c9b07a2f3bcb | 10 | #include <mbed.h> |
lennartgroen | 0:c9b07a2f3bcb | 11 | |
lennartgroen | 0:c9b07a2f3bcb | 12 | DigitalOut led(D2); |
lennartgroen | 0:c9b07a2f3bcb | 13 | DigitalIn Button(D3); |
lennartgroen | 0:c9b07a2f3bcb | 14 | Serial pc(USBTX, USBRX); // tx, rx |
lennartgroen | 0:c9b07a2f3bcb | 15 | |
lennartgroen | 0:c9b07a2f3bcb | 16 | enum machinestate |
lennartgroen | 0:c9b07a2f3bcb | 17 | { |
lennartgroen | 0:c9b07a2f3bcb | 18 | create_random_time, |
lennartgroen | 0:c9b07a2f3bcb | 19 | measure_start, |
lennartgroen | 0:c9b07a2f3bcb | 20 | reaction, |
lennartgroen | 0:c9b07a2f3bcb | 21 | send_time |
lennartgroen | 0:c9b07a2f3bcb | 22 | }; |
lennartgroen | 0:c9b07a2f3bcb | 23 | |
lennartgroen | 0:c9b07a2f3bcb | 24 | class reactiontester |
lennartgroen | 0:c9b07a2f3bcb | 25 | { |
lennartgroen | 0:c9b07a2f3bcb | 26 | private: |
lennartgroen | 0:c9b07a2f3bcb | 27 | |
lennartgroen | 0:c9b07a2f3bcb | 28 | public: |
lennartgroen | 0:c9b07a2f3bcb | 29 | reactiontester(); |
lennartgroen | 0:c9b07a2f3bcb | 30 | float set_random_time(); |
lennartgroen | 0:c9b07a2f3bcb | 31 | |
lennartgroen | 0:c9b07a2f3bcb | 32 | }; |
lennartgroen | 0:c9b07a2f3bcb | 33 | |
lennartgroen | 0:c9b07a2f3bcb | 34 | int main() |
lennartgroen | 0:c9b07a2f3bcb | 35 | { |
lennartgroen | 0:c9b07a2f3bcb | 36 | enum machinestate state; |
lennartgroen | 0:c9b07a2f3bcb | 37 | reactiontester recationtester1; |
lennartgroen | 0:c9b07a2f3bcb | 38 | Timer timer1; |
lennartgroen | 0:c9b07a2f3bcb | 39 | Timer timer2; |
lennartgroen | 0:c9b07a2f3bcb | 40 | timer1.start(); |
lennartgroen | 0:c9b07a2f3bcb | 41 | float random_time = 0.0; |
lennartgroen | 0:c9b07a2f3bcb | 42 | |
lennartgroen | 0:c9b07a2f3bcb | 43 | while(1){ |
lennartgroen | 0:c9b07a2f3bcb | 44 | switch(state) |
lennartgroen | 0:c9b07a2f3bcb | 45 | { |
lennartgroen | 0:c9b07a2f3bcb | 46 | case create_random_time: |
lennartgroen | 0:c9b07a2f3bcb | 47 | random_time = recationtester1.set_random_time(); |
lennartgroen | 0:c9b07a2f3bcb | 48 | break; |
lennartgroen | 0:c9b07a2f3bcb | 49 | case measure_start: |
lennartgroen | 0:c9b07a2f3bcb | 50 | led = 1; |
lennartgroen | 0:c9b07a2f3bcb | 51 | timer2.start(); |
lennartgroen | 0:c9b07a2f3bcb | 52 | break; |
lennartgroen | 0:c9b07a2f3bcb | 53 | case reaction: |
lennartgroen | 0:c9b07a2f3bcb | 54 | led = 0; |
lennartgroen | 0:c9b07a2f3bcb | 55 | timer2.stop(); |
lennartgroen | 0:c9b07a2f3bcb | 56 | state = send_time; |
lennartgroen | 0:c9b07a2f3bcb | 57 | break; |
lennartgroen | 0:c9b07a2f3bcb | 58 | case send_time: |
lennartgroen | 0:c9b07a2f3bcb | 59 | float time = timer2.read(); |
lennartgroen | 0:c9b07a2f3bcb | 60 | pc.printf("Measured Time: "); |
lennartgroen | 0:c9b07a2f3bcb | 61 | break; |
lennartgroen | 0:c9b07a2f3bcb | 62 | } |
lennartgroen | 0:c9b07a2f3bcb | 63 | if (random_time <= timer1.read() && state == create_random_time){ |
lennartgroen | 0:c9b07a2f3bcb | 64 | state = measure_start; |
lennartgroen | 0:c9b07a2f3bcb | 65 | } |
lennartgroen | 0:c9b07a2f3bcb | 66 | if (Button == 1 && state == measure_start){ |
lennartgroen | 0:c9b07a2f3bcb | 67 | state = reaction; |
lennartgroen | 0:c9b07a2f3bcb | 68 | } |
lennartgroen | 0:c9b07a2f3bcb | 69 | |
lennartgroen | 0:c9b07a2f3bcb | 70 | } |
lennartgroen | 0:c9b07a2f3bcb | 71 | |
lennartgroen | 0:c9b07a2f3bcb | 72 | } |
lennartgroen | 0:c9b07a2f3bcb | 73 | |
lennartgroen | 0:c9b07a2f3bcb | 74 | |
lennartgroen | 0:c9b07a2f3bcb | 75 | reactiontester::reactiontester() |
lennartgroen | 0:c9b07a2f3bcb | 76 | { |
lennartgroen | 0:c9b07a2f3bcb | 77 | |
lennartgroen | 0:c9b07a2f3bcb | 78 | } |
lennartgroen | 0:c9b07a2f3bcb | 79 | |
lennartgroen | 0:c9b07a2f3bcb | 80 | float reactiontester::set_random_time() |
lennartgroen | 0:c9b07a2f3bcb | 81 | { |
lennartgroen | 0:c9b07a2f3bcb | 82 | int min_value = 3; |
lennartgroen | 0:c9b07a2f3bcb | 83 | int max_value = 10; |
lennartgroen | 0:c9b07a2f3bcb | 84 | float random_number = rand() % (max_value+min_value); |
lennartgroen | 0:c9b07a2f3bcb | 85 | return random_number; |
lennartgroen | 0:c9b07a2f3bcb | 86 | } |
lennartgroen | 0:c9b07a2f3bcb | 87 |