I have an Arduino project I'm working on for a steel target hit indicator. I know I can just buy a MagnetoSpeed T1000, but then I wouldn't have to stress and think, and I kinda hate myself, so here we are.
I bought a SparkFun MicroPro, piezo vibration sensor, LED bulb, some 100ohm resistors for the LED and some 1Mohm resistors for the Piezo to Arduino connection. I also bought a 9v battery holder to power the whole thing. Ill be mounting it into a mini pelican case that I can Velcro onto the back of my steel target to keep it packaged well.
The issue I'm having (besides trashing my piezo sensor by melting the shit out of it) is that the LED wants to stay on all the time, with or without the Piezo attached. I don't know what I'm missing on this. Any help would be greatly appreciated! I pasted the code I'm using below.
100ohm attached to the negative is of the LED which is then attached to GRND and Pin 5 for positive, 1Mohm resistor soldered to pins A0 and GRND, 9v connection attached to RAW and GRND pins.
Thanks!
This is the code I'm using:
const int ledPin = 5; // led connected to digital pin 5
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 20; // threshold value to decide when the detected sound is a knock or not
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
unsigned long lastPeakTime = 0; //used for calculating peak
int currentPeak = 0;
int blinkCounter = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port for debugging
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
if (sensorReading > currentPeak) {
currentPeak = sensorReading;
}
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
Serial.print(sensorReading);
Serial.println(" Knock! ");
while(blinkCounter <= 3)
{
Serial.println(blinkCounter);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
blinkCounter++;
}
blinkCounter = 0;
delay(50);
}
//DEBUG CODE THAT GETS WRITTEN TO SERIAL, NOT NECESSARY TO LEAVE IN
if(millis() - lastPeakTime > 2000) {
Serial.print("Peak value: ");
Serial.println(currentPeak);
lastPeakTime = millis();
currentPeak = 0;
}
}
I bought a SparkFun MicroPro, piezo vibration sensor, LED bulb, some 100ohm resistors for the LED and some 1Mohm resistors for the Piezo to Arduino connection. I also bought a 9v battery holder to power the whole thing. Ill be mounting it into a mini pelican case that I can Velcro onto the back of my steel target to keep it packaged well.
The issue I'm having (besides trashing my piezo sensor by melting the shit out of it) is that the LED wants to stay on all the time, with or without the Piezo attached. I don't know what I'm missing on this. Any help would be greatly appreciated! I pasted the code I'm using below.
100ohm attached to the negative is of the LED which is then attached to GRND and Pin 5 for positive, 1Mohm resistor soldered to pins A0 and GRND, 9v connection attached to RAW and GRND pins.
Thanks!
This is the code I'm using:
const int ledPin = 5; // led connected to digital pin 5
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 20; // threshold value to decide when the detected sound is a knock or not
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
unsigned long lastPeakTime = 0; //used for calculating peak
int currentPeak = 0;
int blinkCounter = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port for debugging
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
if (sensorReading > currentPeak) {
currentPeak = sensorReading;
}
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
Serial.print(sensorReading);
Serial.println(" Knock! ");
while(blinkCounter <= 3)
{
Serial.println(blinkCounter);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
blinkCounter++;
}
blinkCounter = 0;
delay(50);
}
//DEBUG CODE THAT GETS WRITTEN TO SERIAL, NOT NECESSARY TO LEAVE IN
if(millis() - lastPeakTime > 2000) {
Serial.print("Peak value: ");
Serial.println(currentPeak);
lastPeakTime = millis();
currentPeak = 0;
}
}