Group B

Q13: Write a program using a piezo element and use it to play a tune after someone knocks.

Piezo Tune on Knock

Solution and implementation for Q13 from Internet of Things (iotl).

piezo_tune_on_knock.cpp Download
const int buzzer = A1;             // LED connected to digital pin 13
const int knockSensor = A1;        // the piezo is connected to analog pin 0
const int threshold = 400;         // 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

void setup() {
    pinMode(buzzer, INPUT);
}

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) {
        pinMode(buzzer, OUTPUT);
        tone(buzzer, 261);  // Sounds the buzzer at the frequency relative to the note C in Hz
        delay(200);         // Waits some time to turn off
        noTone(buzzer);     // Turns the buzzer off

        tone(buzzer, 293);  // Sounds the buzzer at the frequency relative to the note D in Hz
        delay(200);
        noTone(buzzer);     // Turns the buzzer off

        tone(buzzer, 329);  // Sounds the buzzer at the frequency relative to the note E in Hz
        delay(200);
        noTone(buzzer);     // Turns the buzzer off

        tone(buzzer, 349);  // Sounds the buzzer at the frequency relative to the note F in Hz
        delay(200);
        noTone(buzzer);     // Turns the buzzer off

        tone(buzzer, 392);  // Sounds the buzzer at the frequency relative to the note G in Hz
        delay(200);
        noTone(buzzer);     // Turns the buzzer off
    }

    delay(100);  // delay to avoid overloading the serial port buffer
}

Other Questions in Internet of Things

See All Available Questions
Download