Drop the ball to turn the light on and off.
https://www.youtube.com/watch?v=9DUadJ-OBf0
A drop is detected by an analogRead on the piezo speaker. After some experimentation I came to a fairly low threshold (10) to recognize soft drops. I think I could have weighted the ball more to increase reliability and get more sizable readings.
I didn't do much coding of my own on this one. Mostly re-using the knock sensor example to gather input from the piezo speaker.
/* Knock Sensor
* ----------------
*
* Program using a Piezo element as if it was a knock sensor.
*
* We have to basically listen to an analog pin and detect
* if the signal goes over a certain threshold. It writes
* "knock" to the serial port if the Threshold is crossed,
* and toggles the LED on pin 13.
*
* (cleft) 2005 D. Cuartielles for K3
* edited by Scott Fitzgerald 14 April 2013
*/
int ledPin = 11;
int knockSensor = 5;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 10;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(knockSensor);
Serial.println(val);
if (val >= THRESHOLD) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
Serial.println("Knock!");
}
delay(300); // we have to make a delay to avoid overloading the serial port
}
- Login to post comments