![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Fixed wait time
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Nhi by
button.cpp@32:e87bb6e83072, 2017-11-20 (annotated)
- Committer:
- nnguyen45
- Date:
- Mon Nov 20 15:46:11 2017 +0000
- Revision:
- 32:e87bb6e83072
- Parent:
- 27:b2e53ce54b3e
- Child:
- 33:3090ec93b4aa
code demo to Dr. Howard
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 10:21268d8bf979 | 1 | #include "mbed.h" |
aismail1997 | 10:21268d8bf979 | 2 | #include "button.h" |
aismail1997 | 22:6931917c70cd | 3 | #include "emic2.h" |
aismail1997 | 22:6931917c70cd | 4 | |
aismail1997 | 23:23970cf718ee | 5 | //emic2 myTTS(p28, p27); //serial RX,TX pins to emic |
nnguyen45 | 32:e87bb6e83072 | 6 | DigitalOut led4(LED4); |
nnguyen45 | 32:e87bb6e83072 | 7 | DigitalOut led3(LED3); |
nnguyen45 | 32:e87bb6e83072 | 8 | DigitalOut led2(LED2); |
aismail1997 | 10:21268d8bf979 | 9 | |
aismail1997 | 13:b80dde24e9bc | 10 | // button constructor |
aismail1997 | 21:c5df903f068a | 11 | button::button(PwmOut servo, DigitalIn pb, int id) |
aismail1997 | 22:6931917c70cd | 12 | : servo(servo), pb(pb), state(0), press(0), id(id) {} |
aismail1997 | 21:c5df903f068a | 13 | /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp) |
aismail1997 | 21:c5df903f068a | 14 | : servo(servo), pb(pb), linpot(lp), press(0), state(0) {} |
aismail1997 | 21:c5df903f068a | 15 | */ |
aismail1997 | 10:21268d8bf979 | 16 | |
aismail1997 | 10:21268d8bf979 | 17 | // FUNCTIONS |
aismail1997 | 13:b80dde24e9bc | 18 | |
aismail1997 | 13:b80dde24e9bc | 19 | // get servo pin |
aismail1997 | 10:21268d8bf979 | 20 | PwmOut button::getServoPin() |
aismail1997 | 10:21268d8bf979 | 21 | { |
aismail1997 | 10:21268d8bf979 | 22 | return servo; |
aismail1997 | 10:21268d8bf979 | 23 | } |
aismail1997 | 10:21268d8bf979 | 24 | |
aismail1997 | 14:581a3b02f4c3 | 25 | // get servo pin |
aismail1997 | 19:ceac47be2e64 | 26 | void button::setState(int mystate) |
aismail1997 | 14:581a3b02f4c3 | 27 | { |
aismail1997 | 14:581a3b02f4c3 | 28 | state = mystate; |
aismail1997 | 14:581a3b02f4c3 | 29 | } |
aismail1997 | 14:581a3b02f4c3 | 30 | |
aismail1997 | 21:c5df903f068a | 31 | void button::setPress(int mypress) |
aismail1997 | 21:c5df903f068a | 32 | { |
aismail1997 | 21:c5df903f068a | 33 | press = mypress; |
aismail1997 | 21:c5df903f068a | 34 | } |
aismail1997 | 21:c5df903f068a | 35 | |
aismail1997 | 14:581a3b02f4c3 | 36 | // get servo pin |
aismail1997 | 19:ceac47be2e64 | 37 | /*void button::setMode(int mymode) |
aismail1997 | 14:581a3b02f4c3 | 38 | { |
aismail1997 | 14:581a3b02f4c3 | 39 | mode = mymode; |
aismail1997 | 14:581a3b02f4c3 | 40 | }*/ |
aismail1997 | 14:581a3b02f4c3 | 41 | |
aismail1997 | 13:b80dde24e9bc | 42 | // get current state of the button |
aismail1997 | 19:ceac47be2e64 | 43 | int button::getState() |
aismail1997 | 10:21268d8bf979 | 44 | { |
aismail1997 | 10:21268d8bf979 | 45 | return state; |
aismail1997 | 19:ceac47be2e64 | 46 | } |
aismail1997 | 19:ceac47be2e64 | 47 | |
aismail1997 | 21:c5df903f068a | 48 | int button::getID() |
aismail1997 | 21:c5df903f068a | 49 | { |
aismail1997 | 21:c5df903f068a | 50 | return id; |
aismail1997 | 21:c5df903f068a | 51 | } |
aismail1997 | 21:c5df903f068a | 52 | |
aismail1997 | 21:c5df903f068a | 53 | |
aismail1997 | 19:ceac47be2e64 | 54 | int button::getPress() |
aismail1997 | 19:ceac47be2e64 | 55 | { |
aismail1997 | 19:ceac47be2e64 | 56 | return press; |
aismail1997 | 19:ceac47be2e64 | 57 | } |
aismail1997 | 19:ceac47be2e64 | 58 | |
aismail1997 | 21:c5df903f068a | 59 | // get current state of the button |
aismail1997 | 21:c5df903f068a | 60 | int button::getLp() |
aismail1997 | 21:c5df903f068a | 61 | { |
aismail1997 | 22:6931917c70cd | 62 | /* if (linpot < 2) |
aismail1997 | 22:6931917c70cd | 63 | return 1; |
aismail1997 | 22:6931917c70cd | 64 | else*/ |
aismail1997 | 22:6931917c70cd | 65 | return 0; |
aismail1997 | 21:c5df903f068a | 66 | } |
aismail1997 | 10:21268d8bf979 | 67 | |
aismail1997 | 13:b80dde24e9bc | 68 | // move servo into the slot |
aismail1997 | 10:21268d8bf979 | 69 | void button::moveServoIn() |
aismail1997 | 10:21268d8bf979 | 70 | { |
aismail1997 | 10:21268d8bf979 | 71 | //myled = 1; |
aismail1997 | 10:21268d8bf979 | 72 | // rotate 90 degrees one way |
aismail1997 | 10:21268d8bf979 | 73 | for(int i=3; i<=7; i++) { |
aismail1997 | 10:21268d8bf979 | 74 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 75 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 76 | } |
nnguyen45 | 32:e87bb6e83072 | 77 | switch (id) { |
nnguyen45 | 32:e87bb6e83072 | 78 | case 1: |
nnguyen45 | 32:e87bb6e83072 | 79 | led2 = 0; |
nnguyen45 | 32:e87bb6e83072 | 80 | led3 = 0; |
nnguyen45 | 32:e87bb6e83072 | 81 | led4 = 1; |
nnguyen45 | 32:e87bb6e83072 | 82 | break; |
nnguyen45 | 32:e87bb6e83072 | 83 | case 2: |
nnguyen45 | 32:e87bb6e83072 | 84 | led2 = 0; |
nnguyen45 | 32:e87bb6e83072 | 85 | led3 = 1; |
nnguyen45 | 32:e87bb6e83072 | 86 | led4 = 0; |
nnguyen45 | 32:e87bb6e83072 | 87 | break; |
nnguyen45 | 32:e87bb6e83072 | 88 | case 3: |
nnguyen45 | 32:e87bb6e83072 | 89 | led2 = 0; |
nnguyen45 | 32:e87bb6e83072 | 90 | led3 = 1; |
nnguyen45 | 32:e87bb6e83072 | 91 | led4 = 1; |
nnguyen45 | 32:e87bb6e83072 | 92 | break; |
nnguyen45 | 32:e87bb6e83072 | 93 | case 4: |
nnguyen45 | 32:e87bb6e83072 | 94 | led2 = 1; |
nnguyen45 | 32:e87bb6e83072 | 95 | led3 = 0; |
nnguyen45 | 32:e87bb6e83072 | 96 | led4 = 0; |
nnguyen45 | 32:e87bb6e83072 | 97 | break; |
nnguyen45 | 32:e87bb6e83072 | 98 | case 5: |
nnguyen45 | 32:e87bb6e83072 | 99 | led2 = 1; |
nnguyen45 | 32:e87bb6e83072 | 100 | led3 = 0; |
nnguyen45 | 32:e87bb6e83072 | 101 | led4 = 1; |
nnguyen45 | 32:e87bb6e83072 | 102 | break; |
nnguyen45 | 32:e87bb6e83072 | 103 | case 6: |
nnguyen45 | 32:e87bb6e83072 | 104 | led2 = 1; |
nnguyen45 | 32:e87bb6e83072 | 105 | led3 = 1; |
nnguyen45 | 32:e87bb6e83072 | 106 | led4 = 0; |
nnguyen45 | 32:e87bb6e83072 | 107 | break; |
nnguyen45 | 32:e87bb6e83072 | 108 | } |
aismail1997 | 10:21268d8bf979 | 109 | } |
aismail1997 | 10:21268d8bf979 | 110 | |
aismail1997 | 13:b80dde24e9bc | 111 | // move servo out of the slot |
aismail1997 | 10:21268d8bf979 | 112 | void button::moveServoOut() |
aismail1997 | 10:21268d8bf979 | 113 | { |
aismail1997 | 10:21268d8bf979 | 114 | //myled = 0; |
aismail1997 | 10:21268d8bf979 | 115 | for(int i=7; i>3; i--) { |
aismail1997 | 10:21268d8bf979 | 116 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 117 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 118 | } |
nnguyen45 | 32:e87bb6e83072 | 119 | led2 = 0; |
nnguyen45 | 32:e87bb6e83072 | 120 | led3 = 0; |
nnguyen45 | 32:e87bb6e83072 | 121 | led4 = 0; |
aismail1997 | 10:21268d8bf979 | 122 | } |
aismail1997 | 19:ceac47be2e64 | 123 | |
aismail1997 | 19:ceac47be2e64 | 124 | int button::updateState() |
aismail1997 | 19:ceac47be2e64 | 125 | { |
aismail1997 | 19:ceac47be2e64 | 126 | //myled = 0; |
aismail1997 | 19:ceac47be2e64 | 127 | // state 0 - button is up, pb = 0 |
aismail1997 | 19:ceac47be2e64 | 128 | if (pb == 0 && state == 3) { |
aismail1997 | 19:ceac47be2e64 | 129 | // nothing happens here, servo is still |
aismail1997 | 19:ceac47be2e64 | 130 | state = 0; |
aismail1997 | 19:ceac47be2e64 | 131 | } |
aismail1997 | 19:ceac47be2e64 | 132 | // state 1 - button is moving down, pb = 1 |
aismail1997 | 19:ceac47be2e64 | 133 | if (pb == 1 && state == 0) { |
aismail1997 | 19:ceac47be2e64 | 134 | moveServoIn(); |
aismail1997 | 19:ceac47be2e64 | 135 | state = 1; |
aismail1997 | 19:ceac47be2e64 | 136 | press = 1; |
aismail1997 | 22:6931917c70cd | 137 | |
aismail1997 | 22:6931917c70cd | 138 | // Speaker says stuff |
aismail1997 | 22:6931917c70cd | 139 | /*myTTS.volume(18); //max volume |
aismail1997 | 22:6931917c70cd | 140 | myTTS.speakf("S");//Speak command starts with "S" |
aismail1997 | 22:6931917c70cd | 141 | myTTS.speakf("Hey, you pressed a pin!"); // Send the desired string to convert to speech |
aismail1997 | 22:6931917c70cd | 142 | myTTS.speakf("\r"); //marks end of speak command*/ |
aismail1997 | 19:ceac47be2e64 | 143 | } |
aismail1997 | 19:ceac47be2e64 | 144 | // state 2 - button is down, pb = 0 |
aismail1997 | 19:ceac47be2e64 | 145 | if (pb == 0 && state == 1) { |
aismail1997 | 19:ceac47be2e64 | 146 | // nothing happens here, servo is still |
aismail1997 | 19:ceac47be2e64 | 147 | state = 2; |
aismail1997 | 19:ceac47be2e64 | 148 | } |
aismail1997 | 19:ceac47be2e64 | 149 | // state 3 - button is moving up, pb = 1 |
aismail1997 | 19:ceac47be2e64 | 150 | if (pb == 1 && state == 2) { |
aismail1997 | 19:ceac47be2e64 | 151 | moveServoOut(); |
aismail1997 | 19:ceac47be2e64 | 152 | state = 3; |
aismail1997 | 19:ceac47be2e64 | 153 | press = 0; |
aismail1997 | 19:ceac47be2e64 | 154 | } |
aismail1997 | 19:ceac47be2e64 | 155 | // state 4 - handle debouncing while button is down |
aismail1997 | 19:ceac47be2e64 | 156 | /*if (pb1 = 1 && state == 2) { |
aismail1997 | 19:ceac47be2e64 | 157 | count++; |
aismail1997 | 19:ceac47be2e64 | 158 | }*/ |
aismail1997 | 19:ceac47be2e64 | 159 | return state; |
aismail1997 | 19:ceac47be2e64 | 160 | } |
aismail1997 | 19:ceac47be2e64 | 161 | |
aismail1997 | 27:b2e53ce54b3e | 162 | void button::setup() { |
aismail1997 | 27:b2e53ce54b3e | 163 | for(int i=0; i<=3; i++) { |
aismail1997 | 27:b2e53ce54b3e | 164 | servo = i/100.0; |
aismail1997 | 27:b2e53ce54b3e | 165 | wait(0.01); |
aismail1997 | 27:b2e53ce54b3e | 166 | } |
aismail1997 | 27:b2e53ce54b3e | 167 | } |