Changing a secret knock code

Hello there,

I'm working with a secret knocking detector code.
It can detect if a knock is Hard or Soft.
I would like to change this code into a code which can detect if
a light signal is Long or Short.
How can I change this?

Thank you in advance

Here is the code I'm working with

This code is in the public domain
 
*/
 
const int outputPin = 6;    // led indicator connected to digital pin
const int knockSensor = A0; // the piezo is connected to an analog pin
const int thresholdHIGH = 150;  // threshold value to decide when the detected knock is hard (HIGH)
const int thresholdLOW = 120;  // threshold value to decide when the detected knock is gentle (LOW)
 
 
const int secretKnockLength = 4; //How many knocks are in your secret knock
 
/* This is the secret knock sequence
 * 0 represents a LOW or quiet knock
 * 1 represents a HIGH or loud knock
 * The sequence can be as long as you like, but longer codes increase the difficulty of matching */
const int secretKnock[secretKnockLength] = {0, 0, 1, 0};
 
int secretCounter = 0; //this tracks the correct knocks and allows you to move through the sequence
int sensorReading = 0; // variable to store the value read from the sensor pin
 
void setup() {
 
  //Set the output pin as an OUTPUT
  pinMode(outputPin, OUTPUT);
 
  //Begin Serial Communication.
  Serial.begin(9600);
 
}
 
void loop() {
 
  // read the piezo sensor and store the value in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
 
  // First determine is knock if Hard (HIGH) or Gentle (LOW)
 
  //Hard knock (HIGH) is detected
  if (sensorReading >= thresholdHIGH) {
 
    //Check to see if a Hard Knock matches the Secret Knock in the correct sequence.
    if (secretKnock[secretCounter] == 1) {
 
      //The Knock was correct, iterate the counter.
      secretCounter++;
      Serial.println("Correct");
 
    } else {
 
      //The Knock was incorrect, reset the counter
      secretCounter = 0;
      Serial.println("Fail - You are a spy!");
 
    }//close if
 
    //Allow some time to pass before sampling again to ensure a clear signal.
    delay(100);
 
    //Gentle knock (LOW) is detected
  } else if (sensorReading >= thresholdLOW) {
 
    //Check to see if a Gentle Knock matches the Secret Knock in the correct sequence.
    if (secretKnock[secretCounter] == 0) {
 
      //The Knock was correct, iterate the counter.
      secretCounter++;
      Serial.println("Correct");
 
    } else {
 
      //The Knock was incorrect, reset the counter.
      secretCounter = 0;
      Serial.println("Fail - You are a spy!");
 
    }//close if
 
    //Allow some time to pass before sampling again to ensure a clear signal.
    delay(100);
 
  }//close if else
 
  //Check for successful entry of the code, by seeing if the entire array has been walked through.
  if (secretCounter == (secretKnockLength) ) {
 
    Serial.println("Welcome in fellow Illuminante!");
 
    //if the sececret knock is correct, illuminate the LED for a couple seconds
    digitalWrite(outputPin, HIGH);
    delay(2000);
    digitalWrite(outputPin, LOW);
 
    //Reset the secret counter to 0.
    secretCounter = 0;
 
  }//close success check
 
}//close loop

What has light to do with knocking?

Or do you mean a soft knock?

But a knock itself has no duration... So what do you mean with long and short?

I would like to leave the knocking behind and change the code into a light length detector
instead of a knock volume detector

Then drop all this code and make new code. Except from maybe how they check a correct code this code is useless. You can't check the length of something with a analog port. So you need to start using "time". And checking time (in time) is quite different then a volume measure. Hint, look at millis() :wink: