Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
aismail1997
Date:
Sun Oct 22 22:34:06 2017 +0000
Revision:
17:7e9308d14faa
Parent:
16:581a3b02f4c3
Child:
18:5c91af9b4e7c
added commented out code for new states

Who changed what in which revision?

UserRevisionLine numberNew 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 17:7e9308d14faa 69 // state 0 - button is up, pb = 0
aismail1997 17:7e9308d14faa 70 // if (pb == 0 && state == 3) {
aismail1997 17:7e9308d14faa 71 // nothing happens here, servo is still
aismail1997 17:7e9308d14faa 72 // state2 = 0;
aismail1997 17:7e9308d14faa 73 // }
aismail1997 17:7e9308d14faa 74 // state 1 - button is moving down, pb = 1
aismail1997 17:7e9308d14faa 75 // if (pb == 1 && state == 0) {
aismail1997 17:7e9308d14faa 76 //button2.moveServoIn();
aismail1997 17:7e9308d14faa 77 //state2 = 1;
aismail1997 17:7e9308d14faa 78 // }
aismail1997 17:7e9308d14faa 79 // state 2 - button is down, pb = 0
aismail1997 17:7e9308d14faa 80 // if (pb == 0 && state == 1) {
aismail1997 17:7e9308d14faa 81 // nothing happens here, servo is still
aismail1997 17:7e9308d14faa 82 // state2 = 2;
aismail1997 17:7e9308d14faa 83 // }
aismail1997 17:7e9308d14faa 84 // state 3 - button is moving up, pb = 1
aismail1997 17:7e9308d14faa 85 // if (pb == 1 && state == 2) {
aismail1997 17:7e9308d14faa 86 //button2.moveServoOut();
aismail1997 17:7e9308d14faa 87 //state2 = 3;
aismail1997 17:7e9308d14faa 88 // }
aismail1997 17:7e9308d14faa 89 // state 4 - handle bouncing while button is down
aismail1997 17:7e9308d14faa 90
aismail1997 17:7e9308d14faa 91
aismail1997 17:7e9308d14faa 92 // button was up and is moving down, move servo in
aismail1997 16:581a3b02f4c3 93 if (pb2 == 1 && state2 == 2) {
aismail1997 17:7e9308d14faa 94 button2.moveServoIn();
aismail1997 17:7e9308d14faa 95 state2 = 1;
aismail1997 17:7e9308d14faa 96 led2 = 0;
aismail1997 16:581a3b02f4c3 97 }
aismail1997 16:581a3b02f4c3 98 // button was down and is being pushed again, move servo out
aismail1997 16:581a3b02f4c3 99 else if (pb2 == 1 && state2 == 1) {
aismail1997 16:581a3b02f4c3 100 button2.moveServoOut();
aismail1997 16:581a3b02f4c3 101 state2 = 2;
aismail1997 16:581a3b02f4c3 102 led2 = 1;
aismail1997 16:581a3b02f4c3 103 }
aismail1997 16:581a3b02f4c3 104 Thread::wait(100); // wait till thread is done
aismail1997 12:21268d8bf979 105 }
aismail1997 6:cc181f8f2bd1 106 }
aismail1997 6:cc181f8f2bd1 107
aismail1997 8:1aa86ad19af9 108 void submit_thread()
aismail1997 6:cc181f8f2bd1 109 {
aismail1997 15:b80dde24e9bc 110 Thread::wait(500); // wait till thread is done
aismail1997 6:cc181f8f2bd1 111 }
aismail1997 6:cc181f8f2bd1 112
aismail1997 8:1aa86ad19af9 113 void start_thread()
aismail1997 6:cc181f8f2bd1 114 {
aismail1997 8:1aa86ad19af9 115 // read pb_start
aismail1997 8:1aa86ad19af9 116 // if 1
aismail1997 16:581a3b02f4c3 117 start = 1;
aismail1997 16:581a3b02f4c3 118 //pc.printf("start %d ", start);
aismail1997 8:1aa86ad19af9 119 // else 0
aismail1997 15:b80dde24e9bc 120 Thread::wait(500); // wait till thread is done
aismail1997 6:cc181f8f2bd1 121 }
aismail1997 2:9eda4611081a 122
aismail1997 16:581a3b02f4c3 123
aismail1997 2:9eda4611081a 124 int main()
aismail1997 2:9eda4611081a 125 {
aismail1997 8:1aa86ad19af9 126 // SETUP
aismail1997 6:cc181f8f2bd1 127 // pull up the pushbutton to prevent bouncing
aismail1997 16:581a3b02f4c3 128 //pb1.mode(PullUp);
aismail1997 16:581a3b02f4c3 129 //pb2.mode(PullUp);
aismail1997 16:581a3b02f4c3 130 //wait(.001);
aismail1997 17:7e9308d14faa 131
aismail1997 6:cc181f8f2bd1 132 // servo begins at 30 degrees
aismail1997 12:21268d8bf979 133 // replace with a button setup function
aismail1997 2:9eda4611081a 134 for(int i=0; i<=3; i++) {
aismail1997 2:9eda4611081a 135 myservo = i/100.0;
aismail1997 2:9eda4611081a 136 wait(0.01);
aismail1997 2:9eda4611081a 137 }
aismail1997 16:581a3b02f4c3 138 for(int i=0; i<=3; i++) {
aismail1997 16:581a3b02f4c3 139 myservo2 = i/100.0;
aismail1997 16:581a3b02f4c3 140 wait(0.01);
aismail1997 16:581a3b02f4c3 141 }
aismail1997 6:cc181f8f2bd1 142
aismail1997 16:581a3b02f4c3 143 led1 = 1;
aismail1997 16:581a3b02f4c3 144 led2 = 1;
aismail1997 16:581a3b02f4c3 145 Thread t3(button_thread);
aismail1997 16:581a3b02f4c3 146 Thread t4(button2_thread);
aismail1997 16:581a3b02f4c3 147 t3.start(button_thread);
aismail1997 16:581a3b02f4c3 148 t4.start(button2_thread);
aismail1997 16:581a3b02f4c3 149
aismail1997 16:581a3b02f4c3 150 // start threads for reset, mode, start
aismail1997 16:581a3b02f4c3 151 //Thread t1(start_thread);
aismail1997 16:581a3b02f4c3 152 //pc.printf("start thread");
aismail1997 16:581a3b02f4c3 153 //Thread t2(submit_thread);
aismail1997 16:581a3b02f4c3 154 // setup SDcard and Speaker
aismail1997 16:581a3b02f4c3 155
aismail1997 16:581a3b02f4c3 156 // when started
aismail1997 16:581a3b02f4c3 157 //while (start == 0){}
aismail1997 16:581a3b02f4c3 158 //Thread t3(button_thread);
aismail1997 16:581a3b02f4c3 159 //pc.printf("button thread");
aismail1997 16:581a3b02f4c3 160 //Thread t4(button2_thread);
aismail1997 16:581a3b02f4c3 161 //pc.printf("button2 thread");
aismail1997 16:581a3b02f4c3 162
aismail1997 16:581a3b02f4c3 163 //Thread t3(submit_thread);
aismail1997 16:581a3b02f4c3 164
aismail1997 16:581a3b02f4c3 165 // when submitted
aismail1997 16:581a3b02f4c3 166 //while (submit == 0) {}
aismail1997 16:581a3b02f4c3 167
aismail1997 16:581a3b02f4c3 168 // start button threads and submit thread
aismail1997 16:581a3b02f4c3 169 // if submit close button threads and submit thread
aismail1997 16:581a3b02f4c3 170 // check result
aismail1997 16:581a3b02f4c3 171 // play results on speaker
aismail1997 16:581a3b02f4c3 172 // save results
aismail1997 16:581a3b02f4c3 173
aismail1997 16:581a3b02f4c3 174 // read linear potentiometer
aismail1997 16:581a3b02f4c3 175 //if (linpot < 0.5) {
aismail1997 16:581a3b02f4c3 176 //float potval = linpot;
aismail1997 16:581a3b02f4c3 177 //pc.printf("linear pot: %f\n", potval);
aismail1997 8:1aa86ad19af9 178 // MAIN THREAD
aismail1997 8:1aa86ad19af9 179 while(true) {
aismail1997 16:581a3b02f4c3 180 Thread::wait(500); // wait till thread is done
aismail1997 2:9eda4611081a 181 }
aismail1997 2:9eda4611081a 182 }