Who here is good with electronics (Arduino Project)

3-0-hate

Captain Nimcompoop
Full Member
Minuteman
Jun 13, 2011
721
422
43
Lost in Idaho...
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;
}


}
 
1E13C0F1-6DFF-431B-98D9-43F585227F51.gif
 
I've really been wanting to do this same thing as well with the knock sensor. Hopefully someone will be able to help out here, or if you sort it out on your own please post back with what you end up running for parts/code.
I've read that some people have had issues with the shock throwing the sensor off the plate when using Velcro to attach it. But I imagine that is relative to the type of you use, and plate weight, impact energy etc. If you don't have some for it already, you might want to go for the highest holding power you can find.
 
  • Like
Reactions: 3-0-hate
The resistor should go on the positive leg of the LED not the ground. Also be aware there are two models of the Pro Micro, 3.3 V and 5 V, you may need to adjust the LED current limiting resister depending which you have and the specs on your LED's
 
  • Like
Reactions: 3-0-hate
Your logic loop seems to be fine. However, you have a variable that is declared but never referenced in your code:

int ledState = LOW;

Don’t think it is actually needed since you seem to toggle the led light on and off directly by setting the value on the ledPin to HIGH and then LOW to make it blink 3 times for each Knock.

Nevertheless, it would be good to use this variable to set and track the state changes of the led while the program is running.
 
  • Like
Reactions: 3-0-hate
At the beginning of the loop, set the pin going to the LED to LOW with
digitalWrite(ledPin, LOW);
just in case it is initialized as HIGH when you define the pin. The LED should be off now without any signal from sensor.
 
  • Like
Reactions: 3-0-hate