val = analogRead(knockSensor);
if (val <= THRESHOLD) {......
It seems you assume a piezo element generates a positive DC voltage.
That statement might only work for only one half of one sinewave of the string's base frequency.
Maybe you should read the sensor constantly until the peak A/D values have decreased below the threshold.
I like to see how you have attached that (unknown) piezo.
Leo..
The circuit I use for piezo elements is use a 47k/47k voltage divider to one terminal of the sensor,
then the other terminal to the Arduino pin.
The signal is centred at 2.5V and the high impedance of the divider limits the current that can flow. You
can then see the whole waveform as its offset to mid-rail.
You can add a resistance in parallel with the piezo element to decrease the sensitivity and
to stop it drifting at DC.
val = analogRead(knockSensor);
if (val <= THRESHOLD) {......
It seems you assume a piezo element generates a positive DC voltage.
That statement might only work for only one half of one sinewave of the string's base frequency.
Maybe you should read the sensor constantly until the peak A/D values have decreased below the threshold.
I like to see how you have attached that (unknown) piezo.
Leo..
Thanks for all the comments guys. I really appreciate this advice
I have since changed:
val = analogRead(knockSensor);
if (val <= THRESHOLD) {......
Something to try.
This example repeatedly reads the piezo 100x and stores the highest peak into "finalValue".
"finalValue" is always available for the rest of the code.
See code if too sensitive.
During testing it could detect a pin dropping on a desk 50cm away from the piezo.
Leo..
const byte piezoPin = A0;
int rawValue; // raw A/D readings
int piezoValue; // peak value
int finalValue; // smoothed final value
void setup() {
analogReference(INTERNAL); // remove this line if too sensitive
Serial.begin(9600);
}
void loop() {
piezo();
//other code
}
void piezo() {
for (int x = 0; x < 100; x++) {
rawValue = analogRead(piezoPin); // 100 A/D readings
if (rawValue > piezoValue) {
piezoValue = rawValue; // store peaks
}
}
if (finalValue < piezoValue) { // fast attack
finalValue = piezoValue;
}
else {
finalValue = (finalValue + piezoValue) / 2; // smooth decay
}
piezoValue = 0; // reset
if (finalValue > 0) {
Serial.println(finalValue); // print only if > 0
}
}
Wawa:
Something to try.
This example repeatedly reads the piezo 100x and stores the highest peak into "finalValue".
"finalValue" is always available for the rest of the code.
See code if too sensitive.
During testing it could detect a pin dropping on a desk 50cm away from the piezo.
Leo..
const byte piezoPin = A0;
int rawValue; // raw A/D readings
int piezoValue; // peak value
int finalValue; // smoothed final value
void setup() {
analogReference(INTERNAL); // remove this line if too sensitive
Serial.begin(9600);
}
void loop() {
piezo();
//other code
}
void piezo() {
for (int x = 0; x < 100; x++) {
rawValue = analogRead(piezoPin); // 100 A/D readings
if (rawValue > piezoValue) {
piezoValue = rawValue; // store peaks
}
}
if (finalValue < piezoValue) { // fast attack
finalValue = piezoValue;
}
else {
finalValue = (finalValue + piezoValue) / 2; // smooth decay
}
piezoValue = 0; // reset
if (finalValue > 0) {
Serial.println(finalValue); // print only if > 0
}
}
Thanks for taking the time to suggest this! I am currently trying to work this into my current code using trial and error. Any pointers would be much appreciated!:
#include <Servo.h>
Servo myservo;
int knockSensor = 0;
byte val = 0;
int THRESHOLD = 0;
int buttonPin = 2;
int count = 1;
void setup() {
myservo.attach(9);
myservo.write(90);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
val = analogRead(knockSensor);
while (val > THRESHOLD) {
val = analogRead(knockSensor);
}
if (count == 1) {
delay(1000);
myservo.write(85);
delay (175);
myservo.write(95);
delay (260);
myservo.write(90);
delay (1000);
count ++;
}
val = analogRead(knockSensor);
while (val > THRESHOLD) {
val = analogRead(knockSensor);
}
if (count ==2) {
delay(1000);
myservo.write(80);
delay (140);
myservo.write(100);
delay (140);
myservo.write(90);
delay (1000);
count ++;
}
val = analogRead(knockSensor);
while (val > THRESHOLD) {
val = analogRead(knockSensor);
}
if (count ==3) {
delay(1000);
myservo.write(30);
delay (120);
myservo.write(130);
delay (120);
myservo.write(90);
delay (1000);
count ++;
}
delay (1000);
if(count > 3) count = 1;
}
}
First step would be to try the code with the piezo setup you have.
See if it works, before you combine.
A picture of the setup (post#3) would be nice.
Leo..
Wawa:
First step would be to try the code with the piezo setup you have.
See if it works, before you combine.
A picture of the setup (post#3) would be nice.
Leo..
I will post a picture as soon as I get the chance and will inform you if the code worked with my current setup.
polymorph:
Schematic? How are you protecting the Arduino input? A piezo puts out positive and negative voltage, and with a sharp rap it can be quite large.
I don't think common 1" piezo disks can produce enough current to damage a pin.
The internal clamping diodes will limit the voltage to VCC +0.5volt and GND -0.5volt.
Leo..
Well, I don't see the difference between a voltage spike from a piezo and a static discharge.
I have just tested a common bare 1" piezo with 1Megohm load resistor on a scope (1Megohm probe).
The piezo was put on the desk with some (soft) weight on it.
Knocking on the desk next to the piezo produced only a few volts.
I could only get more than 5volt by tapping directly onto the brass of the disk.
Current was measured by shorting the piezo with a 1k resistor.
I could only get it slightly over 1mA by hitting the brass directly with a hard object, like a screwdriver.
I don't want this to end in another pin protection argument like we did before.
Use external clamping diodes if you want.
In this case I don't see the need for it.
Leo..