Fun application for FRDM-KL25Z. Uses the on-board accelerometer's built-in tap detection to detect a sequence of knocks on the table the board is sitting on. If the knock sequence matches the secret knock, it makes it happy (blinks green). Otherwise it gets angry (blinks red).

Dependencies:   MMA8451Q_ext mbed

Fun application for FRDM-KL25Z. Uses the on-board accelerometer's built-in tap detection to detect a sequence of knocks on the table the board is sitting on. If the knock sequence matches the secret knock, it makes it happy (blinks green). Otherwise it gets angry (blinks red).

Right now it doesn't support recording new secret knocks. It only knows, "shave and a hair cut, two bits". There is commented-out code in there to do the recording, but it hasn't been enabled or tested yet.

Committer:
maclobdell
Date:
Fri Mar 15 19:06:22 2013 +0000
Revision:
0:ac4e452b3199
public release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:ac4e452b3199 1 #include "mbed.h"
maclobdell 0:ac4e452b3199 2 #include "MMA8451Q.h"
maclobdell 0:ac4e452b3199 3
maclobdell 0:ac4e452b3199 4 #define MMA8451_I2C_ADDRESS (0x1d<<1)
maclobdell 0:ac4e452b3199 5
maclobdell 0:ac4e452b3199 6 /* Secret Knock Demo for FRDM-KL25Z by Mac Lobdell */
maclobdell 0:ac4e452b3199 7 /* Several bits and pieces taken from code by Steve Hoefer (http://grathio.com) under a creative commons share-alike license */
maclobdell 0:ac4e452b3199 8 /* Revison 1.0
maclobdell 0:ac4e452b3199 9 /* Programming not supported yet - only knows "shave and a hair cut, two bits" (or another pre-set sequence if set in secretCode variable initialization) */
maclobdell 0:ac4e452b3199 10
maclobdell 0:ac4e452b3199 11 void INT2ISR(void);
maclobdell 0:ac4e452b3199 12 void INT1ISR(void);
maclobdell 0:ac4e452b3199 13 void d2ISR(void);
maclobdell 0:ac4e452b3199 14 int validateKnock(void);
maclobdell 0:ac4e452b3199 15 void triggerSuccessfulAction(void);
maclobdell 0:ac4e452b3199 16 void triggerFailedAction(void);
maclobdell 0:ac4e452b3199 17 int map(int x, int in_min, int in_max, int out_min, int out_max);
maclobdell 0:ac4e452b3199 18
maclobdell 0:ac4e452b3199 19 Serial pc(USBTX,USBRX);
maclobdell 0:ac4e452b3199 20 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
maclobdell 0:ac4e452b3199 21 DigitalOut rled(LED_RED);
maclobdell 0:ac4e452b3199 22 DigitalOut gled(LED_GREEN);
maclobdell 0:ac4e452b3199 23 DigitalOut bled(LED_BLUE);
maclobdell 0:ac4e452b3199 24
maclobdell 0:ac4e452b3199 25 DigitalOut d2(PTD4);
maclobdell 0:ac4e452b3199 26 Timer timer;
maclobdell 0:ac4e452b3199 27
maclobdell 0:ac4e452b3199 28 const int maximumKnocks = 8; // Maximum number of knocks to listen for.
maclobdell 0:ac4e452b3199 29 int secretCode[maximumKnocks] = {
maclobdell 0:ac4e452b3199 30 50, 25, 25, 50, 100, 50, 0, 0 }; //, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initial setup: "Shave and a Hair Cut, two bits." 100=full note, 50=half note, 25=quarter note, etc.
maclobdell 0:ac4e452b3199 31 int secretKnockMax = 6;
maclobdell 0:ac4e452b3199 32 const int rejectValue = 30; // If an individual knock is off by this percentage of a knock we ignore. (30 is pretty lose. 10 is strict)
maclobdell 0:ac4e452b3199 33 const int averageRejectValue = 20; // If the average timing of the knocks is off by this percent we ignore. (20 is pretty lose, 10 is strict.)
maclobdell 0:ac4e452b3199 34 int knockTime[maximumKnocks]; // When someone knocks this array fills with delays between knocks. (A correct knock looks a lot like the line above).
maclobdell 0:ac4e452b3199 35 int nKnockTime[maximumKnocks];
maclobdell 0:ac4e452b3199 36 int knocking = 0;
maclobdell 0:ac4e452b3199 37 int counter;
maclobdell 0:ac4e452b3199 38 int knockCount = 0; //starts at 0 for first knock
maclobdell 0:ac4e452b3199 39 int i = 0;
maclobdell 0:ac4e452b3199 40 int programButtonPressed = false;
maclobdell 0:ac4e452b3199 41 int startTime;
maclobdell 0:ac4e452b3199 42 int dummyKnock = 1;
maclobdell 0:ac4e452b3199 43 int unlocked = 0;
maclobdell 0:ac4e452b3199 44
maclobdell 0:ac4e452b3199 45 int main(void) {
maclobdell 0:ac4e452b3199 46
maclobdell 0:ac4e452b3199 47 DigitalIn int1(PTA14); //data ready interrupt
maclobdell 0:ac4e452b3199 48 int1.mode(PullUp);
maclobdell 0:ac4e452b3199 49 DigitalIn int2(PTA15); //tap or Portrat/Landscape interrupt
maclobdell 0:ac4e452b3199 50 int2.mode(PullUp);
maclobdell 0:ac4e452b3199 51
maclobdell 0:ac4e452b3199 52 /* initialize all variables */
maclobdell 0:ac4e452b3199 53 knocking = 0;
maclobdell 0:ac4e452b3199 54 counter = 0;
maclobdell 0:ac4e452b3199 55 knockCount = 0;
maclobdell 0:ac4e452b3199 56 startTime = 0;
maclobdell 0:ac4e452b3199 57 dummyKnock = 1;
maclobdell 0:ac4e452b3199 58 unlocked = 0;
maclobdell 0:ac4e452b3199 59 for(i = 0; i<maximumKnocks; i++)
maclobdell 0:ac4e452b3199 60 {
maclobdell 0:ac4e452b3199 61 knockTime[i] = 0;
maclobdell 0:ac4e452b3199 62 nKnockTime[i] = 0;
maclobdell 0:ac4e452b3199 63 }
maclobdell 0:ac4e452b3199 64 rled = 1; //off
maclobdell 0:ac4e452b3199 65 gled = 0; //on
maclobdell 0:ac4e452b3199 66 bled = 1; //off
maclobdell 0:ac4e452b3199 67
maclobdell 0:ac4e452b3199 68 //To Do - need to check if programming
maclobdell 0:ac4e452b3199 69 //capacitive touch slider swipe?
maclobdell 0:ac4e452b3199 70 if (programButtonPressed==true){
maclobdell 0:ac4e452b3199 71 for (i=0;i<3;i++){
maclobdell 0:ac4e452b3199 72 wait_ms(100);
maclobdell 0:ac4e452b3199 73 gled = 0;
maclobdell 0:ac4e452b3199 74 bled = 0;
maclobdell 0:ac4e452b3199 75 wait_ms(100);
maclobdell 0:ac4e452b3199 76 gled = 1;
maclobdell 0:ac4e452b3199 77 bled = 1;
maclobdell 0:ac4e452b3199 78 }
maclobdell 0:ac4e452b3199 79 }
maclobdell 0:ac4e452b3199 80
maclobdell 0:ac4e452b3199 81 //enable interrupts
maclobdell 0:ac4e452b3199 82 InterruptIn int1i(PTA14);
maclobdell 0:ac4e452b3199 83 int1i.fall(&INT1ISR);
maclobdell 0:ac4e452b3199 84 InterruptIn int2i(PTA15);
maclobdell 0:ac4e452b3199 85 int2i.fall(&INT2ISR);
maclobdell 0:ac4e452b3199 86
maclobdell 0:ac4e452b3199 87 while(1)
maclobdell 0:ac4e452b3199 88 {
maclobdell 0:ac4e452b3199 89 //just hang out and wait for interrupts
maclobdell 0:ac4e452b3199 90 if(knocking == 1)
maclobdell 0:ac4e452b3199 91 {
maclobdell 0:ac4e452b3199 92 counter++; //increment here, it will start over to 0 if knock happens in ISR
maclobdell 0:ac4e452b3199 93 bled = 0; //blue on
maclobdell 0:ac4e452b3199 94 gled = 1; //green off
maclobdell 0:ac4e452b3199 95 wait_ms(10); //wait 10 ms
maclobdell 0:ac4e452b3199 96 if ((counter > 1000) || ((knockCount) >= secretKnockMax )) //timeout after 100*10ms = 1 sec, or if max knocks ocurred
maclobdell 0:ac4e452b3199 97 {
maclobdell 0:ac4e452b3199 98 timer.stop();
maclobdell 0:ac4e452b3199 99
maclobdell 0:ac4e452b3199 100 //report what happened
maclobdell 0:ac4e452b3199 101 if(counter > 100)
maclobdell 0:ac4e452b3199 102 {
maclobdell 0:ac4e452b3199 103 pc.printf("timout\n\r");
maclobdell 0:ac4e452b3199 104 }else
maclobdell 0:ac4e452b3199 105 {
maclobdell 0:ac4e452b3199 106 pc.printf("max knocks\n\r");
maclobdell 0:ac4e452b3199 107 }
maclobdell 0:ac4e452b3199 108
maclobdell 0:ac4e452b3199 109 /* check if secret code */
maclobdell 0:ac4e452b3199 110 if (validateKnock() == true){
maclobdell 0:ac4e452b3199 111 triggerSuccessfulAction();
maclobdell 0:ac4e452b3199 112 unlocked = 1;
maclobdell 0:ac4e452b3199 113 }
maclobdell 0:ac4e452b3199 114 else {
maclobdell 0:ac4e452b3199 115 triggerFailedAction();
maclobdell 0:ac4e452b3199 116 unlocked = 0;
maclobdell 0:ac4e452b3199 117 }
maclobdell 0:ac4e452b3199 118
maclobdell 0:ac4e452b3199 119 /* re-initialize all variables */
maclobdell 0:ac4e452b3199 120 counter = 0;
maclobdell 0:ac4e452b3199 121 knocking = 0;
maclobdell 0:ac4e452b3199 122 knockCount = 0;
maclobdell 0:ac4e452b3199 123 unlocked = 0;
maclobdell 0:ac4e452b3199 124 for(i = 0; i<maximumKnocks; i++)
maclobdell 0:ac4e452b3199 125 {
maclobdell 0:ac4e452b3199 126 knockTime[i] = 0;
maclobdell 0:ac4e452b3199 127 }
maclobdell 0:ac4e452b3199 128 rled = 1; //off
maclobdell 0:ac4e452b3199 129 gled = 0; //on
maclobdell 0:ac4e452b3199 130 bled = 1; //off
maclobdell 0:ac4e452b3199 131
maclobdell 0:ac4e452b3199 132
maclobdell 0:ac4e452b3199 133 }
maclobdell 0:ac4e452b3199 134
maclobdell 0:ac4e452b3199 135 }
maclobdell 0:ac4e452b3199 136 }
maclobdell 0:ac4e452b3199 137
maclobdell 0:ac4e452b3199 138
maclobdell 0:ac4e452b3199 139 }
maclobdell 0:ac4e452b3199 140 void d2ISR(void)
maclobdell 0:ac4e452b3199 141 {
maclobdell 0:ac4e452b3199 142 pc.printf("d2 ISR\n\r");
maclobdell 0:ac4e452b3199 143
maclobdell 0:ac4e452b3199 144 }
maclobdell 0:ac4e452b3199 145 void INT1ISR(void)
maclobdell 0:ac4e452b3199 146 {
maclobdell 0:ac4e452b3199 147 /*Acceleration data Ready ISR */
maclobdell 0:ac4e452b3199 148
maclobdell 0:ac4e452b3199 149 // rled = 1.0 - abs(acc.getAccX());
maclobdell 0:ac4e452b3199 150 // gled = 1.0 - abs(acc.getAccY());
maclobdell 0:ac4e452b3199 151 // bled = 1.0 - abs(acc.getAccZ());
maclobdell 0:ac4e452b3199 152 }
maclobdell 0:ac4e452b3199 153
maclobdell 0:ac4e452b3199 154 void INT2ISR(void)
maclobdell 0:ac4e452b3199 155 {
maclobdell 0:ac4e452b3199 156 /* tap occurred - make sure only single taps in one direction are enabled */
maclobdell 0:ac4e452b3199 157
maclobdell 0:ac4e452b3199 158 uint8_t dummy;
maclobdell 0:ac4e452b3199 159
maclobdell 0:ac4e452b3199 160 dummy = acc.tapSource(); //seems like you need to read the source to clear the interrupt
maclobdell 0:ac4e452b3199 161
maclobdell 0:ac4e452b3199 162 // pc.printf("tap isr\n\r");
maclobdell 0:ac4e452b3199 163
maclobdell 0:ac4e452b3199 164 //if(dummyKnock == 1)
maclobdell 0:ac4e452b3199 165 //{
maclobdell 0:ac4e452b3199 166 // //always get a bad knock on reset
maclobdell 0:ac4e452b3199 167 // dummyKnock = 0;
maclobdell 0:ac4e452b3199 168 // pc.printf("dummy knock \n");
maclobdell 0:ac4e452b3199 169 //}else
maclobdell 0:ac4e452b3199 170 {
maclobdell 0:ac4e452b3199 171
maclobdell 0:ac4e452b3199 172
maclobdell 0:ac4e452b3199 173 // pc.printf("Tap \n");
maclobdell 0:ac4e452b3199 174 counter = 0; //reset the timeout counter
maclobdell 0:ac4e452b3199 175
maclobdell 0:ac4e452b3199 176 if(knocking ==0)
maclobdell 0:ac4e452b3199 177 {
maclobdell 0:ac4e452b3199 178 //this is the first knock
maclobdell 0:ac4e452b3199 179 pc.printf("first knock\n\r");
maclobdell 0:ac4e452b3199 180 knockCount = 0;
maclobdell 0:ac4e452b3199 181 knocking = 1;
maclobdell 0:ac4e452b3199 182 pc.printf("start timer\n\r");
maclobdell 0:ac4e452b3199 183 timer.start();
maclobdell 0:ac4e452b3199 184 timer.reset();
maclobdell 0:ac4e452b3199 185 startTime = timer.read_ms();
maclobdell 0:ac4e452b3199 186
maclobdell 0:ac4e452b3199 187 }else if(knocking == 1)
maclobdell 0:ac4e452b3199 188 {
maclobdell 0:ac4e452b3199 189 //this is not the first knock
maclobdell 0:ac4e452b3199 190 pc.printf("not first knock\n\r");
maclobdell 0:ac4e452b3199 191 int now;
maclobdell 0:ac4e452b3199 192 now = timer.read_ms();
maclobdell 0:ac4e452b3199 193 pc.printf("startTime = %d\n\r",startTime);
maclobdell 0:ac4e452b3199 194 pc.printf("now = %d\n\r",now);
maclobdell 0:ac4e452b3199 195 knockTime[knockCount] = (now - startTime);
maclobdell 0:ac4e452b3199 196 pc.printf("knockTime[knockCount] : %d\n\r",knockTime[knockCount]);
maclobdell 0:ac4e452b3199 197
maclobdell 0:ac4e452b3199 198 knockCount++;
maclobdell 0:ac4e452b3199 199 pc.printf("knockCount : %d\n\r",knockCount);
maclobdell 0:ac4e452b3199 200
maclobdell 0:ac4e452b3199 201 startTime = now; //start time for next knock period
maclobdell 0:ac4e452b3199 202
maclobdell 0:ac4e452b3199 203
maclobdell 0:ac4e452b3199 204
maclobdell 0:ac4e452b3199 205 }
maclobdell 0:ac4e452b3199 206
maclobdell 0:ac4e452b3199 207 }
maclobdell 0:ac4e452b3199 208
maclobdell 0:ac4e452b3199 209 }
maclobdell 0:ac4e452b3199 210
maclobdell 0:ac4e452b3199 211
maclobdell 0:ac4e452b3199 212 // We got a good knock, so do something!
maclobdell 0:ac4e452b3199 213 void triggerSuccessfulAction(){
maclobdell 0:ac4e452b3199 214 pc.printf("Success!\n\r");
maclobdell 0:ac4e452b3199 215
maclobdell 0:ac4e452b3199 216 rled = 1; //off
maclobdell 0:ac4e452b3199 217 gled = 0; //on
maclobdell 0:ac4e452b3199 218 bled = 1; //off
maclobdell 0:ac4e452b3199 219
maclobdell 0:ac4e452b3199 220 for (int i=0;i<16;i++){
maclobdell 0:ac4e452b3199 221 gled = 0;
maclobdell 0:ac4e452b3199 222 wait_ms(100);
maclobdell 0:ac4e452b3199 223 gled = 1;
maclobdell 0:ac4e452b3199 224 wait_ms(100);
maclobdell 0:ac4e452b3199 225 }
maclobdell 0:ac4e452b3199 226
maclobdell 0:ac4e452b3199 227 }
maclobdell 0:ac4e452b3199 228
maclobdell 0:ac4e452b3199 229 // We didn't like the knock. Indicate displeasure.
maclobdell 0:ac4e452b3199 230 void triggerFailedAction(){
maclobdell 0:ac4e452b3199 231 pc.printf("Secret knock failed\n");
maclobdell 0:ac4e452b3199 232 rled = 0; //on
maclobdell 0:ac4e452b3199 233 gled = 1; //off
maclobdell 0:ac4e452b3199 234 bled = 1; //off
maclobdell 0:ac4e452b3199 235
maclobdell 0:ac4e452b3199 236 for (int i=0;i<16;i++){
maclobdell 0:ac4e452b3199 237 rled = 0;
maclobdell 0:ac4e452b3199 238 wait_ms(100);
maclobdell 0:ac4e452b3199 239 rled = 1;
maclobdell 0:ac4e452b3199 240 wait_ms(100);
maclobdell 0:ac4e452b3199 241 }
maclobdell 0:ac4e452b3199 242 }
maclobdell 0:ac4e452b3199 243
maclobdell 0:ac4e452b3199 244 // Checks if our knock matches the secret.
maclobdell 0:ac4e452b3199 245 // Returns true if it's a good knock, false if it's not.
maclobdell 0:ac4e452b3199 246 int validateKnock(){
maclobdell 0:ac4e452b3199 247 int i=0;
maclobdell 0:ac4e452b3199 248 // Simplest check first: Did we get the right number of knocks?
maclobdell 0:ac4e452b3199 249 int currentKnockCount = 0;
maclobdell 0:ac4e452b3199 250 int secretKnockCount = 0;
maclobdell 0:ac4e452b3199 251 int maxKnockInterval = 0; // We use this later to normalize the times.
maclobdell 0:ac4e452b3199 252
maclobdell 0:ac4e452b3199 253 int codeFound=true;
maclobdell 0:ac4e452b3199 254 int totaltimeDifferences=0;
maclobdell 0:ac4e452b3199 255 int timeDiff=0;
maclobdell 0:ac4e452b3199 256
maclobdell 0:ac4e452b3199 257 pc.printf("validating knock sequence \n\r");
maclobdell 0:ac4e452b3199 258
maclobdell 0:ac4e452b3199 259
maclobdell 0:ac4e452b3199 260 for (i=0;i<maximumKnocks;i++){
maclobdell 0:ac4e452b3199 261 if (knockTime[i] > 0){
maclobdell 0:ac4e452b3199 262 currentKnockCount++;
maclobdell 0:ac4e452b3199 263 }
maclobdell 0:ac4e452b3199 264 if (secretCode[i] > 0){
maclobdell 0:ac4e452b3199 265 secretKnockCount++;
maclobdell 0:ac4e452b3199 266 }
maclobdell 0:ac4e452b3199 267
maclobdell 0:ac4e452b3199 268 if (knockTime[i] > maxKnockInterval){ // Collect normalization data while we're looping.
maclobdell 0:ac4e452b3199 269 maxKnockInterval = knockTime[i];
maclobdell 0:ac4e452b3199 270 }
maclobdell 0:ac4e452b3199 271 }
maclobdell 0:ac4e452b3199 272 pc.printf("max knock interval: \n\r",maxKnockInterval);
maclobdell 0:ac4e452b3199 273
maclobdell 0:ac4e452b3199 274 // If we're recording a new knock, save the relevant info and get out of here.
maclobdell 0:ac4e452b3199 275 /*if (programButtonPressed==true){
maclobdell 0:ac4e452b3199 276 for (i=0;i<maximumKnocks;i++){ // Normalize the knock timing
maclobdell 0:ac4e452b3199 277 secretCode[i]= map(knockTime[i],0, maxKnockInterval, 0, 100);
maclobdell 0:ac4e452b3199 278 }
maclobdell 0:ac4e452b3199 279 // And flash the lights in the recorded pattern to let us know it's been programmed.
maclobdell 0:ac4e452b3199 280 bled = 0;
maclobdell 0:ac4e452b3199 281 rled = 0;
maclobdell 0:ac4e452b3199 282 wait_ms(750);
maclobdell 0:ac4e452b3199 283
maclobdell 0:ac4e452b3199 284 //Start playing back the knocks
maclobdell 0:ac4e452b3199 285 bled = 1;
maclobdell 0:ac4e452b3199 286 rled = 1;
maclobdell 0:ac4e452b3199 287 wait_ms(40);
maclobdell 0:ac4e452b3199 288 for (i = 0; i < maximumKnocks ; i++){
maclobdell 0:ac4e452b3199 289 bled = 0;
maclobdell 0:ac4e452b3199 290 rled = 0;
maclobdell 0:ac4e452b3199 291
maclobdell 0:ac4e452b3199 292 if (programButtonPressed==true){ // Only turn it on if there's a delay
maclobdell 0:ac4e452b3199 293 if (secretCode[i] > 0){
maclobdell 0:ac4e452b3199 294 wait_ms(map(secretCode[i],0, 100, 0, maxKnockInterval)); // Expand the time back out to what it was. Roughly.
maclobdell 0:ac4e452b3199 295 bled = 1;
maclobdell 0:ac4e452b3199 296 rled = 1;
maclobdell 0:ac4e452b3199 297 }
maclobdell 0:ac4e452b3199 298 }
maclobdell 0:ac4e452b3199 299 wait_ms(40);
maclobdell 0:ac4e452b3199 300 bled = 0;
maclobdell 0:ac4e452b3199 301 rled = 0;
maclobdell 0:ac4e452b3199 302 }
maclobdell 0:ac4e452b3199 303 return false; // We don't do anything when we are recording a new knock.
maclobdell 0:ac4e452b3199 304 }
maclobdell 0:ac4e452b3199 305 */
maclobdell 0:ac4e452b3199 306 pc.printf("currentKnockCount: %d secretKnockCount %d\n\r",currentKnockCount,secretKnockCount);
maclobdell 0:ac4e452b3199 307
maclobdell 0:ac4e452b3199 308 if (currentKnockCount != secretKnockCount){
maclobdell 0:ac4e452b3199 309 pc.printf("wrong number of knocks\n\r");
maclobdell 0:ac4e452b3199 310 return false; // Return false if the number of knocks are wrong.
maclobdell 0:ac4e452b3199 311 }
maclobdell 0:ac4e452b3199 312
maclobdell 0:ac4e452b3199 313 /* Now we compare the relative intervals of our knocks, not the absolute time between them.
maclobdell 0:ac4e452b3199 314 (ie: if you do the same pattern slow or fast it should still work.)
maclobdell 0:ac4e452b3199 315 This makes it less picky, which does make it less secure but also makes it
maclobdell 0:ac4e452b3199 316 less of a pain to use if you're tempo is a little slow or fast.
maclobdell 0:ac4e452b3199 317 */
maclobdell 0:ac4e452b3199 318 for (i=0;i<maximumKnocks;i++){
maclobdell 0:ac4e452b3199 319 nKnockTime[i] = 0; //reinitialize
maclobdell 0:ac4e452b3199 320 }
maclobdell 0:ac4e452b3199 321 for (i=0;i<maximumKnocks;i++){ // Normalize the times
maclobdell 0:ac4e452b3199 322 nKnockTime[i]= map(knockTime[i],0, maxKnockInterval, 0, 100);
maclobdell 0:ac4e452b3199 323 pc.printf("knock time: %d, normalized knock time: %d, secret code time %d \n\r",knockTime[i], nKnockTime[i], secretCode[i]);
maclobdell 0:ac4e452b3199 324 timeDiff = abs(nKnockTime[i]-secretCode[i]);
maclobdell 0:ac4e452b3199 325 if (timeDiff > rejectValue){ // Individual value too far out of whack
maclobdell 0:ac4e452b3199 326 codeFound=false;
maclobdell 0:ac4e452b3199 327 }
maclobdell 0:ac4e452b3199 328 totaltimeDifferences += timeDiff;
maclobdell 0:ac4e452b3199 329 }
maclobdell 0:ac4e452b3199 330 // It can also fail if the whole thing is too inaccurate.
maclobdell 0:ac4e452b3199 331 if (totaltimeDifferences/secretKnockCount>averageRejectValue){
maclobdell 0:ac4e452b3199 332 codeFound = false;
maclobdell 0:ac4e452b3199 333 }
maclobdell 0:ac4e452b3199 334
maclobdell 0:ac4e452b3199 335 if (codeFound==false){
maclobdell 0:ac4e452b3199 336 return false;
maclobdell 0:ac4e452b3199 337 }
maclobdell 0:ac4e452b3199 338 else {
maclobdell 0:ac4e452b3199 339 return true;
maclobdell 0:ac4e452b3199 340 }
maclobdell 0:ac4e452b3199 341
maclobdell 0:ac4e452b3199 342 }
maclobdell 0:ac4e452b3199 343
maclobdell 0:ac4e452b3199 344
maclobdell 0:ac4e452b3199 345 int map(int x, int in_min, int in_max, int out_min, int out_max)
maclobdell 0:ac4e452b3199 346 {
maclobdell 0:ac4e452b3199 347 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
maclobdell 0:ac4e452b3199 348 }