Acrob005
Zo stránky SensorWiki
Make Music
Speaker is a small electronic device which can make sounds if appropriate driven. Lets connect the speaker to the Arduino pin D4 (see schematic diagram and connection diagram below) and program it to make few tones.
#define SPEAKER 4 // Piezospeaker connected to pin 4
void setup()
{
// Example 1: tone of specified frequency and duration
tone(SPEAKER, 4000, 1000); // 4kHz, 1 sec.
delay(500); //small pause
// Example 2: tone of specified frequency without duration
tone(SPEAKER, 2000); // 2kHz, forever
delay(1000);
noTone(SPEAKER); // turn off sound.
}
void loop()
{
/* do nothing */
}
For more details about the tone command see the Reference
How it works
Your turn
Try to examine loudness of the speaker at the different frequencies. Did You notice the change?
Why robot needs to play music?
Microprocessor needs to be RESET before it starts its normal operation. This means that the processor will be setuped into the defined initial state. RESET signal is coming from various sources. One of them is the RESET button on Your board. So You can initialize the processor anytime You find it dead-locked. Another RESET signal comes from the programming interface. When You download new program to the board, it is also initialized before your program starts.
And last, but not least, there is also Power-Up reset system. There is a subcircuit in the internal processor system designed to watch the actual voltage level at the power pins. This prevents to start processor operation during the unstable power-up phase. The operation starts when the power supply is high enough to ensure proper operation of the processor.
This also means, that when Your processor is operated from the batteries and suddenly the battery voltage decreases (e.g when motors require more power and batteries are weak), this also generates the RESET signal. This is unfortunately not visible so You can sometimes find Your robot behaviour very strange.
To solve this problem it is a good idea to add single beep in setup() part of your programm. In the case Your batteries are too weak, you can be sure that Your robot will get into the RESET condition very often (e.g. during the motor start). So when You listen sudden beep during your robot operation, it's likely time to change (or recharge) batteries.
And why there is a label on the speaker? (Remove seal after washing)
See http://www.sonitron.be/site/subpage.php?primid=7&hid=8
Advanced example
Following example shows more sound effects. It comes from the http://www.arduino.cc/en/Tutorial/Melody with just one minor (but important) change: the pinnumber is 4, not 9.
#define SPEAKER_Pin 4
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(SPEAKER_Pin, HIGH);
delayMicroseconds(tone);
digitalWrite(SPEAKER_Pin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup()
{
pinMode(SPEAKER_Pin, OUTPUT);
}
void loop()
{
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
< Previous | Home | Next >