Fork of original senior design repo
Dependencies: SDFileSystem mbed-rtos mbed wave_player emic2
Fork of BAT_senior_design by
main.cpp@15:5c91af9b4e7c, 2017-10-25 (annotated)
- Committer:
- aismail1997
- Date:
- Wed Oct 25 14:31:33 2017 +0000
- Revision:
- 15:5c91af9b4e7c
- Parent:
- 14:7e9308d14faa
- Child:
- 16:08c575082052
Button functionality works, debouncing may still be an issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 0:9eda4611081a | 1 | #include "mbed.h" |
aismail1997 | 6:1aa86ad19af9 | 2 | #include "rtos.h" |
aismail1997 | 9:418a4437a693 | 3 | #include "wave_player.h" |
aismail1997 | 9:418a4437a693 | 4 | #include "SDFileSystem.h" |
aismail1997 | 10:21268d8bf979 | 5 | #include "button.h" |
aismail1997 | 3:9ed43e974156 | 6 | |
aismail1997 | 12:b80dde24e9bc | 7 | // DEFINE I/O |
aismail1997 | 0:9eda4611081a | 8 | PwmOut myservo(p21); |
aismail1997 | 0:9eda4611081a | 9 | DigitalIn pb1 (p20); |
aismail1997 | 13:581a3b02f4c3 | 10 | PwmOut myservo2(p22); |
aismail1997 | 13:581a3b02f4c3 | 11 | DigitalIn pb2 (p19); |
aismail1997 | 13:581a3b02f4c3 | 12 | DigitalOut led1(LED1); |
aismail1997 | 15:5c91af9b4e7c | 13 | DigitalOut led3(LED3); |
aismail1997 | 15:5c91af9b4e7c | 14 | DigitalOut led4(LED4); |
aismail1997 | 13:581a3b02f4c3 | 15 | |
aismail1997 | 0:9eda4611081a | 16 | //AnalogIn linpot(p20); |
aismail1997 | 13:581a3b02f4c3 | 17 | //Serial pc(USBTX, USBRX); |
aismail1997 | 13:581a3b02f4c3 | 18 | //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 13:581a3b02f4c3 | 19 | //AnalogOut DACout(p26); |
aismail1997 | 13:581a3b02f4c3 | 20 | //wave_player waver(&DACout); |
aismail1997 | 10:21268d8bf979 | 21 | button button1(myservo, pb1); |
aismail1997 | 13:581a3b02f4c3 | 22 | button button2(myservo2, pb2); |
aismail1997 | 4:cc181f8f2bd1 | 23 | |
aismail1997 | 12:b80dde24e9bc | 24 | // INITIALIZE VARIABLES |
aismail1997 | 12:b80dde24e9bc | 25 | // add mode, reset buttons |
aismail1997 | 9:418a4437a693 | 26 | int start = 0; |
aismail1997 | 9:418a4437a693 | 27 | int submit = 0; |
aismail1997 | 10:21268d8bf979 | 28 | int state = 2; |
aismail1997 | 15:5c91af9b4e7c | 29 | int state2 = 0; // Button begins in up state |
aismail1997 | 15:5c91af9b4e7c | 30 | ///int state2 = 2; |
aismail1997 | 13:581a3b02f4c3 | 31 | int count = 0; |
aismail1997 | 6:1aa86ad19af9 | 32 | |
aismail1997 | 9:418a4437a693 | 33 | // FUNCTIONS |
aismail1997 | 12:b80dde24e9bc | 34 | // play a file on the speaker |
aismail1997 | 13:581a3b02f4c3 | 35 | /*void playSound(wave_player waver) |
aismail1997 | 9:418a4437a693 | 36 | { |
aismail1997 | 9:418a4437a693 | 37 | FILE *wave_file; |
aismail1997 | 12:b80dde24e9bc | 38 | wave_file=fopen("/sd/lesson.wav","r"); |
aismail1997 | 9:418a4437a693 | 39 | waver.play(wave_file); |
aismail1997 | 9:418a4437a693 | 40 | fclose(wave_file); |
aismail1997 | 13:581a3b02f4c3 | 41 | }*/ |
aismail1997 | 6:1aa86ad19af9 | 42 | |
aismail1997 | 6:1aa86ad19af9 | 43 | // THREADS |
aismail1997 | 12:b80dde24e9bc | 44 | |
aismail1997 | 12:b80dde24e9bc | 45 | // thread for the custom button |
aismail1997 | 6:1aa86ad19af9 | 46 | void button_thread() |
aismail1997 | 4:cc181f8f2bd1 | 47 | { |
aismail1997 | 13:581a3b02f4c3 | 48 | while(true) { |
aismail1997 | 13:581a3b02f4c3 | 49 | // button was up and is moving down, move servo in |
aismail1997 | 13:581a3b02f4c3 | 50 | //pc.printf("in button thread"); |
aismail1997 | 13:581a3b02f4c3 | 51 | if (pb1 == 1 && state == 2) { |
aismail1997 | 13:581a3b02f4c3 | 52 | button1.moveServoIn(); |
aismail1997 | 13:581a3b02f4c3 | 53 | state = 1; |
aismail1997 | 13:581a3b02f4c3 | 54 | led1 = 0; |
aismail1997 | 13:581a3b02f4c3 | 55 | } |
aismail1997 | 13:581a3b02f4c3 | 56 | // button was down and is being pushed again, move servo out |
aismail1997 | 13:581a3b02f4c3 | 57 | else if (pb1 == 1 && state == 1) { |
aismail1997 | 13:581a3b02f4c3 | 58 | button1.moveServoOut(); |
aismail1997 | 13:581a3b02f4c3 | 59 | state = 2; |
aismail1997 | 13:581a3b02f4c3 | 60 | led1 = 1; |
aismail1997 | 13:581a3b02f4c3 | 61 | } |
aismail1997 | 13:581a3b02f4c3 | 62 | Thread::wait(100); // wait till thread is done |
aismail1997 | 10:21268d8bf979 | 63 | } |
aismail1997 | 13:581a3b02f4c3 | 64 | } |
aismail1997 | 13:581a3b02f4c3 | 65 | |
aismail1997 | 13:581a3b02f4c3 | 66 | void button2_thread() |
aismail1997 | 13:581a3b02f4c3 | 67 | { |
aismail1997 | 13:581a3b02f4c3 | 68 | // TODO: Add states |
aismail1997 | 13:581a3b02f4c3 | 69 | while(true) { |
aismail1997 | 14:7e9308d14faa | 70 | // state 0 - button is up, pb = 0 |
aismail1997 | 15:5c91af9b4e7c | 71 | if (pb2 == 0 && state2 == 3) { |
aismail1997 | 14:7e9308d14faa | 72 | // nothing happens here, servo is still |
aismail1997 | 15:5c91af9b4e7c | 73 | state2 = 0; |
aismail1997 | 15:5c91af9b4e7c | 74 | led3 = 0; |
aismail1997 | 15:5c91af9b4e7c | 75 | led4 = 0; |
aismail1997 | 15:5c91af9b4e7c | 76 | } |
aismail1997 | 14:7e9308d14faa | 77 | // state 1 - button is moving down, pb = 1 |
aismail1997 | 15:5c91af9b4e7c | 78 | if (pb2 == 1 && state2 == 0) { |
aismail1997 | 15:5c91af9b4e7c | 79 | button2.moveServoIn(); |
aismail1997 | 15:5c91af9b4e7c | 80 | state2 = 1; |
aismail1997 | 15:5c91af9b4e7c | 81 | led3 = 0; |
aismail1997 | 15:5c91af9b4e7c | 82 | led4 = 1; |
aismail1997 | 15:5c91af9b4e7c | 83 | } |
aismail1997 | 14:7e9308d14faa | 84 | // state 2 - button is down, pb = 0 |
aismail1997 | 15:5c91af9b4e7c | 85 | if (pb2 == 0 && state2 == 1) { |
aismail1997 | 14:7e9308d14faa | 86 | // nothing happens here, servo is still |
aismail1997 | 15:5c91af9b4e7c | 87 | state2 = 2; |
aismail1997 | 15:5c91af9b4e7c | 88 | led3 = 1; |
aismail1997 | 15:5c91af9b4e7c | 89 | led4 = 0; |
aismail1997 | 15:5c91af9b4e7c | 90 | } |
aismail1997 | 14:7e9308d14faa | 91 | // state 3 - button is moving up, pb = 1 |
aismail1997 | 15:5c91af9b4e7c | 92 | if (pb2 == 1 && state2 == 2) { |
aismail1997 | 15:5c91af9b4e7c | 93 | button2.moveServoOut(); |
aismail1997 | 15:5c91af9b4e7c | 94 | state2 = 3; |
aismail1997 | 15:5c91af9b4e7c | 95 | led3 = 1; |
aismail1997 | 15:5c91af9b4e7c | 96 | led4 = 1; |
aismail1997 | 15:5c91af9b4e7c | 97 | } |
aismail1997 | 14:7e9308d14faa | 98 | // state 4 - handle bouncing while button is down |
aismail1997 | 15:5c91af9b4e7c | 99 | /*if (pb2 = 1 && state2 == 2) { |
aismail1997 | 15:5c91af9b4e7c | 100 | |
aismail1997 | 15:5c91af9b4e7c | 101 | }*/ |
aismail1997 | 14:7e9308d14faa | 102 | |
aismail1997 | 14:7e9308d14faa | 103 | // button was up and is moving down, move servo in |
aismail1997 | 15:5c91af9b4e7c | 104 | /*if (pb2 == 1 && state2 == 2) { |
aismail1997 | 14:7e9308d14faa | 105 | button2.moveServoIn(); |
aismail1997 | 14:7e9308d14faa | 106 | state2 = 1; |
aismail1997 | 14:7e9308d14faa | 107 | led2 = 0; |
aismail1997 | 13:581a3b02f4c3 | 108 | } |
aismail1997 | 13:581a3b02f4c3 | 109 | // button was down and is being pushed again, move servo out |
aismail1997 | 13:581a3b02f4c3 | 110 | else if (pb2 == 1 && state2 == 1) { |
aismail1997 | 13:581a3b02f4c3 | 111 | button2.moveServoOut(); |
aismail1997 | 13:581a3b02f4c3 | 112 | state2 = 2; |
aismail1997 | 13:581a3b02f4c3 | 113 | led2 = 1; |
aismail1997 | 13:581a3b02f4c3 | 114 | } |
aismail1997 | 15:5c91af9b4e7c | 115 | */ |
aismail1997 | 13:581a3b02f4c3 | 116 | Thread::wait(100); // wait till thread is done |
aismail1997 | 10:21268d8bf979 | 117 | } |
aismail1997 | 4:cc181f8f2bd1 | 118 | } |
aismail1997 | 4:cc181f8f2bd1 | 119 | |
aismail1997 | 6:1aa86ad19af9 | 120 | void submit_thread() |
aismail1997 | 4:cc181f8f2bd1 | 121 | { |
aismail1997 | 12:b80dde24e9bc | 122 | Thread::wait(500); // wait till thread is done |
aismail1997 | 4:cc181f8f2bd1 | 123 | } |
aismail1997 | 4:cc181f8f2bd1 | 124 | |
aismail1997 | 6:1aa86ad19af9 | 125 | void start_thread() |
aismail1997 | 4:cc181f8f2bd1 | 126 | { |
aismail1997 | 6:1aa86ad19af9 | 127 | // read pb_start |
aismail1997 | 6:1aa86ad19af9 | 128 | // if 1 |
aismail1997 | 13:581a3b02f4c3 | 129 | start = 1; |
aismail1997 | 13:581a3b02f4c3 | 130 | //pc.printf("start %d ", start); |
aismail1997 | 6:1aa86ad19af9 | 131 | // else 0 |
aismail1997 | 12:b80dde24e9bc | 132 | Thread::wait(500); // wait till thread is done |
aismail1997 | 4:cc181f8f2bd1 | 133 | } |
aismail1997 | 0:9eda4611081a | 134 | |
aismail1997 | 13:581a3b02f4c3 | 135 | |
aismail1997 | 0:9eda4611081a | 136 | int main() |
aismail1997 | 0:9eda4611081a | 137 | { |
aismail1997 | 6:1aa86ad19af9 | 138 | // SETUP |
aismail1997 | 4:cc181f8f2bd1 | 139 | // pull up the pushbutton to prevent bouncing |
aismail1997 | 15:5c91af9b4e7c | 140 | pb1.mode(PullUp); |
aismail1997 | 15:5c91af9b4e7c | 141 | pb2.mode(PullUp); |
aismail1997 | 15:5c91af9b4e7c | 142 | wait(.001); |
aismail1997 | 14:7e9308d14faa | 143 | |
aismail1997 | 4:cc181f8f2bd1 | 144 | // servo begins at 30 degrees |
aismail1997 | 10:21268d8bf979 | 145 | // replace with a button setup function |
aismail1997 | 0:9eda4611081a | 146 | for(int i=0; i<=3; i++) { |
aismail1997 | 0:9eda4611081a | 147 | myservo = i/100.0; |
aismail1997 | 0:9eda4611081a | 148 | wait(0.01); |
aismail1997 | 0:9eda4611081a | 149 | } |
aismail1997 | 13:581a3b02f4c3 | 150 | for(int i=0; i<=3; i++) { |
aismail1997 | 13:581a3b02f4c3 | 151 | myservo2 = i/100.0; |
aismail1997 | 13:581a3b02f4c3 | 152 | wait(0.01); |
aismail1997 | 13:581a3b02f4c3 | 153 | } |
aismail1997 | 4:cc181f8f2bd1 | 154 | |
aismail1997 | 13:581a3b02f4c3 | 155 | led1 = 1; |
aismail1997 | 15:5c91af9b4e7c | 156 | //led2 = 1; |
aismail1997 | 13:581a3b02f4c3 | 157 | Thread t3(button_thread); |
aismail1997 | 13:581a3b02f4c3 | 158 | Thread t4(button2_thread); |
aismail1997 | 13:581a3b02f4c3 | 159 | t3.start(button_thread); |
aismail1997 | 13:581a3b02f4c3 | 160 | t4.start(button2_thread); |
aismail1997 | 13:581a3b02f4c3 | 161 | |
aismail1997 | 13:581a3b02f4c3 | 162 | // start threads for reset, mode, start |
aismail1997 | 13:581a3b02f4c3 | 163 | //Thread t1(start_thread); |
aismail1997 | 13:581a3b02f4c3 | 164 | //pc.printf("start thread"); |
aismail1997 | 13:581a3b02f4c3 | 165 | //Thread t2(submit_thread); |
aismail1997 | 13:581a3b02f4c3 | 166 | // setup SDcard and Speaker |
aismail1997 | 13:581a3b02f4c3 | 167 | |
aismail1997 | 13:581a3b02f4c3 | 168 | // when started |
aismail1997 | 13:581a3b02f4c3 | 169 | //while (start == 0){} |
aismail1997 | 13:581a3b02f4c3 | 170 | //Thread t3(button_thread); |
aismail1997 | 13:581a3b02f4c3 | 171 | //pc.printf("button thread"); |
aismail1997 | 13:581a3b02f4c3 | 172 | //Thread t4(button2_thread); |
aismail1997 | 13:581a3b02f4c3 | 173 | //pc.printf("button2 thread"); |
aismail1997 | 13:581a3b02f4c3 | 174 | |
aismail1997 | 13:581a3b02f4c3 | 175 | //Thread t3(submit_thread); |
aismail1997 | 13:581a3b02f4c3 | 176 | |
aismail1997 | 13:581a3b02f4c3 | 177 | // when submitted |
aismail1997 | 13:581a3b02f4c3 | 178 | //while (submit == 0) {} |
aismail1997 | 13:581a3b02f4c3 | 179 | |
aismail1997 | 13:581a3b02f4c3 | 180 | // start button threads and submit thread |
aismail1997 | 13:581a3b02f4c3 | 181 | // if submit close button threads and submit thread |
aismail1997 | 13:581a3b02f4c3 | 182 | // check result |
aismail1997 | 13:581a3b02f4c3 | 183 | // play results on speaker |
aismail1997 | 13:581a3b02f4c3 | 184 | // save results |
aismail1997 | 13:581a3b02f4c3 | 185 | |
aismail1997 | 13:581a3b02f4c3 | 186 | // read linear potentiometer |
aismail1997 | 13:581a3b02f4c3 | 187 | //if (linpot < 0.5) { |
aismail1997 | 13:581a3b02f4c3 | 188 | //float potval = linpot; |
aismail1997 | 13:581a3b02f4c3 | 189 | //pc.printf("linear pot: %f\n", potval); |
aismail1997 | 6:1aa86ad19af9 | 190 | // MAIN THREAD |
aismail1997 | 6:1aa86ad19af9 | 191 | while(true) { |
aismail1997 | 13:581a3b02f4c3 | 192 | Thread::wait(500); // wait till thread is done |
aismail1997 | 0:9eda4611081a | 193 | } |
aismail1997 | 0:9eda4611081a | 194 | } |