An analog read example using a piezo element out of the SparkFun Inventor's Kit with a MSP-
EXP430F5529LP....
/* Knock Sensor
This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes
"knock" to the serial port, and toggles the LED on pin 13.
The circuit:
+ connection of the piezo attached to pin 23 (P6.0)
- connection of the piezo attached to ground
1-megohm resistor attached from pin 23 (P6.0) to GND
http://www.arduino.cc/en/Tutorial/Knock
created 25 Mar 20070
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified 4 Feb 2013
by Frank Milburn for MSP-EXP430F5529LP
This example code is in the public domain.
*/
// these constants won't change:
const int ledPin = RED_LED; // connected the red LED on the LaunchPad
const int knockSensor = P6_0; // the pin the piezo is connected to
const int threshold = 20; // threshold value to decide when the detected sound is a knock or not
// NOTE: You may need to vary this depending on your sensor
// 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()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // initiate the serial connection and let the user know we've started
Serial.println("Starting to listen.... ");
}
void loop()
{
sensorReading = analogRead(knockSensor); // read the sensor and store it in the variable sensorReading:
if (sensorReading >= threshold) // if the sensor reading is greater than the threshold:
{
ledState = !ledState; // toggle the status of the ledPin:
digitalWrite(ledPin, ledState); // and update the LED pin itself:
Serial.print("Knock! Reading was:"); // let the user know there was a knock and how loud it was
Serial.println(sensorReading);
}
delay(30); // delay to avoid overloading the serial port buffer
// vary this delay if multiple knocks or no knocks are detected
}
No comments:
Post a Comment