![](/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@22:6931917c70cd, 2017-11-03 (annotated)
- Committer:
- aismail1997
- Date:
- Fri Nov 03 18:56:01 2017 +0000
- Revision:
- 22:6931917c70cd
- Parent:
- 21:c5df903f068a
- Child:
- 23:23970cf718ee
speaker and lin pot code commented out
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 | 22:6931917c70cd | 5 | emic2 myTTS(p28, p27); //serial RX,TX pins to emic |
aismail1997 | 22:6931917c70cd | 6 | //DigitalOut led4(LED4); |
aismail1997 | 10:21268d8bf979 | 7 | |
aismail1997 | 13:b80dde24e9bc | 8 | // button constructor |
aismail1997 | 21:c5df903f068a | 9 | button::button(PwmOut servo, DigitalIn pb, int id) |
aismail1997 | 22:6931917c70cd | 10 | : servo(servo), pb(pb), state(0), press(0), id(id) {} |
aismail1997 | 21:c5df903f068a | 11 | /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp) |
aismail1997 | 21:c5df903f068a | 12 | : servo(servo), pb(pb), linpot(lp), press(0), state(0) {} |
aismail1997 | 21:c5df903f068a | 13 | */ |
aismail1997 | 10:21268d8bf979 | 14 | |
aismail1997 | 10:21268d8bf979 | 15 | // FUNCTIONS |
aismail1997 | 13:b80dde24e9bc | 16 | |
aismail1997 | 13:b80dde24e9bc | 17 | // get servo pin |
aismail1997 | 10:21268d8bf979 | 18 | PwmOut button::getServoPin() |
aismail1997 | 10:21268d8bf979 | 19 | { |
aismail1997 | 10:21268d8bf979 | 20 | return servo; |
aismail1997 | 10:21268d8bf979 | 21 | } |
aismail1997 | 10:21268d8bf979 | 22 | |
aismail1997 | 14:581a3b02f4c3 | 23 | // get servo pin |
aismail1997 | 19:ceac47be2e64 | 24 | void button::setState(int mystate) |
aismail1997 | 14:581a3b02f4c3 | 25 | { |
aismail1997 | 14:581a3b02f4c3 | 26 | state = mystate; |
aismail1997 | 14:581a3b02f4c3 | 27 | } |
aismail1997 | 14:581a3b02f4c3 | 28 | |
aismail1997 | 21:c5df903f068a | 29 | void button::setPress(int mypress) |
aismail1997 | 21:c5df903f068a | 30 | { |
aismail1997 | 21:c5df903f068a | 31 | press = mypress; |
aismail1997 | 21:c5df903f068a | 32 | } |
aismail1997 | 21:c5df903f068a | 33 | |
aismail1997 | 14:581a3b02f4c3 | 34 | // get servo pin |
aismail1997 | 19:ceac47be2e64 | 35 | /*void button::setMode(int mymode) |
aismail1997 | 14:581a3b02f4c3 | 36 | { |
aismail1997 | 14:581a3b02f4c3 | 37 | mode = mymode; |
aismail1997 | 14:581a3b02f4c3 | 38 | }*/ |
aismail1997 | 14:581a3b02f4c3 | 39 | |
aismail1997 | 13:b80dde24e9bc | 40 | // get current state of the button |
aismail1997 | 19:ceac47be2e64 | 41 | int button::getState() |
aismail1997 | 10:21268d8bf979 | 42 | { |
aismail1997 | 10:21268d8bf979 | 43 | return state; |
aismail1997 | 19:ceac47be2e64 | 44 | } |
aismail1997 | 19:ceac47be2e64 | 45 | |
aismail1997 | 21:c5df903f068a | 46 | int button::getID() |
aismail1997 | 21:c5df903f068a | 47 | { |
aismail1997 | 21:c5df903f068a | 48 | return id; |
aismail1997 | 21:c5df903f068a | 49 | } |
aismail1997 | 21:c5df903f068a | 50 | |
aismail1997 | 21:c5df903f068a | 51 | |
aismail1997 | 19:ceac47be2e64 | 52 | int button::getPress() |
aismail1997 | 19:ceac47be2e64 | 53 | { |
aismail1997 | 19:ceac47be2e64 | 54 | return press; |
aismail1997 | 19:ceac47be2e64 | 55 | } |
aismail1997 | 19:ceac47be2e64 | 56 | |
aismail1997 | 21:c5df903f068a | 57 | // get current state of the button |
aismail1997 | 21:c5df903f068a | 58 | int button::getLp() |
aismail1997 | 21:c5df903f068a | 59 | { |
aismail1997 | 22:6931917c70cd | 60 | /* if (linpot < 2) |
aismail1997 | 22:6931917c70cd | 61 | return 1; |
aismail1997 | 22:6931917c70cd | 62 | else*/ |
aismail1997 | 22:6931917c70cd | 63 | return 0; |
aismail1997 | 21:c5df903f068a | 64 | } |
aismail1997 | 10:21268d8bf979 | 65 | |
aismail1997 | 13:b80dde24e9bc | 66 | // move servo into the slot |
aismail1997 | 10:21268d8bf979 | 67 | void button::moveServoIn() |
aismail1997 | 10:21268d8bf979 | 68 | { |
aismail1997 | 10:21268d8bf979 | 69 | //myled = 1; |
aismail1997 | 10:21268d8bf979 | 70 | // rotate 90 degrees one way |
aismail1997 | 10:21268d8bf979 | 71 | for(int i=3; i<=7; i++) { |
aismail1997 | 10:21268d8bf979 | 72 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 73 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 74 | } |
aismail1997 | 10:21268d8bf979 | 75 | } |
aismail1997 | 10:21268d8bf979 | 76 | |
aismail1997 | 13:b80dde24e9bc | 77 | // move servo out of the slot |
aismail1997 | 10:21268d8bf979 | 78 | void button::moveServoOut() |
aismail1997 | 10:21268d8bf979 | 79 | { |
aismail1997 | 10:21268d8bf979 | 80 | //myled = 0; |
aismail1997 | 10:21268d8bf979 | 81 | for(int i=7; i>3; i--) { |
aismail1997 | 10:21268d8bf979 | 82 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 83 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 84 | } |
aismail1997 | 10:21268d8bf979 | 85 | } |
aismail1997 | 19:ceac47be2e64 | 86 | |
aismail1997 | 19:ceac47be2e64 | 87 | int button::updateState() |
aismail1997 | 19:ceac47be2e64 | 88 | { |
aismail1997 | 19:ceac47be2e64 | 89 | //myled = 0; |
aismail1997 | 19:ceac47be2e64 | 90 | // state 0 - button is up, pb = 0 |
aismail1997 | 19:ceac47be2e64 | 91 | if (pb == 0 && state == 3) { |
aismail1997 | 19:ceac47be2e64 | 92 | // nothing happens here, servo is still |
aismail1997 | 19:ceac47be2e64 | 93 | state = 0; |
aismail1997 | 19:ceac47be2e64 | 94 | } |
aismail1997 | 19:ceac47be2e64 | 95 | // state 1 - button is moving down, pb = 1 |
aismail1997 | 19:ceac47be2e64 | 96 | if (pb == 1 && state == 0) { |
aismail1997 | 19:ceac47be2e64 | 97 | moveServoIn(); |
aismail1997 | 19:ceac47be2e64 | 98 | state = 1; |
aismail1997 | 19:ceac47be2e64 | 99 | press = 1; |
aismail1997 | 22:6931917c70cd | 100 | |
aismail1997 | 22:6931917c70cd | 101 | // Speaker says stuff |
aismail1997 | 22:6931917c70cd | 102 | /*myTTS.volume(18); //max volume |
aismail1997 | 22:6931917c70cd | 103 | myTTS.speakf("S");//Speak command starts with "S" |
aismail1997 | 22:6931917c70cd | 104 | myTTS.speakf("Hey, you pressed a pin!"); // Send the desired string to convert to speech |
aismail1997 | 22:6931917c70cd | 105 | myTTS.speakf("\r"); //marks end of speak command*/ |
aismail1997 | 19:ceac47be2e64 | 106 | } |
aismail1997 | 19:ceac47be2e64 | 107 | // state 2 - button is down, pb = 0 |
aismail1997 | 19:ceac47be2e64 | 108 | if (pb == 0 && state == 1) { |
aismail1997 | 19:ceac47be2e64 | 109 | // nothing happens here, servo is still |
aismail1997 | 19:ceac47be2e64 | 110 | state = 2; |
aismail1997 | 19:ceac47be2e64 | 111 | } |
aismail1997 | 19:ceac47be2e64 | 112 | // state 3 - button is moving up, pb = 1 |
aismail1997 | 19:ceac47be2e64 | 113 | if (pb == 1 && state == 2) { |
aismail1997 | 19:ceac47be2e64 | 114 | moveServoOut(); |
aismail1997 | 19:ceac47be2e64 | 115 | state = 3; |
aismail1997 | 19:ceac47be2e64 | 116 | press = 0; |
aismail1997 | 19:ceac47be2e64 | 117 | } |
aismail1997 | 19:ceac47be2e64 | 118 | // state 4 - handle debouncing while button is down |
aismail1997 | 19:ceac47be2e64 | 119 | /*if (pb1 = 1 && state == 2) { |
aismail1997 | 19:ceac47be2e64 | 120 | count++; |
aismail1997 | 19:ceac47be2e64 | 121 | }*/ |
aismail1997 | 19:ceac47be2e64 | 122 | return state; |
aismail1997 | 19:ceac47be2e64 | 123 | } |
aismail1997 | 19:ceac47be2e64 | 124 |