c - Identified amplitude in secret knock project -
i'm doing project http://grathio.com/2013/11/new-old-project-secret-knock-drawer-lock/
project process time interval between knock, , try add amplitude input, if use code link above works fine i want add amplitude (loud , soft knock) input too.
code still have problem, it can't recognise knock , led acting weird. need recognise knock time interval , knock amplitude. comment 'this add' code add myself, code doesn't work. could tell me wrong code?
below code:
#include <eeprom.h> const byte eepromvalid = 123; // if first byte in eeprom data valid. /*pin definitions*/ const int programbutton = 0; // record new knock button. const int ledpin = 1; // built in led const int knocksensor = 1; // (analog 1) using piezo input device. (aka knock sensor) const int audioout = 2; // (digial 2) using peizo output device. (thing goes beep.) const int lockpin = 3; // pin activates solenoid lock. /*tuning constants. changing values below changes behavior of device.*/ int threshold = 3; // minimum signal piezo register knock. higher = less sensitive. typical values 1 - 10 const int rejectvalue = 25; // if individual knock off percentage of knock don't unlock. typical values 10-30 const int averagerejectvalue = 15; // if average timing of knocks off percent don't unlock. typical values 5-20 const int knockfadetime = 150; // milliseconds allow knock fade before listen one. (debounce timer.) const int lockoperatetime = 2500; // milliseconds operate lock solenoid latch before releasing it. const int maximumknocks = 20; // maximum number of knocks listen for. const int maximumamp = 20; // add const int amploud = 10; // add const int ampsoft = 20; // add const int knockcomplete = 1200; // longest time wait knock before assume it's finished. (milliseconds) byte secretcode[maximumknocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // initial setup: "shave , hair cut, 2 bits." byte secretamp[maximumamp] = {10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // add int knockreadings[maximumknocks]; // when knocks array fills delays between knocks. int ampreadings[maximumamp]; // add int knocksensorvalue = 0; // last reading of knock sensor. int amp = 0; // add boolean programmodeactive = false; // true if we're trying program new knock. void setup() { pinmode(ledpin, output); pinmode(lockpin, output); readsecretknock(); // load secret knock (if any) eeprom. doorunlock(500); // unlock door bit when power up. system check , allow way in if key forgotten. delay(500); // delay here because solenoid lock returning place can otherwise trigger , inadvertent knock. } void loop() { // listen knock @ all. knocksensorvalue = analogread(knocksensor); if (digitalread(programbutton) == high){ // program button pressed? delay(100); // cheap debounce. if (digitalread(programbutton) == high){ if (programmodeactive == false){ // if we're not in programming mode, turn on. programmodeactive = true; // remember we're in programming mode. digitalwrite(ledpin, high); // turn on red light user knows we're programming. chirp(500, 1500); // , play tone in case user can't see led. chirp(500, 1000); } else { // if in programing mode, turn off. programmodeactive = false; digitalwrite(ledpin, low); chirp(500, 1000); // turn off programming led , play sad note. chirp(500, 1500); delay(500); } while (digitalread(programbutton) == high){ delay(10); // hang around until button released. } } delay(250); // cheap debounce. longer because releasing button can sensed knock. } if (knocksensorvalue >= threshold){ if (programmodeactive == true){ // blink led when sense knock. digitalwrite(ledpin, low); } else { digitalwrite(ledpin, high); } knockdelay(); if (programmodeactive == true){ // un-blink led. digitalwrite(ledpin, high); } else { digitalwrite(ledpin, low); } listentosecretknock(); // have our first knock. go , see other knocks in store... } } // records timing of knocks. void listentosecretknock(){ int = 0; int j = 0; // first reset listening array. (i=0; < maximumknocks; i++){ knockreadings[i] = 0; } (j=0; j < maximumamp; j++){ // add ampreadings[j] = 0; } int currentknocknumber = 0; // position counter array. int currentampnumber = 0; int starttime = millis(); // reference when knock started. int = millis(); { // listen next knock or wait timeout. knocksensorvalue = analogread(knocksensor); //=========================================================================================================== if(knocksensorvalue >=3 && knocksensorvalue <=75){ // add amp = ampsoft; // add } if(knocksensorvalue >=76){ // add amp = amploud; // add } ampreadings[currentampnumber] = amp; // add currentampnumber++; // add //=========================================================================================================== if (knocksensorvalue >= threshold){ // here's knock. save time between knocks. now=millis(); knockreadings[currentknocknumber] = - starttime; currentknocknumber ++; starttime = now; if (programmodeactive==true){ // blink led when sense knock. digitalwrite(ledpin, low); } else { digitalwrite(ledpin, high); } knockdelay(); if (programmodeactive == true){ // un-blink led. digitalwrite(ledpin, high); } else { digitalwrite(ledpin, low); } } = millis(); // stop listening if there many knocks or there time between knocks. } while ((now-starttime < knockcomplete) && (currentknocknumber < maximumknocks)); //we've got our knock recorded, lets see if it's valid if (programmodeactive == false){ // if we're not recording new knock. if (validateknock() == true){ doorunlock(lockoperatetime); } else { // knock invalid. blink led warning others. (i=0; < 4; i++){ digitalwrite(ledpin, high); delay(50); digitalwrite(ledpin, low); delay(50); } } } else { // if we're in programming mode still validate lock because makes numbers need, don't return. validateknock(); } } // unlocks door. void doorunlock(int delaytime){ digitalwrite(ledpin, high); digitalwrite(lockpin, high); delay(delaytime); digitalwrite(lockpin, low); digitalwrite(ledpin, low); delay(500); // delay here because releasing latch can cause vibration sensed knock. } // checks see if our knock matches secret. // returns true if it's knock, false if it's not. boolean validateknock(){ int = 0; int currentknockcount = 0; int secretknockcount = 0; int currentampcount = 0; // add int secretampcount = 0; // add int maxknockinterval = 0; // use later normalize times. (i=0;i<maximumknocks;i++){ if (knockreadings[i] > 0){ currentknockcount++; } if (secretcode[i] > 0){ secretknockcount++; } if (ampreadings[i] > 0){ // add currentampcount++; // add } if (secretamp[i] > 0){ // add secretampcount++; // add } if (knockreadings[i] > maxknockinterval){ // collect normalization data while we're looping. maxknockinterval = knockreadings[i]; } } // if we're recording new knock, save info , out of here. if (programmodeactive == true){ (i=0; < maximumknocks; i++){ // normalize time between knocks. (the longest time = 100) secretcode[i] = map(knockreadings[i], 0, maxknockinterval, 0, 100); } (int j = 0; j < maximumamp; j++){ secretamp[j] = ampreadings[j]; } savesecretknock(); // save result eeprom programmodeactive = false; playbackknock(maxknockinterval); return false; } if (currentknockcount != secretknockcount && currentampcount != secretampcount){ // easiest check first. if number of knocks wrong, don't unlock. // add return false; } /* compare relative intervals of our knocks, not absolute time between them. (ie: if same pattern slow or fast should still open door.) makes less picky, while making less secure can make less of pain use if you're tempo little slow or fast. */ int totaltimedifferences = 0; int timediff = 0; (i=0; < maximumknocks; i++){ // normalize times knockreadings[i]= map(knockreadings[i], 0, maxknockinterval, 0, 100); timediff = abs(knockreadings[i] - secretcode[i]); if (timediff > rejectvalue){ // individual value far out of whack. no access knock! return false; } totaltimedifferences += timediff; } // can fail if whole thing inaccurate. if (totaltimedifferences / secretknockcount > averagerejectvalue){ return false; } return true; } // reads secret knock eeprom. (if any.) void readsecretknock(){ byte reading; int i; int j; reading = eeprom.read(0); if (reading == eepromvalid){ // read eeprom if signature byte correct. (int i=0; < maximumknocks ;i++){ secretcode[i] = eeprom.read(i+1); } (int j=0; j < maximumamp ;j++){ // add secretamp[j] = eeprom.read(j+1); // add } } } //saves new pattern eeprom void savesecretknock(){ eeprom.write(0, 0); // clear out signature. way know if didn't finish write successfully. (int i=0; < maximumknocks; i++){ eeprom.write(i+1, secretcode[i]); eeprom.write(i+1, secretamp[i]); // add } eeprom.write(0, eepromvalid); // good. write signature we'll know it's good. } // plays pattern of knock in blinks , beeps void playbackknock(int maxknockinterval){ digitalwrite(ledpin, low); delay(1000); digitalwrite(ledpin, high); chirp(200, 1800); (int = 0; < maximumknocks ; i++){ digitalwrite(ledpin, low); // turn on if there's delay if (secretcode[i] > 0){ delay(map(secretcode[i], 0, 100, 0, maxknockinterval)); // expand time out was. roughly. digitalwrite(ledpin, high); chirp(200, 1800); } } digitalwrite(ledpin, low); } // deals knock delay thingy. void knockdelay(){ int itterations = (knockfadetime / 20); // wait peak dissipate before listening next one. (int i=0; < itterations; i++){ delay(10); analogread(knocksensor); // done in attempt defuse analog sensor's capacitor give false readings on high impedance sensors. delay(10); } } // plays non-musical tone on piezo. // playtime = milliseconds play tone // delaytime = time in microseconds between ticks. (smaller=higher pitch tone.) void chirp(int playtime, int delaytime){ long looptime = (playtime * 1000l) / delaytime; pinmode(audioout, output); for(int i=0; < looptime; i++){ digitalwrite(audioout, high); delaymicroseconds(delaytime); digitalwrite(audioout, low); } pinmode(audioout, input); }
i'm guessing wanting record knock pattern , knock volume @ same time. similar pattern softer or louder knocks not activate lock. if case, suggest make array mysecretknock 2 dimensional array. use 2 variables record analog , digital inputs.
capture them 1 after other in same control structure.
i.e. if value of 1 high enough trigger if statement record value of other 1 on next line of code.
keep in mind analog input lower if processed after digital one, may have adjust threshold numbers
Comments
Post a Comment