Change Existing Code

Hi there,

How can i change the----> high and low in this code to----> long and short?
Thank you

*/

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 = {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 == 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 == 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

Use Millis() to time the knocks, though I think you want to measure the time between knocks, not the knocks themselves. When is the homework due?

Thank you KeithRB
I would like to time the knocks themeselfs. Not the time between the knocks.
How to do this?

I am not sure if I understand the concept of long and short knocks. Hard and soft. yes, but long and short ?

I would like to time the knocks themeselfs.

Perhaps you should draw a picture that shows what you think a knock looks like, and illustrate on the picture what you want to time.

Think about knocking on a door. The time that your knuckle is in contact with the door doesn't really make any difference to someone hearing a knocking sound. It is the pattern of knocks - the time between them - that makes for a secret code.

I don't want to use knocks.
I'm changing the project to long and short light signals.
Does that makes more sense?

Does that makes more sense?

Not really, since you specifically said that you wanted to time the knocks.

Knowing how long a light is on is easy enough. On some pass through loop(), you may see that the light level has risen above some threshold. If that happens, record the time, using millis() or micros(). On some pass through loop(), you may see that the light level has dropped below some threshold. If that happens, record the time, using millis() or micros().

The difference between the times is the time that the light level was above the threshold.