/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
*/
#include <Tone.h>
Tone noteplayer;
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
noteplayer.begin(8);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
noteplayer.play(melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
void loop() {
// no need to repeat the melody.
}
Hi,
I've tested both versions an an ATmega8 board. No differences between 1. and 2., I can only hear and measure a single tone (about 250Hz).
Greatings
Peter
Ok, I've done a second test. Making a tone sweep from 1..10000Hz in 10ms steps.
// Tone Sweep
int x;
void setup()
{
}
void loop()
{
while(x<10000)
{
tone(8, x);
delay(10);
x++;
}
}
The result is not a continous increasing of the frequency, it jumps in only 4 steps, depending on the CPU frequency.
With 1MHz internal clock:
30Hz, 60Hz, 240Hz, 1918Hz.
With 3.6864MHz external clock
113Hz, 225Hz, 900Hz, 7200Hz
no steps between. The frequency steps are 2x,4x,8x of the previous frequency. The CPU frequency divided by the following divisors gives the frequency: 16384, 4096, 512.
now the ToneTest works. ATMega8 has only a TCCR2 register, TCCR2A and TCCR2B like ATmegha168. The bug is writing the prescalerbits into TCCR2B overwrites other bits. Now lets see if the other examples works too.
Sorry, but there is another problem with the tone lib and the ATMega8. Now the sketch gives out the correct frequency, but when the sketch uses the duration parameter of the tone command (like the toneMelody sketch), only the first note is played.
When I change the sketch without using the duration parameter and add a delay command, the sketch works as accepted. Any guess?