Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPin = 5; // led connected to digital pin 5
- const int knockSensor = A0; // the piezo is connected to analog pin 0
- const int threshold = 20; // threshold value to decide when the detected sound is a knock or not
- 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
- unsigned long lastPeakTime = 0; //used for calculating peak
- int currentPeak = 0;
- int blinkCounter = 0;
- void setup() {
- pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
- Serial.begin(9600); // use the serial port for debugging
- }
- void loop() {
- // read the sensor and store it in the variable sensorReading:
- sensorReading = analogRead(knockSensor);
- if (sensorReading > currentPeak) {
- currentPeak = sensorReading;
- }
- // if the sensor reading is greater than the threshold:
- if (sensorReading >= threshold) {
- Serial.print(sensorReading);
- Serial.println(" Knock! ");
- while(blinkCounter <= 3)
- {
- Serial.println(blinkCounter);
- digitalWrite(ledPin, HIGH);
- delay(250);
- digitalWrite(ledPin, LOW);
- delay(250);
- blinkCounter++;
- }
- blinkCounter = 0;
- delay(50);
- }
- //DEBUG CODE THAT GETS WRITTEN TO SERIAL, NOT NECESSARY TO LEAVE IN
- if(millis() - lastPeakTime > 2000) {
- Serial.print("Peak value: ");
- Serial.println(currentPeak);
- lastPeakTime = millis();
- currentPeak = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement