updated example to match tutorial style guide

This commit is contained in:
Tom Igoe 2009-06-30 20:17:34 +00:00
parent 6b0e34540f
commit 7a80165286
1 changed files with 41 additions and 22 deletions

View File

@ -1,21 +1,34 @@
/* Knock Sensor /* Knock Sensor
* by DojoDave <http://www.0j0.org>
* This sketch reads a piezo element to detect a knocking sound.
* Program using a Piezo element as if it was a knock sensor. It reads an analog pin and compares the result to a set threshold.
* If the result is greater than the threshold, it writes
* We have to basically listen to an analog pin and detect "knock" to the serial port, and toggles the LED on pin 13.
* if the signal goes over a certain threshold. It writes
* "knock" to the serial port if the Threshold is crossed, The circuit:
* and toggles the LED on pin 13. * + connection of the piezo attached to analog in 0
* * - connection of the piezo attached to ground
* http://www.arduino.cc/en/Tutorial/Knock * 1-megohm resistor attached from analog in 0 to ground
*/
int ledPin = 13; // led connected to control pin 13 http://www.arduino.cc/en/Tutorial/Knock
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 created 25 Mar 2007
int statePin = LOW; // variable used to store the last LED status, to toggle the light by David Cuartielles <http://www.0j0.org>
int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not modified 30 Jun 2009
by Tom Igoe
*/
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = 0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup() { void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
@ -23,12 +36,18 @@ void setup() {
} }
void loop() { void loop() {
val = analogRead(knockSensor); // read the sensor and store it in the variable "val" // read the sensor and store it in the variable sensorReading:
if (val >= THRESHOLD) { sensorReading = analogRead(knockSensor);
statePin = !statePin; // toggle the status of the ledPin (this trick doesn't use time cycles)
digitalWrite(ledPin, statePin); // turn the led on or off // if the sensor reading is greater than the threshold:
Serial.println("Knock!"); // send the string "Knock!" back to the computer, followed by newline if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
} }
delay(100); // we have to make a delay to avoid overloading the serial port delay(100); // delay to avoid overloading the serial port buffer
} }