Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
nnguyen45
Date:
Wed Nov 29 00:40:16 2017 +0000
Revision:
35:3090ec93b4aa
Parent:
34:e87bb6e83072
read pin signal working

Who changed what in which revision?

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