Assignment: Input / Output Coincidence Lab Assignment
Collaborators:
This lab was intended to explore the use of input and output in the same device.
The concept was a sleeping baby. He wakes up and cries if someone bumps the table, and depending on the time of day, has a different pitched cry.
Here I used a piezo as a 'shock sensor' to determine when and how hard the object was hit and a second piezo to generate noise (duration depending on how hard it was 'startled'). A light sensor was also used to determine time of day (as a function of light).
* (1) Plastic doll
* (2) Piezo speakers
* (1) Photocell
* Arduino board
* USB Cable
/* 303 Emulator (Tappable Theremin)
* -----------
* Uses a piezo as a knock sensor and light sensor for tone
* Piezo read modeled from http://todbot.com/arduino/sketches/piezo_read/piezo_read.pde
*/
// input
int knockPin = 0;
int pitchPin = 1;
// output
int speakerPin = 7;
int ledPin = 13;
int THRESHOLD = 100; // set minimum value that indicates a knock
int knock = 0; // variable to store the value coming from the knock sensor
int pitch = 0; // variable to store from the value coming from the light sensor
int t = 0; // the "time" measured for how long the knock lasts
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready"); // indicate we're waiting
}
void loop() {
digitalWrite(ledPin,LOW); // indicate we're waiting
digitalWrite(speakerPin,LOW); // indicate we're waiting
knock = analogRead(knockPin); // read piezo
pitch = analogRead(pitchPin)/4; // read pitch
if( knock >= THRESHOLD ) { // is it bigger than our minimum?
digitalWrite(ledPin, HIGH); // tell the world
for( int i=0; i<knock; i++ ) { // play it for 50 cycles
for ( int j=0; j<50; j++ ) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(pitch);
digitalWrite(speakerPin, LOW);
delayMicroseconds(pitch);
}
}
Serial.print("knock:");
Serial.print(knock);
Serial.print(" pitch:");
Serial.println(pitch);
Serial.println();
t = 0;
while(analogRead(knockPin) >= (THRESHOLD/2)) {
t++;
} // wait for it to go LOW (with a little hysteresis)
if(t!=0)
Serial.println(t);
}
}