Working read code with mode button
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Testnew by
main.cpp@14:7ff1f6022694, 2017-10-18 (annotated)
- Committer:
- nnguyen45
- Date:
- Wed Oct 18 14:14:11 2017 +0000
- Revision:
- 14:7ff1f6022694
- Parent:
- 13:b302c71e300f
test nhi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 2:9eda4611081a | 1 | #include "mbed.h" |
aismail1997 | 8:1aa86ad19af9 | 2 | #include "rtos.h" |
aismail1997 | 11:418a4437a693 | 3 | #include "wave_player.h" |
aismail1997 | 11:418a4437a693 | 4 | #include "SDFileSystem.h" |
aismail1997 | 12:21268d8bf979 | 5 | #include "button.h" |
aismail1997 | 5:9ed43e974156 | 6 | |
aismail1997 | 13:b302c71e300f | 7 | // Azra |
nnguyen45 | 14:7ff1f6022694 | 8 | //Nhi |
aismail1997 | 13:b302c71e300f | 9 | |
aismail1997 | 2:9eda4611081a | 10 | PwmOut myservo(p21); |
aismail1997 | 2:9eda4611081a | 11 | DigitalIn pb1 (p20); |
aismail1997 | 2:9eda4611081a | 12 | //AnalogIn linpot(p20); |
aismail1997 | 2:9eda4611081a | 13 | Serial pc(USBTX, USBRX); |
aismail1997 | 6:cc181f8f2bd1 | 14 | //DigitalOut myled(LED1); |
aismail1997 | 6:cc181f8f2bd1 | 15 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 6:cc181f8f2bd1 | 16 | AnalogOut DACout(p26); |
aismail1997 | 6:cc181f8f2bd1 | 17 | wave_player waver(&DACout); |
aismail1997 | 12:21268d8bf979 | 18 | button button1(myservo, pb1); |
aismail1997 | 6:cc181f8f2bd1 | 19 | |
aismail1997 | 8:1aa86ad19af9 | 20 | // add start, mode, reset buttons |
aismail1997 | 11:418a4437a693 | 21 | int start = 0; |
aismail1997 | 11:418a4437a693 | 22 | int submit = 0; |
aismail1997 | 12:21268d8bf979 | 23 | // FIX THIS: button up: state = 2, button halfway: state = 0; button down: state = 1 |
aismail1997 | 12:21268d8bf979 | 24 | int state = 2; |
aismail1997 | 8:1aa86ad19af9 | 25 | |
aismail1997 | 11:418a4437a693 | 26 | // FUNCTIONS |
aismail1997 | 11:418a4437a693 | 27 | void playSound(wave_player waver) |
aismail1997 | 11:418a4437a693 | 28 | { |
aismail1997 | 11:418a4437a693 | 29 | FILE *wave_file; |
aismail1997 | 11:418a4437a693 | 30 | wave_file=fopen("/sd/police_siren.wav","r"); |
aismail1997 | 11:418a4437a693 | 31 | waver.play(wave_file); |
aismail1997 | 11:418a4437a693 | 32 | fclose(wave_file); |
aismail1997 | 11:418a4437a693 | 33 | } |
aismail1997 | 8:1aa86ad19af9 | 34 | |
aismail1997 | 8:1aa86ad19af9 | 35 | // THREADS |
aismail1997 | 8:1aa86ad19af9 | 36 | void button_thread() |
aismail1997 | 6:cc181f8f2bd1 | 37 | { |
aismail1997 | 12:21268d8bf979 | 38 | // button was up and is moving down, move servo in |
aismail1997 | 12:21268d8bf979 | 39 | if (pb1 == 1 && state == 2) { |
aismail1997 | 12:21268d8bf979 | 40 | button1.moveServoIn(); |
aismail1997 | 12:21268d8bf979 | 41 | state = 1; |
aismail1997 | 12:21268d8bf979 | 42 | } |
aismail1997 | 12:21268d8bf979 | 43 | // button was down and is being pushed again, move servo out |
aismail1997 | 12:21268d8bf979 | 44 | if (pb1 == 1 && state == 1) { |
aismail1997 | 12:21268d8bf979 | 45 | button1.moveServoOut(); |
aismail1997 | 12:21268d8bf979 | 46 | state = 2; |
aismail1997 | 12:21268d8bf979 | 47 | } |
aismail1997 | 6:cc181f8f2bd1 | 48 | } |
aismail1997 | 6:cc181f8f2bd1 | 49 | |
aismail1997 | 8:1aa86ad19af9 | 50 | void submit_thread() |
aismail1997 | 6:cc181f8f2bd1 | 51 | { |
aismail1997 | 8:1aa86ad19af9 | 52 | |
aismail1997 | 6:cc181f8f2bd1 | 53 | } |
aismail1997 | 6:cc181f8f2bd1 | 54 | |
aismail1997 | 8:1aa86ad19af9 | 55 | void start_thread() |
aismail1997 | 6:cc181f8f2bd1 | 56 | { |
aismail1997 | 8:1aa86ad19af9 | 57 | // read pb_start |
aismail1997 | 8:1aa86ad19af9 | 58 | // if 1 |
aismail1997 | 8:1aa86ad19af9 | 59 | start == 1; |
aismail1997 | 8:1aa86ad19af9 | 60 | // else 0 |
aismail1997 | 6:cc181f8f2bd1 | 61 | } |
aismail1997 | 2:9eda4611081a | 62 | |
aismail1997 | 2:9eda4611081a | 63 | int main() |
aismail1997 | 2:9eda4611081a | 64 | { |
aismail1997 | 8:1aa86ad19af9 | 65 | // SETUP |
aismail1997 | 6:cc181f8f2bd1 | 66 | // pull up the pushbutton to prevent bouncing |
aismail1997 | 2:9eda4611081a | 67 | pb1.mode(PullUp); |
aismail1997 | 2:9eda4611081a | 68 | wait(.001); |
aismail1997 | 6:cc181f8f2bd1 | 69 | // servo begins at 30 degrees |
aismail1997 | 12:21268d8bf979 | 70 | // replace with a button setup function |
aismail1997 | 2:9eda4611081a | 71 | for(int i=0; i<=3; i++) { |
aismail1997 | 2:9eda4611081a | 72 | myservo = i/100.0; |
aismail1997 | 2:9eda4611081a | 73 | wait(0.01); |
aismail1997 | 2:9eda4611081a | 74 | } |
aismail1997 | 6:cc181f8f2bd1 | 75 | |
aismail1997 | 8:1aa86ad19af9 | 76 | // MAIN THREAD |
aismail1997 | 8:1aa86ad19af9 | 77 | while(true) { |
aismail1997 | 8:1aa86ad19af9 | 78 | |
aismail1997 | 8:1aa86ad19af9 | 79 | // start threads for reset, mode, start |
aismail1997 | 8:1aa86ad19af9 | 80 | Thread t1(start_thread); |
aismail1997 | 8:1aa86ad19af9 | 81 | // setup SDcard and Speaker |
aismail1997 | 12:21268d8bf979 | 82 | |
aismail1997 | 8:1aa86ad19af9 | 83 | // when started |
aismail1997 | 8:1aa86ad19af9 | 84 | while (start == 0) {} |
aismail1997 | 8:1aa86ad19af9 | 85 | Thread t2(button_thread); |
aismail1997 | 12:21268d8bf979 | 86 | |
aismail1997 | 12:21268d8bf979 | 87 | //Thread t3(submit_thread); |
aismail1997 | 8:1aa86ad19af9 | 88 | |
aismail1997 | 8:1aa86ad19af9 | 89 | // when submitted |
aismail1997 | 12:21268d8bf979 | 90 | //while (submit == 0) {} |
aismail1997 | 12:21268d8bf979 | 91 | |
aismail1997 | 8:1aa86ad19af9 | 92 | |
aismail1997 | 8:1aa86ad19af9 | 93 | // start button threads and submit thread |
aismail1997 | 8:1aa86ad19af9 | 94 | // if submit close button threads and submit thread |
aismail1997 | 8:1aa86ad19af9 | 95 | // check result, speaker result |
aismail1997 | 8:1aa86ad19af9 | 96 | // save results |
aismail1997 | 8:1aa86ad19af9 | 97 | |
aismail1997 | 6:cc181f8f2bd1 | 98 | // SD card code |
aismail1997 | 6:cc181f8f2bd1 | 99 | |
aismail1997 | 6:cc181f8f2bd1 | 100 | // speaker code |
aismail1997 | 2:9eda4611081a | 101 | |
aismail1997 | 2:9eda4611081a | 102 | // read linear potentiometer |
aismail1997 | 2:9eda4611081a | 103 | //if (linpot < 0.5) { |
aismail1997 | 2:9eda4611081a | 104 | //float potval = linpot; |
aismail1997 | 2:9eda4611081a | 105 | //pc.printf("linear pot: %f\n", potval); |
aismail1997 | 2:9eda4611081a | 106 | } |
aismail1997 | 2:9eda4611081a | 107 | } |