Alex Louden
/
KeypadServo
main.cpp@0:76b1b51d98f6, 2009-12-06 (annotated)
- Committer:
- alex89
- Date:
- Sun Dec 06 07:29:17 2009 +0000
- Revision:
- 0:76b1b51d98f6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alex89 | 0:76b1b51d98f6 | 1 | #include "mbed.h" |
alex89 | 0:76b1b51d98f6 | 2 | #include "Servo.h" |
alex89 | 0:76b1b51d98f6 | 3 | |
alex89 | 0:76b1b51d98f6 | 4 | DigitalOut myled1(LED1); |
alex89 | 0:76b1b51d98f6 | 5 | DigitalOut myled2(LED2); |
alex89 | 0:76b1b51d98f6 | 6 | DigitalOut myled3(LED3); |
alex89 | 0:76b1b51d98f6 | 7 | DigitalOut myled4(LED4); |
alex89 | 0:76b1b51d98f6 | 8 | |
alex89 | 0:76b1b51d98f6 | 9 | char code[] = "1234"; |
alex89 | 0:76b1b51d98f6 | 10 | |
alex89 | 0:76b1b51d98f6 | 11 | Serial pc(USBTX, USBRX); // debugging |
alex89 | 0:76b1b51d98f6 | 12 | |
alex89 | 0:76b1b51d98f6 | 13 | //--------------- IC2 trial --------------- |
alex89 | 0:76b1b51d98f6 | 14 | |
alex89 | 0:76b1b51d98f6 | 15 | const int address = 0x30; |
alex89 | 0:76b1b51d98f6 | 16 | |
alex89 | 0:76b1b51d98f6 | 17 | // write value into register regno, return success |
alex89 | 0:76b1b51d98f6 | 18 | bool write_reg(int regno, int value) |
alex89 | 0:76b1b51d98f6 | 19 | { |
alex89 | 0:76b1b51d98f6 | 20 | char data[2] = {regno, value}; |
alex89 | 0:76b1b51d98f6 | 21 | return i2c.write(address, data, 2) == 0; |
alex89 | 0:76b1b51d98f6 | 22 | } |
alex89 | 0:76b1b51d98f6 | 23 | |
alex89 | 0:76b1b51d98f6 | 24 | // read value from register regno, return success |
alex89 | 0:76b1b51d98f6 | 25 | bool read_reg(int regno, int *value) |
alex89 | 0:76b1b51d98f6 | 26 | { |
alex89 | 0:76b1b51d98f6 | 27 | char data = regno; |
alex89 | 0:76b1b51d98f6 | 28 | if (i2c.write(address, &data, 1) == 0 && i2c.read(address, &data, 1) == 0) |
alex89 | 0:76b1b51d98f6 | 29 | { |
alex89 | 0:76b1b51d98f6 | 30 | *value = data; |
alex89 | 0:76b1b51d98f6 | 31 | return true; |
alex89 | 0:76b1b51d98f6 | 32 | } |
alex89 | 0:76b1b51d98f6 | 33 | return false; |
alex89 | 0:76b1b51d98f6 | 34 | } |
alex89 | 0:76b1b51d98f6 | 35 | |
alex89 | 0:76b1b51d98f6 | 36 | // read complete value of X axis, return it or -1 on failure |
alex89 | 0:76b1b51d98f6 | 37 | int read_x() |
alex89 | 0:76b1b51d98f6 | 38 | { |
alex89 | 0:76b1b51d98f6 | 39 | int low, high; |
alex89 | 0:76b1b51d98f6 | 40 | if ( read_reg(XOUT_H, &high) && read_reg(XOUT_L, &low) ) |
alex89 | 0:76b1b51d98f6 | 41 | return high<<8 | low; |
alex89 | 0:76b1b51d98f6 | 42 | else |
alex89 | 0:76b1b51d98f6 | 43 | return -1; |
alex89 | 0:76b1b51d98f6 | 44 | } |
alex89 | 0:76b1b51d98f6 | 45 | |
alex89 | 0:76b1b51d98f6 | 46 | void accData(){ |
alex89 | 0:76b1b51d98f6 | 47 | |
alex89 | 0:76b1b51d98f6 | 48 | DigitalOut enable(p26); //enable pin |
alex89 | 0:76b1b51d98f6 | 49 | I2C i2c(p28, p27); // accelerometer |
alex89 | 0:76b1b51d98f6 | 50 | |
alex89 | 0:76b1b51d98f6 | 51 | enable = 1; //set enable pin to 1 |
alex89 | 0:76b1b51d98f6 | 52 | i2c.frequency(400000); //set frequency to 400 KHz |
alex89 | 0:76b1b51d98f6 | 53 | |
alex89 | 0:76b1b51d98f6 | 54 | const int CTRL_REGB = 0x0D; |
alex89 | 0:76b1b51d98f6 | 55 | const int CTRL_REGC = 0x0C; |
alex89 | 0:76b1b51d98f6 | 56 | |
alex89 | 0:76b1b51d98f6 | 57 | write_reg(CTRL_REGB, 0xC2); |
alex89 | 0:76b1b51d98f6 | 58 | write_reg(CTRL_REGC, 0x00); |
alex89 | 0:76b1b51d98f6 | 59 | printf("X axis: %d\n", read_x()); |
alex89 | 0:76b1b51d98f6 | 60 | |
alex89 | 0:76b1b51d98f6 | 61 | |
alex89 | 0:76b1b51d98f6 | 62 | |
alex89 | 0:76b1b51d98f6 | 63 | /* |
alex89 | 0:76b1b51d98f6 | 64 | const int address = 0x97; //slave address |
alex89 | 0:76b1b51d98f6 | 65 | |
alex89 | 0:76b1b51d98f6 | 66 | char data[12]; |
alex89 | 0:76b1b51d98f6 | 67 | |
alex89 | 0:76b1b51d98f6 | 68 | data[0] = 0x42; |
alex89 | 0:76b1b51d98f6 | 69 | i2c.write(CTRL_REGB, data, 1); // register CTRL_REGB to 0x42 (start condition) |
alex89 | 0:76b1b51d98f6 | 70 | |
alex89 | 0:76b1b51d98f6 | 71 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 72 | |
alex89 | 0:76b1b51d98f6 | 73 | data[0] = 0x00; |
alex89 | 0:76b1b51d98f6 | 74 | i2c.write(CTRL_REGC, data, 1); // register CTRL_REGC to 0x00 (start condition) |
alex89 | 0:76b1b51d98f6 | 75 | |
alex89 | 0:76b1b51d98f6 | 76 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 77 | |
alex89 | 0:76b1b51d98f6 | 78 | data[0] = 0x00; |
alex89 | 0:76b1b51d98f6 | 79 | i2c.write(address, data, 1); // tell accelerometer i want to talk to it? |
alex89 | 0:76b1b51d98f6 | 80 | |
alex89 | 0:76b1b51d98f6 | 81 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 82 | |
alex89 | 0:76b1b51d98f6 | 83 | const int XOUT_H = 0x00; //x high register |
alex89 | 0:76b1b51d98f6 | 84 | const int XOUT_L = 0x01; //x low register |
alex89 | 0:76b1b51d98f6 | 85 | |
alex89 | 0:76b1b51d98f6 | 86 | for(int i = 0; i < 10; i++){ |
alex89 | 0:76b1b51d98f6 | 87 | pc.printf("x high: '%s'\n", i2c.read(XOUT_H, data, 12)); |
alex89 | 0:76b1b51d98f6 | 88 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 89 | pc.printf("x low: '%s'\n", i2c.read(XOUT_L, data, 12)); |
alex89 | 0:76b1b51d98f6 | 90 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 91 | } |
alex89 | 0:76b1b51d98f6 | 92 | |
alex89 | 0:76b1b51d98f6 | 93 | enable = 0; |
alex89 | 0:76b1b51d98f6 | 94 | |
alex89 | 0:76b1b51d98f6 | 95 | |
alex89 | 0:76b1b51d98f6 | 96 | pc.printf("started\n"); |
alex89 | 0:76b1b51d98f6 | 97 | |
alex89 | 0:76b1b51d98f6 | 98 | |
alex89 | 0:76b1b51d98f6 | 99 | for (int i = 0; i < 10; i++){ //get 10 results |
alex89 | 0:76b1b51d98f6 | 100 | |
alex89 | 0:76b1b51d98f6 | 101 | const int CTRL_REGB = 0x0D; |
alex89 | 0:76b1b51d98f6 | 102 | const int CTRL_REGC = 0x0C; |
alex89 | 0:76b1b51d98f6 | 103 | |
alex89 | 0:76b1b51d98f6 | 104 | cmd[0] = 0x2; |
alex89 | 0:76b1b51d98f6 | 105 | i2c.write(addr, data, 2); |
alex89 | 0:76b1b51d98f6 | 106 | |
alex89 | 0:76b1b51d98f6 | 107 | i2c.read(addr, data, 8); // read the echo result |
alex89 | 0:76b1b51d98f6 | 108 | |
alex89 | 0:76b1b51d98f6 | 109 | // print the ranging data to the screen |
alex89 | 0:76b1b51d98f6 | 110 | //float echo = 0.01 * ((cmd[0] << 8) + cmd[1]); |
alex89 | 0:76b1b51d98f6 | 111 | |
alex89 | 0:76b1b51d98f6 | 112 | pc.printf ("Data: %s\n", cmd); |
alex89 | 0:76b1b51d98f6 | 113 | |
alex89 | 0:76b1b51d98f6 | 114 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 115 | } |
alex89 | 0:76b1b51d98f6 | 116 | |
alex89 | 0:76b1b51d98f6 | 117 | i2c.write( |
alex89 | 0:76b1b51d98f6 | 118 | |
alex89 | 0:76b1b51d98f6 | 119 | wait(0.07); |
alex89 | 0:76b1b51d98f6 | 120 | |
alex89 | 0:76b1b51d98f6 | 121 | const int XOUT_H = 0x00; |
alex89 | 0:76b1b51d98f6 | 122 | const int XOUT_L = 0x01; |
alex89 | 0:76b1b51d98f6 | 123 | |
alex89 | 0:76b1b51d98f6 | 124 | i2c.read(XOUT_H, data, 8); // read the result |
alex89 | 0:76b1b51d98f6 | 125 | pc.printf ("XOUT_H: '%s'\n", data); |
alex89 | 0:76b1b51d98f6 | 126 | |
alex89 | 0:76b1b51d98f6 | 127 | wait(0.07); |
alex89 | 0:76b1b51d98f6 | 128 | |
alex89 | 0:76b1b51d98f6 | 129 | i2c.read(XOUT_L, data, 8); // read the result |
alex89 | 0:76b1b51d98f6 | 130 | pc.printf ("XOUT_L: '%s'\n", data); |
alex89 | 0:76b1b51d98f6 | 131 | */ |
alex89 | 0:76b1b51d98f6 | 132 | |
alex89 | 0:76b1b51d98f6 | 133 | } |
alex89 | 0:76b1b51d98f6 | 134 | |
alex89 | 0:76b1b51d98f6 | 135 | |
alex89 | 0:76b1b51d98f6 | 136 | //--------------- Servo position reset --------------- |
alex89 | 0:76b1b51d98f6 | 137 | |
alex89 | 0:76b1b51d98f6 | 138 | void resetServo(){ |
alex89 | 0:76b1b51d98f6 | 139 | |
alex89 | 0:76b1b51d98f6 | 140 | Servo myServo (p21); // steering servo |
alex89 | 0:76b1b51d98f6 | 141 | |
alex89 | 0:76b1b51d98f6 | 142 | int i; |
alex89 | 0:76b1b51d98f6 | 143 | bool j = 0; |
alex89 | 0:76b1b51d98f6 | 144 | |
alex89 | 0:76b1b51d98f6 | 145 | //clockwise 180 |
alex89 | 0:76b1b51d98f6 | 146 | for (i=0 ; i<200 ; i++){ |
alex89 | 0:76b1b51d98f6 | 147 | myServo = i/100.0; |
alex89 | 0:76b1b51d98f6 | 148 | wait (0.01); |
alex89 | 0:76b1b51d98f6 | 149 | if(i%10==0){ |
alex89 | 0:76b1b51d98f6 | 150 | j = !j; |
alex89 | 0:76b1b51d98f6 | 151 | myled1 = j; |
alex89 | 0:76b1b51d98f6 | 152 | } |
alex89 | 0:76b1b51d98f6 | 153 | } |
alex89 | 0:76b1b51d98f6 | 154 | |
alex89 | 0:76b1b51d98f6 | 155 | pc.printf("Servo at full rotation\n"); |
alex89 | 0:76b1b51d98f6 | 156 | |
alex89 | 0:76b1b51d98f6 | 157 | //wait a second |
alex89 | 0:76b1b51d98f6 | 158 | for (i = 0; i < 10; i++){ |
alex89 | 0:76b1b51d98f6 | 159 | wait (0.1); |
alex89 | 0:76b1b51d98f6 | 160 | j = !j; |
alex89 | 0:76b1b51d98f6 | 161 | myled2 = j; |
alex89 | 0:76b1b51d98f6 | 162 | } |
alex89 | 0:76b1b51d98f6 | 163 | |
alex89 | 0:76b1b51d98f6 | 164 | pc.printf("End pause\n"); |
alex89 | 0:76b1b51d98f6 | 165 | |
alex89 | 0:76b1b51d98f6 | 166 | //back to start |
alex89 | 0:76b1b51d98f6 | 167 | for (i=100 ; i>0 ; i--){ |
alex89 | 0:76b1b51d98f6 | 168 | myServo = i/100.0; |
alex89 | 0:76b1b51d98f6 | 169 | wait (0.01); |
alex89 | 0:76b1b51d98f6 | 170 | if(i%10==0){ |
alex89 | 0:76b1b51d98f6 | 171 | j = !j; |
alex89 | 0:76b1b51d98f6 | 172 | myled3 = j; |
alex89 | 0:76b1b51d98f6 | 173 | } |
alex89 | 0:76b1b51d98f6 | 174 | } |
alex89 | 0:76b1b51d98f6 | 175 | pc.printf("Servo at default position\n"); |
alex89 | 0:76b1b51d98f6 | 176 | } |
alex89 | 0:76b1b51d98f6 | 177 | |
alex89 | 0:76b1b51d98f6 | 178 | //--------------- Turning all LEDs off or on --------------- |
alex89 | 0:76b1b51d98f6 | 179 | |
alex89 | 0:76b1b51d98f6 | 180 | void all(bool status){ |
alex89 | 0:76b1b51d98f6 | 181 | |
alex89 | 0:76b1b51d98f6 | 182 | myled1 = status; |
alex89 | 0:76b1b51d98f6 | 183 | myled2 = status; |
alex89 | 0:76b1b51d98f6 | 184 | myled3 = status; |
alex89 | 0:76b1b51d98f6 | 185 | myled4 = status; |
alex89 | 0:76b1b51d98f6 | 186 | |
alex89 | 0:76b1b51d98f6 | 187 | } |
alex89 | 0:76b1b51d98f6 | 188 | |
alex89 | 0:76b1b51d98f6 | 189 | |
alex89 | 0:76b1b51d98f6 | 190 | //--------------- Digital pin checking --------------- |
alex89 | 0:76b1b51d98f6 | 191 | |
alex89 | 0:76b1b51d98f6 | 192 | void checkPin5(){ |
alex89 | 0:76b1b51d98f6 | 193 | |
alex89 | 0:76b1b51d98f6 | 194 | DigitalIn input5(p5); |
alex89 | 0:76b1b51d98f6 | 195 | |
alex89 | 0:76b1b51d98f6 | 196 | //check for pin 5 |
alex89 | 0:76b1b51d98f6 | 197 | while(1) { |
alex89 | 0:76b1b51d98f6 | 198 | if(input5){ |
alex89 | 0:76b1b51d98f6 | 199 | myled4 = !myled4; |
alex89 | 0:76b1b51d98f6 | 200 | pc.printf("pin 5 in!\n"); |
alex89 | 0:76b1b51d98f6 | 201 | } |
alex89 | 0:76b1b51d98f6 | 202 | wait(0.1); |
alex89 | 0:76b1b51d98f6 | 203 | } |
alex89 | 0:76b1b51d98f6 | 204 | } |
alex89 | 0:76b1b51d98f6 | 205 | |
alex89 | 0:76b1b51d98f6 | 206 | //--------------- Get key number --------------- |
alex89 | 0:76b1b51d98f6 | 207 | |
alex89 | 0:76b1b51d98f6 | 208 | int keyNum(){ |
alex89 | 0:76b1b51d98f6 | 209 | |
alex89 | 0:76b1b51d98f6 | 210 | DigitalIn col1(p18); |
alex89 | 0:76b1b51d98f6 | 211 | DigitalIn col2(p20); |
alex89 | 0:76b1b51d98f6 | 212 | DigitalIn col3(p16); |
alex89 | 0:76b1b51d98f6 | 213 | |
alex89 | 0:76b1b51d98f6 | 214 | DigitalOut row1(p19); |
alex89 | 0:76b1b51d98f6 | 215 | DigitalOut row2(p14); |
alex89 | 0:76b1b51d98f6 | 216 | DigitalOut row3(p15); |
alex89 | 0:76b1b51d98f6 | 217 | DigitalOut row4(p17); |
alex89 | 0:76b1b51d98f6 | 218 | |
alex89 | 0:76b1b51d98f6 | 219 | row1 = 1; |
alex89 | 0:76b1b51d98f6 | 220 | row2 = 0; |
alex89 | 0:76b1b51d98f6 | 221 | row3 = 0; |
alex89 | 0:76b1b51d98f6 | 222 | row4 = 0; |
alex89 | 0:76b1b51d98f6 | 223 | |
alex89 | 0:76b1b51d98f6 | 224 | if (col1) |
alex89 | 0:76b1b51d98f6 | 225 | return 1; |
alex89 | 0:76b1b51d98f6 | 226 | else if (col2) |
alex89 | 0:76b1b51d98f6 | 227 | return 2; |
alex89 | 0:76b1b51d98f6 | 228 | else if (col3) |
alex89 | 0:76b1b51d98f6 | 229 | return 3; |
alex89 | 0:76b1b51d98f6 | 230 | |
alex89 | 0:76b1b51d98f6 | 231 | row1 = 0; |
alex89 | 0:76b1b51d98f6 | 232 | row2 = 1; |
alex89 | 0:76b1b51d98f6 | 233 | row3 = 0; |
alex89 | 0:76b1b51d98f6 | 234 | row4 = 0; |
alex89 | 0:76b1b51d98f6 | 235 | |
alex89 | 0:76b1b51d98f6 | 236 | if (col1) |
alex89 | 0:76b1b51d98f6 | 237 | return 4; |
alex89 | 0:76b1b51d98f6 | 238 | else if (col2) |
alex89 | 0:76b1b51d98f6 | 239 | return 5; |
alex89 | 0:76b1b51d98f6 | 240 | else if (col3) |
alex89 | 0:76b1b51d98f6 | 241 | return 6; |
alex89 | 0:76b1b51d98f6 | 242 | |
alex89 | 0:76b1b51d98f6 | 243 | row1 = 0; |
alex89 | 0:76b1b51d98f6 | 244 | row2 = 0; |
alex89 | 0:76b1b51d98f6 | 245 | row3 = 1; |
alex89 | 0:76b1b51d98f6 | 246 | row4 = 0; |
alex89 | 0:76b1b51d98f6 | 247 | |
alex89 | 0:76b1b51d98f6 | 248 | if (col1) |
alex89 | 0:76b1b51d98f6 | 249 | return 7; |
alex89 | 0:76b1b51d98f6 | 250 | else if (col2) |
alex89 | 0:76b1b51d98f6 | 251 | return 8; |
alex89 | 0:76b1b51d98f6 | 252 | else if (col3) |
alex89 | 0:76b1b51d98f6 | 253 | return 9; |
alex89 | 0:76b1b51d98f6 | 254 | |
alex89 | 0:76b1b51d98f6 | 255 | row1 = 0; |
alex89 | 0:76b1b51d98f6 | 256 | row2 = 0; |
alex89 | 0:76b1b51d98f6 | 257 | row3 = 0; |
alex89 | 0:76b1b51d98f6 | 258 | row4 = 1; |
alex89 | 0:76b1b51d98f6 | 259 | |
alex89 | 0:76b1b51d98f6 | 260 | if (col1) |
alex89 | 0:76b1b51d98f6 | 261 | return 10; |
alex89 | 0:76b1b51d98f6 | 262 | else if (col2) |
alex89 | 0:76b1b51d98f6 | 263 | return 0; |
alex89 | 0:76b1b51d98f6 | 264 | else if (col3) |
alex89 | 0:76b1b51d98f6 | 265 | return 11; |
alex89 | 0:76b1b51d98f6 | 266 | |
alex89 | 0:76b1b51d98f6 | 267 | row1 = 0; |
alex89 | 0:76b1b51d98f6 | 268 | row2 = 0; |
alex89 | 0:76b1b51d98f6 | 269 | row3 = 0; |
alex89 | 0:76b1b51d98f6 | 270 | row4 = 0; |
alex89 | 0:76b1b51d98f6 | 271 | |
alex89 | 0:76b1b51d98f6 | 272 | return -1; |
alex89 | 0:76b1b51d98f6 | 273 | } |
alex89 | 0:76b1b51d98f6 | 274 | |
alex89 | 0:76b1b51d98f6 | 275 | //--------------- Check against code --------------- |
alex89 | 0:76b1b51d98f6 | 276 | |
alex89 | 0:76b1b51d98f6 | 277 | bool getCode(){ |
alex89 | 0:76b1b51d98f6 | 278 | |
alex89 | 0:76b1b51d98f6 | 279 | char codeTry[] = ""; |
alex89 | 0:76b1b51d98f6 | 280 | int previous = -1; |
alex89 | 0:76b1b51d98f6 | 281 | int a = -1; |
alex89 | 0:76b1b51d98f6 | 282 | |
alex89 | 0:76b1b51d98f6 | 283 | while(strlen(codeTry) < strlen(code)){ |
alex89 | 0:76b1b51d98f6 | 284 | |
alex89 | 0:76b1b51d98f6 | 285 | int cycles = 0; |
alex89 | 0:76b1b51d98f6 | 286 | |
alex89 | 0:76b1b51d98f6 | 287 | while ((a == previous && cycles < 40) || a == -1){ //keep looping until different to last, or held down |
alex89 | 0:76b1b51d98f6 | 288 | a = keyNum(); |
alex89 | 0:76b1b51d98f6 | 289 | cycles++; |
alex89 | 0:76b1b51d98f6 | 290 | wait(0.01); |
alex89 | 0:76b1b51d98f6 | 291 | } |
alex89 | 0:76b1b51d98f6 | 292 | |
alex89 | 0:76b1b51d98f6 | 293 | sprintf(codeTry, "%s%d", codeTry, a); |
alex89 | 0:76b1b51d98f6 | 294 | |
alex89 | 0:76b1b51d98f6 | 295 | switch(strlen(codeTry)){ |
alex89 | 0:76b1b51d98f6 | 296 | case 1: myled1 = 1; break; |
alex89 | 0:76b1b51d98f6 | 297 | case 2: myled2 = 1; break; |
alex89 | 0:76b1b51d98f6 | 298 | case 3: myled3 = 1; break; |
alex89 | 0:76b1b51d98f6 | 299 | case 4: myled4 = 1; |
alex89 | 0:76b1b51d98f6 | 300 | } |
alex89 | 0:76b1b51d98f6 | 301 | |
alex89 | 0:76b1b51d98f6 | 302 | previous = a; |
alex89 | 0:76b1b51d98f6 | 303 | wait (0.1); |
alex89 | 0:76b1b51d98f6 | 304 | } |
alex89 | 0:76b1b51d98f6 | 305 | |
alex89 | 0:76b1b51d98f6 | 306 | all(0); |
alex89 | 0:76b1b51d98f6 | 307 | |
alex89 | 0:76b1b51d98f6 | 308 | pc.printf("code entered: '%s'\n", codeTry); |
alex89 | 0:76b1b51d98f6 | 309 | |
alex89 | 0:76b1b51d98f6 | 310 | if(strcmp(code, codeTry) == 0){ |
alex89 | 0:76b1b51d98f6 | 311 | return true; |
alex89 | 0:76b1b51d98f6 | 312 | } |
alex89 | 0:76b1b51d98f6 | 313 | |
alex89 | 0:76b1b51d98f6 | 314 | return false; |
alex89 | 0:76b1b51d98f6 | 315 | } |
alex89 | 0:76b1b51d98f6 | 316 | |
alex89 | 0:76b1b51d98f6 | 317 | //--------------- Check what key is pressed --------------- |
alex89 | 0:76b1b51d98f6 | 318 | |
alex89 | 0:76b1b51d98f6 | 319 | void keyCheck(){ |
alex89 | 0:76b1b51d98f6 | 320 | |
alex89 | 0:76b1b51d98f6 | 321 | bool j = 0; |
alex89 | 0:76b1b51d98f6 | 322 | |
alex89 | 0:76b1b51d98f6 | 323 | while (1){ |
alex89 | 0:76b1b51d98f6 | 324 | int a = keyNum(); |
alex89 | 0:76b1b51d98f6 | 325 | if(a == 11){ //on hash close the app |
alex89 | 0:76b1b51d98f6 | 326 | myled1 = 0; |
alex89 | 0:76b1b51d98f6 | 327 | break; |
alex89 | 0:76b1b51d98f6 | 328 | } |
alex89 | 0:76b1b51d98f6 | 329 | if(a!=-1){ |
alex89 | 0:76b1b51d98f6 | 330 | pc.printf("%d\n", a); //send key to pc |
alex89 | 0:76b1b51d98f6 | 331 | } |
alex89 | 0:76b1b51d98f6 | 332 | |
alex89 | 0:76b1b51d98f6 | 333 | j = !j; |
alex89 | 0:76b1b51d98f6 | 334 | myled1 = j; //flash LED1 |
alex89 | 0:76b1b51d98f6 | 335 | |
alex89 | 0:76b1b51d98f6 | 336 | wait (0.1); |
alex89 | 0:76b1b51d98f6 | 337 | } |
alex89 | 0:76b1b51d98f6 | 338 | } |
alex89 | 0:76b1b51d98f6 | 339 | |
alex89 | 0:76b1b51d98f6 | 340 | //---------------- Door keypad --------------- |
alex89 | 0:76b1b51d98f6 | 341 | |
alex89 | 0:76b1b51d98f6 | 342 | void doorlock(){ |
alex89 | 0:76b1b51d98f6 | 343 | |
alex89 | 0:76b1b51d98f6 | 344 | bool password = getCode(); |
alex89 | 0:76b1b51d98f6 | 345 | |
alex89 | 0:76b1b51d98f6 | 346 | if (password){ |
alex89 | 0:76b1b51d98f6 | 347 | pc.printf("Password correct\n"); |
alex89 | 0:76b1b51d98f6 | 348 | resetServo(); |
alex89 | 0:76b1b51d98f6 | 349 | } else { |
alex89 | 0:76b1b51d98f6 | 350 | all(1); |
alex89 | 0:76b1b51d98f6 | 351 | wait(0.5); |
alex89 | 0:76b1b51d98f6 | 352 | all(0); |
alex89 | 0:76b1b51d98f6 | 353 | pc.printf("Password incorrect\n"); |
alex89 | 0:76b1b51d98f6 | 354 | } |
alex89 | 0:76b1b51d98f6 | 355 | } |
alex89 | 0:76b1b51d98f6 | 356 | |
alex89 | 0:76b1b51d98f6 | 357 | //-------------------- Main ------------------- |
alex89 | 0:76b1b51d98f6 | 358 | |
alex89 | 0:76b1b51d98f6 | 359 | int main() { |
alex89 | 0:76b1b51d98f6 | 360 | |
alex89 | 0:76b1b51d98f6 | 361 | pc.printf("Program started\n"); |
alex89 | 0:76b1b51d98f6 | 362 | |
alex89 | 0:76b1b51d98f6 | 363 | accData(); |
alex89 | 0:76b1b51d98f6 | 364 | |
alex89 | 0:76b1b51d98f6 | 365 | //doorlock(); |
alex89 | 0:76b1b51d98f6 | 366 | //resetServo(); |
alex89 | 0:76b1b51d98f6 | 367 | |
alex89 | 0:76b1b51d98f6 | 368 | pc.printf("Program complete\n\n"); |
alex89 | 0:76b1b51d98f6 | 369 | } |