Utilisation d’un buzzer piézoélectrique comme interrupteur de la LED via captation de choc.
Montage:
– Piezo sur pin analog 2.
– LED sur pin digital 13.
int ledPin = 13;
int piezoPin = 2;
int THRESHOLD = 100; // set minimum value that indicates a knock
int val = 0; // variable to store the value coming from the sensor
int t = 0; // the "time" measured for how long the knock lasts
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready"); // indicate we're waiting
}
void loop() {
digitalWrite(ledPin,LOW); // indicate we're waiting
val = analogRead(piezoPin); // read piezo
if( val > THRESHOLD ) { // is it bigger than our minimum?
digitalWrite(ledPin, HIGH); // tell the world
t = 0;
while(analogRead(piezoPin) > THRESHOLD) {
t++;
} // wait for it to go LOW (with a little hysteresis)
if(t>100) { // cut off the low values because they're noise
Serial.print("knock! ");
Serial.println(t);
}
}
}
Comments are closed.