Audio Piggy Bank
Description
For this lab, I used both the FSR and the Piezo speaker. The FSR was taped to a 9V battery and placed in a piggy bank so that coins that are dropped in the slot in the piggy bank fall on the sensor. Every coin that falls on the sensor causes the Piezo speaker (which is also embedded in the piggy bank) to play a tone. For each coin that is dropped into the piggy bank, the tone increases in pitch, until it reaches a maximum pitch level, at which point the tone reverts to the low starting tone. For each coin that is dropped in the piggy bank, Arduino also increments the number of coins that have been dropped and prints out a message showing how many coins the user has saved on the screen. The piggy bank can also be "emptied" by any serial input on the keyboard. Typing anything will cause the number of coins to revert to 0 and Arduino will print a message notifying the user that the piggy bank has been emptied. Any coins that are dropped in the piggy bank afterwards will start with the beginning low tone.
Materials
Piggy bank, FSR, Piezo speaker, Arduino board, 9V battery with tape to hold the FSR in place
Arduino Code
/*
* Resistive Sensor Input, Piezo speaker output
* Takes the input from a resistive sensor, e.g., FSR.
* Plays a tone on the Piezo speaker if the signal from the FSR is greater than 10.
* Keeps track of how many times a valid signal has been read from the sensor
* and increases the pitch of the tone accordingly.
* Prints out the number of times the sensor has been activated.
* Any keyboard input clears the number of sensor readings and resets the tone to be played.
*/
int sensorPin = 0; // select the input pin for the sensor
int speakerPin = 10; // select the output pin for the speaker
int val = 0; // variable to store the value coming from the sensor
int numCoins = 0; // variable to hold the number of times a value greater than 10 is read
int delaytime = 2000; // variable to hold delay time for piezo output
int serialByte = 0; // variable to hold input from serial port (keyboard)
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
if (Serial.available() > 0){
serialByte = Serial.read(); // when serial value is read
numCoins = 0; // clear number of coins
delaytime = 2000; // reset tone
Serial.println("The piggy bank has been emptied."); // print message confirming reset
}
digitalWrite(speakerPin, LOW);
val = analogRead(sensorPin); // read the value from the sensor, 0-1023
if (val > 10) {
numCoins = numCoins + 1; // increment number of coins
delaytime = delaytime - 100; // decrement delaytime (increase tone pitch)
if (delaytime < 100){ // reset tone pitch
delaytime = 2000;
}
for( int i=0; i<500; i++ ) { // play for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(delaytime);
digitalWrite(speakerPin, LOW);
delayMicroseconds(delaytime);
}
Serial.print("You have saved "); // print out number of valid FSR readings
Serial.print(numCoins);
Serial.println(" coin(s).");
}
}
Sample Video
Click here
Comments
i couldn't view the video
i couldn't view the video because I don't have the codec :( please bring it to lab!