I am not sure if this belong in the software or hardware forum so I will start here.
I have been messing around with the example program Knock just to read the piezo output on the serial monitor. I am using a real drum pad (Yamaha TP65) instead of a generic piezo. I modified the code a bit to remove the LED flash and to add a line count.
My question is sometimes when I hit the pad it triggers more than one hit and other times I actually get no hit. I have tried tweaking the threshold and delay values but can't seem to get an accurate print of the hits. Any thoughts as to what I should look at to try and get it to print line for each hit?
int knockSensor = 0; // the knock sensor will be plugged at analog pin 0
byte val = 0; // variable to store the value read from the sensor pin
int THRESHOLD = 20; // threshold value to decide when the detected sound is a knock or not
int count = 0; // variable to store hit count
void setup() {
Serial.begin(38400); // use the serial port
}
void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable "val"
if (val >= THRESHOLD) {
Serial.print(count, DEC); // print hit count
Serial.print("\t");
Serial.println(val, DEC); // send the piezo value back to the computer, followed by newline
count++;
}
delay(10); // we have to make a delay to avoid overloading the serial port
}
I think a delay of 10ms is too long - that means you're sampling at around 100Hz, and I think you could easily miss a peak. What I would suggest is sampling much faster (maybe even remove the delay), and only send a message when your reading changes:
bool lastOneWasAboveThreshold = false;
void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable "val"
if (val >= THRESHOLD )
{
if( !lastOneWasAboveThreshold )
{
Serial.print(count, DEC); // print hit count
Serial.print("\t");
Serial.println(val, DEC); // send the piezo value back to the computer, followed by newline
count++;
lastOneWasAboveThreshold = true;
}
}
else
lastOneWasAboveThreshold = false;
delay(1);
}
That did help. I tried it and it was till trigging to many falses but it gave me an idea.
Here is what I have now:
int knockSensor = 0; // the knock sensor will be plugged at analog pin 0
byte val = 0; // variable to store the value read from the sensor pin
int THRESHOLD = 15; // threshold value to decide when the detected sound is a knock or not
int count = 0; // variable to store hit count
int timestart = 0;
int timestop = 0;
int timebetweenhits = 45;
void setup() {
Serial.begin(38400); // use the serial port
}
void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable "val"
timestart = millis();
if (val >= THRESHOLD)
{
if( timestart - timestop > timebetweenhits )
{
Serial.print(count, DEC); // print hit count
Serial.print("\t");
Serial.print("Time:");
Serial.print(timestart - timestop);
Serial.print("\t");
Serial.println(val, DEC); // send the piezo value back to the computer, followed by newline
count++;
timestop = millis();
}
}
}
I only get a false hit if I strike really hard. I am not certain my time between hits is fast enough but I am not a very fast player. I will keep testing that. Now to try and get my computer to play sounds for the hits.