Sound box
Description:
A box that detects when it is knocked and responds back, using piezo sensors for input and output.
Video:
http://www.youtube.com/watch?v=WSD6PbzcTnE (turn up the sound!)
Materials:
- Metal peppermint creams box, found in the abandoned TUI box
- One bare peizo element
- One housed piezo element
Arduino code:
int analogInPin = A5; int sensorValue; int speakerPin = 10; int scale = 0; int buffer[50]; void setup() { memset(buffer,0,50); Serial.begin(9600); pinMode(speakerPin, OUTPUT); } void loop() { // read sensor value into a 20 item buffer, store a 0 as 1 (helps later when comparing values) // sensorValue = analogRead(analogInPin); if(sensorValue == 0) { sensorValue++; } for(int g = 49; g>0; g--) { int f = g - 1; buffer[g] = buffer[f]; } buffer[0] = sensorValue; // buffer output debug, throws so much data at the serial that it slows down the program. /* for(int z=0; z<20; z++) { Serial.print(buffer[z]); Serial.print(" "); } Serial.println(" "); */ delay(20); // look at what happened 1.9 and 2.0 seconds ago int diff48_49 = buffer[48] - buffer[49]; // sound if it is a significant thump // needed because the piezo sensor gradually changes its reading, steadily // approaching 1023 (~1 or 2 per .1 sec) after being struck if( abs(diff48_49) > 5) { // a larger difference means a harder hit, scale it to a PWM value to control volume scale = map(abs(diff48_49),5,200,1,255); if(scale>255) { scale = 255; } // debugosity Serial.print("buffer[48_49]:"); Serial.print(abs(diff48_49)); Serial.print(" scale:"); Serial.println(scale); analogWrite(speakerPin, 0); for( int i=0; i<5; i++ ) { // play it for .1 second analogWrite(speakerPin, scale); delayMicroseconds(10000); analogWrite(speakerPin, 0); delayMicroseconds(10000); } } }