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