
Working read code with mode button
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Testnew by
main.cpp@16:581a3b02f4c3, 2017-10-22 (annotated)
- Committer:
- aismail1997
- Date:
- Sun Oct 22 22:20:11 2017 +0000
- Revision:
- 16:581a3b02f4c3
- Parent:
- 15:b80dde24e9bc
- Child:
- 17:7e9308d14faa
Last working code
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 | 15:b80dde24e9bc | 7 | // DEFINE I/O |
aismail1997 | 2:9eda4611081a | 8 | PwmOut myservo(p21); |
aismail1997 | 2:9eda4611081a | 9 | DigitalIn pb1 (p20); |
aismail1997 | 16:581a3b02f4c3 | 10 | PwmOut myservo2(p22); |
aismail1997 | 16:581a3b02f4c3 | 11 | DigitalIn pb2 (p19); |
aismail1997 | 16:581a3b02f4c3 | 12 | DigitalOut led1(LED1); |
aismail1997 | 16:581a3b02f4c3 | 13 | DigitalOut led2(LED4); |
aismail1997 | 16:581a3b02f4c3 | 14 | |
aismail1997 | 2:9eda4611081a | 15 | //AnalogIn linpot(p20); |
aismail1997 | 16:581a3b02f4c3 | 16 | //Serial pc(USBTX, USBRX); |
aismail1997 | 16:581a3b02f4c3 | 17 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 16:581a3b02f4c3 | 18 | //AnalogOut DACout(p26); |
aismail1997 | 16:581a3b02f4c3 | 19 | //wave_player waver(&DACout); |
aismail1997 | 12:21268d8bf979 | 20 | button button1(myservo, pb1); |
aismail1997 | 16:581a3b02f4c3 | 21 | button button2(myservo2, pb2); |
aismail1997 | 6:cc181f8f2bd1 | 22 | |
aismail1997 | 15:b80dde24e9bc | 23 | // INITIALIZE VARIABLES |
aismail1997 | 15:b80dde24e9bc | 24 | // add mode, reset buttons |
aismail1997 | 11:418a4437a693 | 25 | int start = 0; |
aismail1997 | 11:418a4437a693 | 26 | int submit = 0; |
aismail1997 | 12:21268d8bf979 | 27 | // FIX THIS: button up: state = 2, button halfway: state = 0; button down: state = 1 |
aismail1997 | 12:21268d8bf979 | 28 | int state = 2; |
aismail1997 | 16:581a3b02f4c3 | 29 | int state2 = 2; |
aismail1997 | 16:581a3b02f4c3 | 30 | int count = 0; |
aismail1997 | 8:1aa86ad19af9 | 31 | |
aismail1997 | 11:418a4437a693 | 32 | // FUNCTIONS |
aismail1997 | 15:b80dde24e9bc | 33 | // play a file on the speaker |
aismail1997 | 16:581a3b02f4c3 | 34 | /*void playSound(wave_player waver) |
aismail1997 | 11:418a4437a693 | 35 | { |
aismail1997 | 11:418a4437a693 | 36 | FILE *wave_file; |
aismail1997 | 15:b80dde24e9bc | 37 | wave_file=fopen("/sd/lesson.wav","r"); |
aismail1997 | 11:418a4437a693 | 38 | waver.play(wave_file); |
aismail1997 | 11:418a4437a693 | 39 | fclose(wave_file); |
aismail1997 | 16:581a3b02f4c3 | 40 | }*/ |
aismail1997 | 8:1aa86ad19af9 | 41 | |
aismail1997 | 8:1aa86ad19af9 | 42 | // THREADS |
aismail1997 | 15:b80dde24e9bc | 43 | |
aismail1997 | 15:b80dde24e9bc | 44 | // thread for the custom button |
aismail1997 | 8:1aa86ad19af9 | 45 | void button_thread() |
aismail1997 | 6:cc181f8f2bd1 | 46 | { |
aismail1997 | 16:581a3b02f4c3 | 47 | while(true) { |
aismail1997 | 16:581a3b02f4c3 | 48 | // button was up and is moving down, move servo in |
aismail1997 | 16:581a3b02f4c3 | 49 | //pc.printf("in button thread"); |
aismail1997 | 16:581a3b02f4c3 | 50 | if (pb1 == 1 && state == 2) { |
aismail1997 | 16:581a3b02f4c3 | 51 | button1.moveServoIn(); |
aismail1997 | 16:581a3b02f4c3 | 52 | state = 1; |
aismail1997 | 16:581a3b02f4c3 | 53 | led1 = 0; |
aismail1997 | 16:581a3b02f4c3 | 54 | } |
aismail1997 | 16:581a3b02f4c3 | 55 | // button was down and is being pushed again, move servo out |
aismail1997 | 16:581a3b02f4c3 | 56 | else if (pb1 == 1 && state == 1) { |
aismail1997 | 16:581a3b02f4c3 | 57 | button1.moveServoOut(); |
aismail1997 | 16:581a3b02f4c3 | 58 | state = 2; |
aismail1997 | 16:581a3b02f4c3 | 59 | led1 = 1; |
aismail1997 | 16:581a3b02f4c3 | 60 | } |
aismail1997 | 16:581a3b02f4c3 | 61 | Thread::wait(100); // wait till thread is done |
aismail1997 | 12:21268d8bf979 | 62 | } |
aismail1997 | 16:581a3b02f4c3 | 63 | } |
aismail1997 | 16:581a3b02f4c3 | 64 | |
aismail1997 | 16:581a3b02f4c3 | 65 | void button2_thread() |
aismail1997 | 16:581a3b02f4c3 | 66 | { |
aismail1997 | 16:581a3b02f4c3 | 67 | // TODO: Add states |
aismail1997 | 16:581a3b02f4c3 | 68 | while(true) { |
aismail1997 | 16:581a3b02f4c3 | 69 | // button was up and is moving down, move servo in |
aismail1997 | 16:581a3b02f4c3 | 70 | if (pb2 == 1 && state2 == 2) { |
aismail1997 | 16:581a3b02f4c3 | 71 | button2.moveServoIn(); |
aismail1997 | 16:581a3b02f4c3 | 72 | state2 = 1; |
aismail1997 | 16:581a3b02f4c3 | 73 | led2 = 0; |
aismail1997 | 16:581a3b02f4c3 | 74 | count = 0; |
aismail1997 | 16:581a3b02f4c3 | 75 | } |
aismail1997 | 16:581a3b02f4c3 | 76 | // button was down and is being pushed again, move servo out |
aismail1997 | 16:581a3b02f4c3 | 77 | else if (pb2 == 1 && state2 == 1) { |
aismail1997 | 16:581a3b02f4c3 | 78 | button2.moveServoOut(); |
aismail1997 | 16:581a3b02f4c3 | 79 | state2 = 2; |
aismail1997 | 16:581a3b02f4c3 | 80 | led2 = 1; |
aismail1997 | 16:581a3b02f4c3 | 81 | } |
aismail1997 | 16:581a3b02f4c3 | 82 | Thread::wait(100); // wait till thread is done |
aismail1997 | 12:21268d8bf979 | 83 | } |
aismail1997 | 6:cc181f8f2bd1 | 84 | } |
aismail1997 | 6:cc181f8f2bd1 | 85 | |
aismail1997 | 8:1aa86ad19af9 | 86 | void submit_thread() |
aismail1997 | 6:cc181f8f2bd1 | 87 | { |
aismail1997 | 15:b80dde24e9bc | 88 | Thread::wait(500); // wait till thread is done |
aismail1997 | 6:cc181f8f2bd1 | 89 | } |
aismail1997 | 6:cc181f8f2bd1 | 90 | |
aismail1997 | 8:1aa86ad19af9 | 91 | void start_thread() |
aismail1997 | 6:cc181f8f2bd1 | 92 | { |
aismail1997 | 8:1aa86ad19af9 | 93 | // read pb_start |
aismail1997 | 8:1aa86ad19af9 | 94 | // if 1 |
aismail1997 | 16:581a3b02f4c3 | 95 | start = 1; |
aismail1997 | 16:581a3b02f4c3 | 96 | //pc.printf("start %d ", start); |
aismail1997 | 8:1aa86ad19af9 | 97 | // else 0 |
aismail1997 | 15:b80dde24e9bc | 98 | Thread::wait(500); // wait till thread is done |
aismail1997 | 6:cc181f8f2bd1 | 99 | } |
aismail1997 | 2:9eda4611081a | 100 | |
aismail1997 | 16:581a3b02f4c3 | 101 | |
aismail1997 | 2:9eda4611081a | 102 | int main() |
aismail1997 | 2:9eda4611081a | 103 | { |
aismail1997 | 8:1aa86ad19af9 | 104 | // SETUP |
aismail1997 | 6:cc181f8f2bd1 | 105 | // pull up the pushbutton to prevent bouncing |
aismail1997 | 16:581a3b02f4c3 | 106 | //pb1.mode(PullUp); |
aismail1997 | 16:581a3b02f4c3 | 107 | //pb2.mode(PullUp); |
aismail1997 | 16:581a3b02f4c3 | 108 | //wait(.001); |
aismail1997 | 16:581a3b02f4c3 | 109 | |
aismail1997 | 6:cc181f8f2bd1 | 110 | // servo begins at 30 degrees |
aismail1997 | 12:21268d8bf979 | 111 | // replace with a button setup function |
aismail1997 | 2:9eda4611081a | 112 | for(int i=0; i<=3; i++) { |
aismail1997 | 2:9eda4611081a | 113 | myservo = i/100.0; |
aismail1997 | 2:9eda4611081a | 114 | wait(0.01); |
aismail1997 | 2:9eda4611081a | 115 | } |
aismail1997 | 16:581a3b02f4c3 | 116 | for(int i=0; i<=3; i++) { |
aismail1997 | 16:581a3b02f4c3 | 117 | myservo2 = i/100.0; |
aismail1997 | 16:581a3b02f4c3 | 118 | wait(0.01); |
aismail1997 | 16:581a3b02f4c3 | 119 | } |
aismail1997 | 6:cc181f8f2bd1 | 120 | |
aismail1997 | 16:581a3b02f4c3 | 121 | led1 = 1; |
aismail1997 | 16:581a3b02f4c3 | 122 | led2 = 1; |
aismail1997 | 16:581a3b02f4c3 | 123 | Thread t3(button_thread); |
aismail1997 | 16:581a3b02f4c3 | 124 | Thread t4(button2_thread); |
aismail1997 | 16:581a3b02f4c3 | 125 | t3.start(button_thread); |
aismail1997 | 16:581a3b02f4c3 | 126 | t4.start(button2_thread); |
aismail1997 | 16:581a3b02f4c3 | 127 | |
aismail1997 | 16:581a3b02f4c3 | 128 | // start threads for reset, mode, start |
aismail1997 | 16:581a3b02f4c3 | 129 | //Thread t1(start_thread); |
aismail1997 | 16:581a3b02f4c3 | 130 | //pc.printf("start thread"); |
aismail1997 | 16:581a3b02f4c3 | 131 | //Thread t2(submit_thread); |
aismail1997 | 16:581a3b02f4c3 | 132 | // setup SDcard and Speaker |
aismail1997 | 16:581a3b02f4c3 | 133 | |
aismail1997 | 16:581a3b02f4c3 | 134 | // when started |
aismail1997 | 16:581a3b02f4c3 | 135 | //while (start == 0){} |
aismail1997 | 16:581a3b02f4c3 | 136 | //Thread t3(button_thread); |
aismail1997 | 16:581a3b02f4c3 | 137 | //pc.printf("button thread"); |
aismail1997 | 16:581a3b02f4c3 | 138 | //Thread t4(button2_thread); |
aismail1997 | 16:581a3b02f4c3 | 139 | //pc.printf("button2 thread"); |
aismail1997 | 16:581a3b02f4c3 | 140 | |
aismail1997 | 16:581a3b02f4c3 | 141 | //Thread t3(submit_thread); |
aismail1997 | 16:581a3b02f4c3 | 142 | |
aismail1997 | 16:581a3b02f4c3 | 143 | // when submitted |
aismail1997 | 16:581a3b02f4c3 | 144 | //while (submit == 0) {} |
aismail1997 | 16:581a3b02f4c3 | 145 | |
aismail1997 | 16:581a3b02f4c3 | 146 | // start button threads and submit thread |
aismail1997 | 16:581a3b02f4c3 | 147 | // if submit close button threads and submit thread |
aismail1997 | 16:581a3b02f4c3 | 148 | // check result |
aismail1997 | 16:581a3b02f4c3 | 149 | // play results on speaker |
aismail1997 | 16:581a3b02f4c3 | 150 | // save results |
aismail1997 | 16:581a3b02f4c3 | 151 | |
aismail1997 | 16:581a3b02f4c3 | 152 | // read linear potentiometer |
aismail1997 | 16:581a3b02f4c3 | 153 | //if (linpot < 0.5) { |
aismail1997 | 16:581a3b02f4c3 | 154 | //float potval = linpot; |
aismail1997 | 16:581a3b02f4c3 | 155 | //pc.printf("linear pot: %f\n", potval); |
aismail1997 | 8:1aa86ad19af9 | 156 | // MAIN THREAD |
aismail1997 | 8:1aa86ad19af9 | 157 | while(true) { |
aismail1997 | 16:581a3b02f4c3 | 158 | Thread::wait(500); // wait till thread is done |
aismail1997 | 2:9eda4611081a | 159 | } |
aismail1997 | 2:9eda4611081a | 160 | } |