Assignment2_Piezo_Veronica

The assignment was to design a circuit that uses sound. My project uses a piezo element sensor that when you “knock” you heels together the LED light up.


//P. Comp 1 - Sept. 27, 2013 - Veronica Black //http://www.arduino.cc/en/Tutorial/Knock //http://www.adafruit.com/blog/2009/05/19/piezo-with-an-arduino-photoresistor/ //http://learn.adafruit.com/photocells/using-a-photocell

//#include “pitches.h” //include this header
// these constants won’t change:
const int ledPin = 13; // led connected to digital pin 13
const int ledPin2 = 12;
const int knockSensor = A0; // 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

int val = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(ledPin2, OUTPUT);
Serial.begin(9600); // use the serial port
}

void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);

/*tone(sensorReading, NOTE_B0); //each note or number
delay(300); //how long the note stays on
tone(sensorReading, NOTE_A4);
delay(400);*/

/*sensorReading = map(sensorReading, 1, 15, 31, 3000); //mapping
tone(knockSensor, sensorReading);*/

// 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);
digitalWrite(ledPin2, 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

}