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