Hi guys and ladies!
I bought a Duemilanove (ATM 328) yesterday.
I connected a little piezzo speaker to it. It worked fine.
I uploaded the basic “Melody” example, it worked fine. I changed note lenghts and stuff, it worked fine.
I added new notes to the basic ones and used them and it worked.
Then i started to “compose” the legendary Beatles “Let It Be” tune on it. I added some notes, uploaded, tried. It worked fine till a point.
Now i have 56 notes but the “Duemila” “freezes” about halfway in the tune, it just puts out a low note constatly (after played about half of the notes) till i turn it off.
The code compiles fine. It uses only 1798 bytes from the 30k.
Here’s the code:
int speakerPin = 9;
int length = 56; // the number of notes
char notes[] = "ggaeggCDEEEDDCC gEEFEED EDDC EDCEGAGGEDCagE EEEFEED EDDC"; // a space represents a rest
int beats[] = { 2, 1, 2, 2, 3, 2, 1, 2, 1, 1, 4, 1, 2, 1, 2, 2, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 8, 1, 2, 5, 1, 2, 5, 1, 1, 1, 1, 4, 1, 2, 5, 5, 1, 1, 1, 2, 1, 1, 4, 1, 1, 1, 2, 2};
int tempo = 200;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 851, 760, 716, 628, 568, 507 };
// play the tone corresponding to the note name
for (int i = 0; i < 56; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, 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);
}
}
There is a hardware limit somewhere? It can play only about 25 notes? I fucked up something bad?
Thanks for the answers.