Working read code with mode button
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Testnew by
main.cpp@23:c5df903f068a, 2017-11-01 (annotated)
- Committer:
- aismail1997
- Date:
- Wed Nov 01 15:22:13 2017 +0000
- Revision:
- 23:c5df903f068a
- Parent:
- 21:ceac47be2e64
- Child:
- 24:6931917c70cd
Updated button and buttonarray classes
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 | 21:ceac47be2e64 | 6 | #include "buttonArray.h" |
aismail1997 | 5:9ed43e974156 | 7 | |
aismail1997 | 15:b80dde24e9bc | 8 | // DEFINE I/O |
aismail1997 | 2:9eda4611081a | 9 | PwmOut myservo(p21); |
aismail1997 | 2:9eda4611081a | 10 | DigitalIn pb1 (p20); |
aismail1997 | 16:581a3b02f4c3 | 11 | PwmOut myservo2(p22); |
aismail1997 | 16:581a3b02f4c3 | 12 | DigitalIn pb2 (p19); |
aismail1997 | 20:d14bf57f435b | 13 | PwmOut myservo3(p23); |
aismail1997 | 20:d14bf57f435b | 14 | DigitalIn pb3 (p18); |
aismail1997 | 20:d14bf57f435b | 15 | PwmOut myservo4(p24); |
aismail1997 | 20:d14bf57f435b | 16 | DigitalIn pb4 (p17); |
aismail1997 | 20:d14bf57f435b | 17 | PwmOut myservo5(p25); |
aismail1997 | 20:d14bf57f435b | 18 | DigitalIn pb5 (p16); |
aismail1997 | 20:d14bf57f435b | 19 | PwmOut myservo6(p26); |
aismail1997 | 20:d14bf57f435b | 20 | DigitalIn pb6 (p15); |
aismail1997 | 20:d14bf57f435b | 21 | |
aismail1997 | 20:d14bf57f435b | 22 | //DigitalOut led1(LED1); |
aismail1997 | 20:d14bf57f435b | 23 | //DigitalOut led3(LED3); |
aismail1997 | 23:c5df903f068a | 24 | DigitalOut led4(LED4); |
aismail1997 | 16:581a3b02f4c3 | 25 | |
aismail1997 | 2:9eda4611081a | 26 | //AnalogIn linpot(p20); |
aismail1997 | 16:581a3b02f4c3 | 27 | //Serial pc(USBTX, USBRX); |
aismail1997 | 16:581a3b02f4c3 | 28 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 16:581a3b02f4c3 | 29 | //AnalogOut DACout(p26); |
aismail1997 | 16:581a3b02f4c3 | 30 | //wave_player waver(&DACout); |
aismail1997 | 23:c5df903f068a | 31 | //button button1(myservo, pb1, linpot); |
aismail1997 | 23:c5df903f068a | 32 | button button1(myservo, pb1, 1); |
aismail1997 | 23:c5df903f068a | 33 | button button2(myservo2, pb2, 2); |
aismail1997 | 23:c5df903f068a | 34 | button button3(myservo3, pb3, 3); |
aismail1997 | 23:c5df903f068a | 35 | button button4(myservo4, pb4, 4); |
aismail1997 | 23:c5df903f068a | 36 | button button5(myservo5, pb5, 5); |
aismail1997 | 23:c5df903f068a | 37 | button button6(myservo6, pb6, 6); |
aismail1997 | 6:cc181f8f2bd1 | 38 | |
aismail1997 | 21:ceac47be2e64 | 39 | buttonArray buttonarr(button1, button2, button3, button4, button5, button6); |
aismail1997 | 21:ceac47be2e64 | 40 | |
aismail1997 | 15:b80dde24e9bc | 41 | // INITIALIZE VARIABLES |
aismail1997 | 15:b80dde24e9bc | 42 | // add mode, reset buttons |
aismail1997 | 11:418a4437a693 | 43 | int start = 0; |
aismail1997 | 11:418a4437a693 | 44 | int submit = 0; |
aismail1997 | 20:d14bf57f435b | 45 | // Buttons begins in up state |
aismail1997 | 19:08c575082052 | 46 | int state = 0; |
aismail1997 | 20:d14bf57f435b | 47 | int state2 = 0; |
aismail1997 | 20:d14bf57f435b | 48 | int state3 = 0; |
aismail1997 | 20:d14bf57f435b | 49 | int state4 = 0; |
aismail1997 | 20:d14bf57f435b | 50 | int state5 = 0; |
aismail1997 | 20:d14bf57f435b | 51 | int state6 = 0; |
aismail1997 | 16:581a3b02f4c3 | 52 | int count = 0; |
aismail1997 | 8:1aa86ad19af9 | 53 | |
aismail1997 | 11:418a4437a693 | 54 | // FUNCTIONS |
aismail1997 | 15:b80dde24e9bc | 55 | // play a file on the speaker |
aismail1997 | 16:581a3b02f4c3 | 56 | /*void playSound(wave_player waver) |
aismail1997 | 11:418a4437a693 | 57 | { |
aismail1997 | 11:418a4437a693 | 58 | FILE *wave_file; |
aismail1997 | 15:b80dde24e9bc | 59 | wave_file=fopen("/sd/lesson.wav","r"); |
aismail1997 | 11:418a4437a693 | 60 | waver.play(wave_file); |
aismail1997 | 11:418a4437a693 | 61 | fclose(wave_file); |
aismail1997 | 16:581a3b02f4c3 | 62 | }*/ |
aismail1997 | 8:1aa86ad19af9 | 63 | |
aismail1997 | 8:1aa86ad19af9 | 64 | // THREADS |
aismail1997 | 15:b80dde24e9bc | 65 | |
aismail1997 | 15:b80dde24e9bc | 66 | // thread for the custom button |
aismail1997 | 20:d14bf57f435b | 67 | //int count = 0; |
aismail1997 | 20:d14bf57f435b | 68 | |
aismail1997 | 8:1aa86ad19af9 | 69 | void button_thread() |
aismail1997 | 6:cc181f8f2bd1 | 70 | { |
aismail1997 | 16:581a3b02f4c3 | 71 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 72 | state = button1.updateState(); |
aismail1997 | 23:c5df903f068a | 73 | //led4 = button1.getLp(); |
aismail1997 | 21:ceac47be2e64 | 74 | Thread::wait(100); // wait till thread is done |
aismail1997 | 12:21268d8bf979 | 75 | } |
aismail1997 | 16:581a3b02f4c3 | 76 | } |
aismail1997 | 16:581a3b02f4c3 | 77 | |
aismail1997 | 16:581a3b02f4c3 | 78 | void button2_thread() |
aismail1997 | 16:581a3b02f4c3 | 79 | { |
aismail1997 | 16:581a3b02f4c3 | 80 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 81 | state2 = button2.updateState(); |
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 | 20:d14bf57f435b | 86 | // thread for the custom button |
aismail1997 | 20:d14bf57f435b | 87 | void button3_thread() |
aismail1997 | 20:d14bf57f435b | 88 | { |
aismail1997 | 20:d14bf57f435b | 89 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 90 | state3 = button3.updateState(); |
aismail1997 | 21:ceac47be2e64 | 91 | Thread::wait(100); // wait till thread is done |
aismail1997 | 20:d14bf57f435b | 92 | } |
aismail1997 | 20:d14bf57f435b | 93 | } |
aismail1997 | 20:d14bf57f435b | 94 | |
aismail1997 | 20:d14bf57f435b | 95 | // thread for the custom button |
aismail1997 | 20:d14bf57f435b | 96 | void button4_thread() |
aismail1997 | 20:d14bf57f435b | 97 | { |
aismail1997 | 20:d14bf57f435b | 98 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 99 | state4 = button4.updateState(); |
aismail1997 | 21:ceac47be2e64 | 100 | Thread::wait(100); // wait till thread is done |
aismail1997 | 20:d14bf57f435b | 101 | } |
aismail1997 | 20:d14bf57f435b | 102 | } |
aismail1997 | 20:d14bf57f435b | 103 | |
aismail1997 | 20:d14bf57f435b | 104 | // thread for the custom button |
aismail1997 | 20:d14bf57f435b | 105 | void button5_thread() |
aismail1997 | 20:d14bf57f435b | 106 | { |
aismail1997 | 20:d14bf57f435b | 107 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 108 | state5 = button5.updateState(); |
aismail1997 | 21:ceac47be2e64 | 109 | Thread::wait(100); // wait till thread is done |
aismail1997 | 20:d14bf57f435b | 110 | } |
aismail1997 | 20:d14bf57f435b | 111 | } |
aismail1997 | 20:d14bf57f435b | 112 | |
aismail1997 | 20:d14bf57f435b | 113 | // thread for the custom button |
aismail1997 | 20:d14bf57f435b | 114 | void button6_thread() |
aismail1997 | 20:d14bf57f435b | 115 | { |
aismail1997 | 20:d14bf57f435b | 116 | while(true) { |
aismail1997 | 21:ceac47be2e64 | 117 | state6 = button6.updateState(); |
aismail1997 | 21:ceac47be2e64 | 118 | Thread::wait(100); // wait till thread is done |
aismail1997 | 20:d14bf57f435b | 119 | } |
aismail1997 | 20:d14bf57f435b | 120 | } |
aismail1997 | 20:d14bf57f435b | 121 | |
aismail1997 | 8:1aa86ad19af9 | 122 | void submit_thread() |
aismail1997 | 6:cc181f8f2bd1 | 123 | { |
aismail1997 | 15:b80dde24e9bc | 124 | Thread::wait(500); // wait till thread is done |
aismail1997 | 6:cc181f8f2bd1 | 125 | } |
aismail1997 | 6:cc181f8f2bd1 | 126 | |
aismail1997 | 8:1aa86ad19af9 | 127 | void start_thread() |
aismail1997 | 6:cc181f8f2bd1 | 128 | { |
aismail1997 | 8:1aa86ad19af9 | 129 | // read pb_start |
aismail1997 | 8:1aa86ad19af9 | 130 | // if 1 |
aismail1997 | 16:581a3b02f4c3 | 131 | start = 1; |
aismail1997 | 16:581a3b02f4c3 | 132 | //pc.printf("start %d ", start); |
aismail1997 | 8:1aa86ad19af9 | 133 | // else 0 |
aismail1997 | 15:b80dde24e9bc | 134 | Thread::wait(500); // wait till thread is done |
aismail1997 | 6:cc181f8f2bd1 | 135 | } |
aismail1997 | 2:9eda4611081a | 136 | |
aismail1997 | 16:581a3b02f4c3 | 137 | |
aismail1997 | 2:9eda4611081a | 138 | int main() |
aismail1997 | 2:9eda4611081a | 139 | { |
aismail1997 | 8:1aa86ad19af9 | 140 | // SETUP |
aismail1997 | 6:cc181f8f2bd1 | 141 | // pull up the pushbutton to prevent bouncing |
aismail1997 | 18:5c91af9b4e7c | 142 | pb1.mode(PullUp); |
aismail1997 | 18:5c91af9b4e7c | 143 | pb2.mode(PullUp); |
aismail1997 | 20:d14bf57f435b | 144 | pb3.mode(PullUp); |
aismail1997 | 20:d14bf57f435b | 145 | pb4.mode(PullUp); |
aismail1997 | 20:d14bf57f435b | 146 | pb5.mode(PullUp); |
aismail1997 | 20:d14bf57f435b | 147 | pb6.mode(PullUp); |
aismail1997 | 18:5c91af9b4e7c | 148 | wait(.001); |
aismail1997 | 17:7e9308d14faa | 149 | |
aismail1997 | 6:cc181f8f2bd1 | 150 | // servo begins at 30 degrees |
aismail1997 | 12:21268d8bf979 | 151 | // replace with a button setup function |
aismail1997 | 2:9eda4611081a | 152 | for(int i=0; i<=3; i++) { |
aismail1997 | 2:9eda4611081a | 153 | myservo = i/100.0; |
aismail1997 | 2:9eda4611081a | 154 | wait(0.01); |
aismail1997 | 2:9eda4611081a | 155 | } |
aismail1997 | 16:581a3b02f4c3 | 156 | for(int i=0; i<=3; i++) { |
aismail1997 | 16:581a3b02f4c3 | 157 | myservo2 = i/100.0; |
aismail1997 | 16:581a3b02f4c3 | 158 | wait(0.01); |
aismail1997 | 16:581a3b02f4c3 | 159 | } |
aismail1997 | 21:ceac47be2e64 | 160 | |
aismail1997 | 20:d14bf57f435b | 161 | for(int i=0; i<=3; i++) { |
aismail1997 | 20:d14bf57f435b | 162 | myservo3 = i/100.0; |
aismail1997 | 20:d14bf57f435b | 163 | wait(0.01); |
aismail1997 | 20:d14bf57f435b | 164 | } |
aismail1997 | 21:ceac47be2e64 | 165 | |
aismail1997 | 20:d14bf57f435b | 166 | for(int i=0; i<=3; i++) { |
aismail1997 | 20:d14bf57f435b | 167 | myservo4 = i/100.0; |
aismail1997 | 20:d14bf57f435b | 168 | wait(0.01); |
aismail1997 | 20:d14bf57f435b | 169 | } |
aismail1997 | 21:ceac47be2e64 | 170 | |
aismail1997 | 20:d14bf57f435b | 171 | for(int i=0; i<=3; i++) { |
aismail1997 | 20:d14bf57f435b | 172 | myservo5 = i/100.0; |
aismail1997 | 20:d14bf57f435b | 173 | wait(0.01); |
aismail1997 | 20:d14bf57f435b | 174 | } |
aismail1997 | 6:cc181f8f2bd1 | 175 | |
aismail1997 | 20:d14bf57f435b | 176 | for(int i=0; i<=3; i++) { |
aismail1997 | 20:d14bf57f435b | 177 | myservo6 = i/100.0; |
aismail1997 | 20:d14bf57f435b | 178 | wait(0.01); |
aismail1997 | 20:d14bf57f435b | 179 | } |
aismail1997 | 21:ceac47be2e64 | 180 | |
aismail1997 | 20:d14bf57f435b | 181 | //led1 = 1; |
aismail1997 | 18:5c91af9b4e7c | 182 | //led2 = 1; |
aismail1997 | 20:d14bf57f435b | 183 | Thread t1(button_thread); |
aismail1997 | 20:d14bf57f435b | 184 | Thread t2(button2_thread); |
aismail1997 | 20:d14bf57f435b | 185 | Thread t3(button3_thread); |
aismail1997 | 20:d14bf57f435b | 186 | Thread t4(button4_thread); |
aismail1997 | 20:d14bf57f435b | 187 | Thread t5(button5_thread); |
aismail1997 | 20:d14bf57f435b | 188 | Thread t6(button6_thread); |
aismail1997 | 20:d14bf57f435b | 189 | t1.start(button_thread); |
aismail1997 | 20:d14bf57f435b | 190 | t2.start(button2_thread); |
aismail1997 | 20:d14bf57f435b | 191 | t3.start(button3_thread); |
aismail1997 | 20:d14bf57f435b | 192 | t4.start(button4_thread); |
aismail1997 | 20:d14bf57f435b | 193 | t5.start(button5_thread); |
aismail1997 | 20:d14bf57f435b | 194 | t6.start(button6_thread); |
aismail1997 | 16:581a3b02f4c3 | 195 | |
aismail1997 | 16:581a3b02f4c3 | 196 | // start threads for reset, mode, start |
aismail1997 | 16:581a3b02f4c3 | 197 | //Thread t1(start_thread); |
aismail1997 | 16:581a3b02f4c3 | 198 | //pc.printf("start thread"); |
aismail1997 | 16:581a3b02f4c3 | 199 | //Thread t2(submit_thread); |
aismail1997 | 16:581a3b02f4c3 | 200 | // setup SDcard and Speaker |
aismail1997 | 16:581a3b02f4c3 | 201 | |
aismail1997 | 16:581a3b02f4c3 | 202 | // when started |
aismail1997 | 16:581a3b02f4c3 | 203 | //while (start == 0){} |
aismail1997 | 16:581a3b02f4c3 | 204 | //Thread t3(button_thread); |
aismail1997 | 16:581a3b02f4c3 | 205 | //pc.printf("button thread"); |
aismail1997 | 16:581a3b02f4c3 | 206 | //Thread t4(button2_thread); |
aismail1997 | 16:581a3b02f4c3 | 207 | //pc.printf("button2 thread"); |
aismail1997 | 16:581a3b02f4c3 | 208 | |
aismail1997 | 16:581a3b02f4c3 | 209 | //Thread t3(submit_thread); |
aismail1997 | 16:581a3b02f4c3 | 210 | |
aismail1997 | 16:581a3b02f4c3 | 211 | // when submitted |
aismail1997 | 16:581a3b02f4c3 | 212 | //while (submit == 0) {} |
aismail1997 | 16:581a3b02f4c3 | 213 | |
aismail1997 | 16:581a3b02f4c3 | 214 | // start button threads and submit thread |
aismail1997 | 16:581a3b02f4c3 | 215 | // if submit close button threads and submit thread |
aismail1997 | 16:581a3b02f4c3 | 216 | // check result |
aismail1997 | 16:581a3b02f4c3 | 217 | // play results on speaker |
aismail1997 | 16:581a3b02f4c3 | 218 | // save results |
aismail1997 | 16:581a3b02f4c3 | 219 | |
aismail1997 | 16:581a3b02f4c3 | 220 | // read linear potentiometer |
aismail1997 | 16:581a3b02f4c3 | 221 | //if (linpot < 0.5) { |
aismail1997 | 16:581a3b02f4c3 | 222 | //float potval = linpot; |
aismail1997 | 16:581a3b02f4c3 | 223 | //pc.printf("linear pot: %f\n", potval); |
aismail1997 | 21:ceac47be2e64 | 224 | |
aismail1997 | 8:1aa86ad19af9 | 225 | // MAIN THREAD |
aismail1997 | 8:1aa86ad19af9 | 226 | while(true) { |
aismail1997 | 16:581a3b02f4c3 | 227 | Thread::wait(500); // wait till thread is done |
aismail1997 | 2:9eda4611081a | 228 | } |
aismail1997 | 2:9eda4611081a | 229 | } |