Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created April 18, 2010 08:36
Show Gist options
  • Save tatsuro-ueda/370111 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/370111 to your computer and use it in GitHub Desktop.
/*
Melody
http://arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
// 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 };
int buttonValue0 = 0;
int buttonValue1 = 0;
int buttonValue2 = 0;
int buttonValue3 = 0;
void setup() {
// Serial.begin(9600);
}
void loop() {
buttonValue0 = analogRead(0);
buttonValue1 = analogRead(1);
buttonValue2 = analogRead(2);
buttonValue3 = analogRead(3);
// Serial.println(buttonValue0);
// Serial.println(buttonValue1);
// Serial.println(buttonValue2);
// Serial.println(buttonValue3);
if (buttonValue0 > 1) {
playMerody();
}
delay(100);
}
void playMerody () {
// 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];
tone(8, 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment