piezo on and off

hi all

I found this example

http://arduino.cc/en/Tutorial/Knock

and saw this code

const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

void setup() {
 pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
 Serial.begin(9600);       // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);    
  
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;   
    // update the LED pin itself:        
    digitalWrite(ledPin, ledState);
    // send the string "Knock!" back to the computer, followed by newline
    Serial.println("Knock!");         
  }
  delay(100);  // delay to avoid overloading the serial port buffer
}

so i tried to change it a bit to make it tap once on and tap once off

but it works sometimes(tap once the led on and tap once the led off) but it doesn't work sometimes (tap once the led on but i had to tap a few times to turn it off)

const int ledPin = 9;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 3;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0,sensorReading1 = 0;      // variable to store the value read from the sensor pin

void setup() {
 pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT

}

void loop() {

  sensorReading = analogRead(knockSensor);    
  
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {

       
    digitalWrite(ledPin, HIGH);
     
  }

 if (sensorReading1 >= threshold) {
   
       
    digitalWrite(ledPin,LOW);}

}

note: if i set the threshold above 3 it doesn't work

the led goes on then off or I had to tap very hard to turn on the led)

Please show me how to make this simple code work

Thanks

But you aren't reading anything into the variable "sensorReading1".....

  sensorReading = analogRead(knockSensor);


sensorReading1 = analogRead(knockSensor);

I copied it wrongly

I do have the second line in the code

thanks for replying