noob/need help with vibe sensor-->led

hi all.
im very new to this. Pretty good DIY'er. The hardware part of this kind of thing shouldn't be a problem for me but the code stuff will take some practice. I think i have about 4 books to read before i get a decent handle on it.

The project im working on is the whole reason i got the arduino. The concept is simple and the fact that i could find code already mostly done for me made it that much better.

Basically, Vibration sensor momentarily lights led. (Bullet hits steal--> led lights on impact--> led goes out.)

I used the example code for the Knock sensor using the piezo speaker however im using a MEAS vibration sensor and multiple led's instead.
My problem is def. in the code. It works as it should for the most part but for what ever reason the led stays lit from time to time. I need it to always go out after the sensor quits reading vibration.
this is the code im using

// these constants won't change:
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 = 10 ; // 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 >= 10) {
// 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("HIT!");
}
delay(10); // delay to avoid overloading the serial port buffer
}

I have tried changing some of the variables but didnt really notice a difference.

Dont really care about the serial print stuff. wont be using that right now.
any help would be appreciated
thank you

I need it to always go out after the sensor quits reading vibration.

That the LEDs don't go out is caused by one of two issues - poor coding or the sensor is still seeing vibration.

I'm thinking poor coding. What you seem to want to do is turn the LED on when there is vibration (i.e. sensorReading is greater than 10) and off when there is not. Every time you detect that there is motion, you toggle (not set) the LED state.

Try:

  if (sensorReading >= 10)
  {
    digitalWrite(ledPin, HIGH);
    Serial.println("HIT!");         
  }
  else
    digitalWrite(ledPin, LOW);

I've simplified your code to make it a little more obvious (to me) what is happening:

toyfreak:

void loop() {

sensorReading = analogRead(knockSensor);   
  if (sensorReading >= 10) {
    ledState = !ledState; 
    digitalWrite(ledPin, ledState);
    Serial.println("HIT!");       
  }
  delay(10);  // delay to avoid overloading the serial port buffer
}

The code toggles the LED state if sensorReading is >= 10. Then it loops through again, reads the sensor and toggles the LED if it is still 10 or greater. It will repeat this forever. Therefore, when the sensor is vibrating, the LED is toggling. When the sensor is not vibrating the LED will stay in it's last state. I would guess that it works the way you intend about 50% of the time. PaulS's code will fix this problem.

Do you want it to send a stream of "Hit!" messages as long as the sensor is vibrating? or do you want only one? There are a lot of ways to implement a single message, but all have some way of keeping track of the 'old' state and the 'new' state. If they are not the same, send the message. If the state is the same as last time, then don't send the message. Here is an example.

sensorReading = analogRead(knockSensor);
if (sensorReading >= 10)
{
    if ledState == LOW 
    {
        Serial.println("HIT!");
    }
    ledState = HIGH;
    digitalWrite(ledPin, ledState);
}
else 
{
    ledState =  LOW;
    digitalWrite(ledPin, ledState);
  }