Piezo Buzzers as Knock Sensors
i didn't know else go information, thought make post, in hopes reach before experience headache have experienced. examples @ www.arduino.cc/en/tutorial/knock? , http://www.arduino.cc/en/tutorial/knocksensor cause think something's not working correctly. it's in code. these examples should updated more proper working software approach problem. following rest of these tutorials fine, when software, take @ following *bold* line:
notice here, fundamental flaw, delay executes every iteration of main loop. creates problem, may trying detect knock happens during delay (it's likely, @ 1/10 of second). fix code behave more "as expected", move delay inside if statement, right after serial.println( "knock!" ); statement. still provides debounce protection intended (protection detecting multiple knocks 1 knock), while maintaining accurate initial knock detection. if want, can tune delay amount based on whether need ability detect faster intervals between knocks (smaller delay, smaller interval between knocks can be). make sure aren't getting "double knocks". happy hacking!
code: [select]
/* knock sensor
* ----------------
*
* program using piezo element if knock sensor.
*
* have listen analog pin , detect
* if signal goes on threshold. writes
* "knock" serial port if threshold crossed,
* , toggles led on pin 13.
*
* (cleft) 2005 d. cuartielles k3
* edited scott fitzgerald 14 april 2013
*/
int ledpin = 13;
int knocksensor = 0;
byte val = 0;
int statepin = low;
int threshold = 100;
void setup() {
pinmode(ledpin, output);
serial.begin(9600);
}
void loop() {
val = analogread(knocksensor);
if (val >= threshold) {
statepin = !statepin;
digitalwrite(ledpin, statepin);
serial.println("knock!");
}
[b]delay(100);[/b] // have make delay avoid overloading serial port
}
notice here, fundamental flaw, delay executes every iteration of main loop. creates problem, may trying detect knock happens during delay (it's likely, @ 1/10 of second). fix code behave more "as expected", move delay inside if statement, right after serial.println( "knock!" ); statement. still provides debounce protection intended (protection detecting multiple knocks 1 knock), while maintaining accurate initial knock detection. if want, can tune delay amount based on whether need ability detect faster intervals between knocks (smaller delay, smaller interval between knocks can be). make sure aren't getting "double knocks". happy hacking!
where go, or go to, code on tutorial pages changed?
Arduino Forum > Using Arduino > Project Guidance > Piezo Buzzers as Knock Sensors
arduino
Comments
Post a Comment