User:Kheckco1075/enes100/DoorAccess/Codingprgram

const int knockSensor = 0;        // Piezo sensor on pin 0. const int programSwitch = 2;      // If this is high we program a new code. const int lockMotor = 3;          // Gear motor used to turn the lock. const int redLED = 4;             // Status LED const int greenLED = 5;           // Status LED // Tuning constants. Could be made vars and hoooked to potentiometers for soft configuration, etc. const int threshold =240;          // Minimum signal from the piezo to register as a knock const int rejectValue = 25;       // If an individual knock is off by this percentage of a knock we don't unlock.. const int averageRejectValue = 15; // If the average timing of the knocks is off by this percent we don't unlock. const int knockFadeTime = 150;    // milliseconds we allow a knock to fade before we listen for another one. (Debounce timer.) const int lockTurnTime = 650;     // milliseconds that we run the motor to get it to go a half turn.

const int maximumKnocks = 20;      // Maximum number of knocks to listen for. const int knockComplete = 1200;    // Longest time to wait for a knock before we assume that it's finished.

// Variables. int 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 and a Hair Cut, two bits." int knockReadings[maximumKnocks];  // When someone knocks this array fills with delays between knocks. int knockSensorValue = 0;          // Last reading of the knock sensor. int programButtonPressed = false;  // Flag so we remember the programming button setting at the end of the cycle.

void setup { pinMode(lockMotor, OUTPUT); pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); pinMode(programSwitch, INPUT); Serial.begin(9600);                          // Uncomment the Serial.bla lines for debugging. Serial.println("Program start.");  // but feel free to comment them out after it's working right. digitalWrite(greenLED, HIGH);     // Green LED on, everything is go. }

void loop { // Listen for any knock at all. knockSensorValue = analogRead(knockSensor); if (digitalRead(programSwitch)==HIGH){ // is the program button pressed? programButtonPressed = true;         // Yes, so lets save that state digitalWrite(redLED, HIGH);          // and turn on the red light too so we know we're programming. } else { programButtonPressed = false; digitalWrite(redLED, LOW); } if (knockSensorValue >=threshold){ listenToSecretKnock; } } // Records the timing of knocks. void listenToSecretKnock{ Serial.println("knock starting"); ---> Serial.println(knockSensorValue); //this is the part of the code that will allows up to read how senstive and how responsive the sensor is to our knocking. <

int i = 0; // First lets reset the listening array. for (i=0;i<maximumKnocks;i++){ knockReadings[i]=0; } int currentKnockNumber=0;                     // Incrementer for the array. int startTime=millis;                      // Reference for when this knock started. int now=millis; digitalWrite(greenLED, LOW);                 // we blink the LED for a bit as a visual indicator of the knock. if (programButtonPressed==true){ digitalWrite(redLED, LOW);                        // and the red one too if we're programming a new knock. } delay(knockFadeTime);                                 // wait for this peak to fade before we listen to the next one. digitalWrite(greenLED, HIGH); if (programButtonPressed==true){ digitalWrite(redLED, HIGH); } }