Skip to content

Instantly share code, notes, and snippets.

@krobro
Created March 4, 2015 05:13
Show Gist options
  • Save krobro/ec63e8652eae910ac8d5 to your computer and use it in GitHub Desktop.
Save krobro/ec63e8652eae910ac8d5 to your computer and use it in GitHub Desktop.
//I got my DuinoBot to play In the Hall of the Mountain King!
//Here's the sketch if you want to try it out.
//(I've also included the sketch to stop the program, because
//I foolishly started it before I could stop it from playing. all. day.)
//I used one of the examples to do this, DelayTone.
//http://www.kendormusic.com/str2004/parts/9841p1.jpg
//This is only the first 4 bars of the melody, so if any of you are interested
//in building upon this you can add on after bar 30. :)
//Hall of the mountain king:
/*
toneDelay Example:
Based on toneMelody by Tom Igoe.
The toneDelay function is an alternative to the tone function.
Basically is the same except it is a blocking function, i.e. it does not use a timer for frequency generation
Author: Federico Lanza
*/
#include "Arduino.h"
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_B2, NOTE_CS3, NOTE_D3, NOTE_E3, NOTE_FS3, NOTE_D3, NOTE_FS3, NOTE_F3, NOTE_CS3, NOTE_F3, NOTE_E3, NOTE_C3, NOTE_E3, NOTE_B2, NOTE_CS3, NOTE_D3, NOTE_E3, NOTE_FS3, NOTE_D3, NOTE_FS3, NOTE_B3, NOTE_A3, NOTE_FS3, NOTE_D3, NOTE_FS3, NOTE_A3};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,8,8,8,8,4,8,8,4,8,8,4,8,8,8,8,8,8,8,8,8,8,8,8,2};
void setup() {
pinMode(SPEAKER, OUTPUT);
}
void loop() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 26; 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];
toneDelay(SPEAKER, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
int pauseBetweenNotes = noteDuration * 0.01;
delay(pauseBetweenNotes);
}
}
//Stop hall of the mountain king:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment