Created
April 22, 2019 16:12
-
-
Save landjd19/27e428fca0f39731032540fe02554c8c to your computer and use it in GitHub Desktop.
piezo stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int ledPin = 13; // LED connected to digital pin 13 | |
const int knockSensor = A0; // the piezo is connected to analog pin 0 | |
const int threshold = 250; // 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() { | |
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT | |
Serial.begin(9600); // use the serial port | |
} | |
void loop() { | |
// read the sensor and store it in the variable sensorReading: | |
sensorReading = analogRead(knockSensor); | |
// if the sensor reading is greater than the threshold: | |
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); // delay to avoid overloading the serial port buffer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment