Greenwich mean time pips

Hi

I was working late last night at a simple buzzer melody. In fact, it's not really a melody because it's just one note. I couldn't find for the life of me how to modify the example code.

I tried everything from adjusting the tempo, to the beats, to the pauses...sometimes I got an infernal loop, sometimes half the 'tune' played - sometimes all of it but incredibly fast! Funny, really.

Is there a generator that will produce the code for the following? I've googled but not found one.

1Khz tone.

0.1s pip.
0.9s pause.
0.1s pip.
0.9s pause.
0.1s pip.
0.9s pause.
0.1s pip.
0.9s pause.
0.1s pip.
0.9s pause.
0.5s pip.

Thanks... maybe it was just late but this didn't seem to be going may way! :slight_smile:

The piezo buzzer is a very small one - it's powered to my arduino without any transistor or similar.

If I get a physically larger buzzer does it follow that it'll be louder?

:slight_smile:

Look at the tone() function - http://arduino.cc/en/Reference/Tone.

delay() takes an argument in milliseconds, so a 0.9 second gap is delay(900).

int pinSpeaker = 8;

void setup () {
    pinMode(pinSpeaker, OUTPUT);
}

void loop () {
    playTone(100, 1000);
    delay(900);
    playTone(100, 1000);
    delay(900);
    playTone(100, 1000);
    delay(900);
    playTone(100, 1000);
    delay(900);
    playTone(100, 1000);
    delay(900);
    playTone(500, 1000);
    delay(9000);

}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

For the benefit of future Googlers who struggled like I did...