Fixed wait time

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Nhi by BAT

Committer:
aismail1997
Date:
Fri Oct 27 15:15:00 2017 +0000
Revision:
19:ceac47be2e64
Parent:
18:d14bf57f435b
Child:
21:c5df903f068a
cleaned up button code, added buttonarray class

Who changed what in which revision?

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