Dies ist eine alte Version des Dokuments!
Die Tetrismelodie besteht aus drei Tonspuren: Sopran, Alt und Bass. Diese sind im Programm als einzelne Melodien gespeichert. Mit mehreren Arduinos kannst du alle drei gleichzeitig abspielen.
Der Sopran ist die Hauptmelodie, die jeder kennt. Wenn du nur eine Tonspur benutzt, dann ist diese hier zu empfehlen.
Aufruf: playMelody( melodyTrack1, durationsTrack3, 59 )
// notes in the melody: int melodyTrack1[] = { NOTE_E3, NOTE_H2, NOTE_C3, NOTE_D3, NOTE_E3, NOTE_D3, NOTE_C3, NOTE_H2, NOTE_A2, NOTE_A2, NOTE_C3, NOTE_E3, NOTE_D3, NOTE_C3, NOTE_H2, NOTE_H2, NOTE_C3, NOTE_D3, NOTE_E3, NOTE_C3, NOTE_A2, NOTE_A2, 0, NOTE_D3, NOTE_F3, NOTE_A3, NOTE_G3, NOTE_F3, NOTE_E3, NOTE_E3, NOTE_C3, NOTE_E3, NOTE_D3, NOTE_C3, NOTE_H2, NOTE_H2, NOTE_C3, NOTE_D3, NOTE_E3, NOTE_C3, NOTE_A2, NOTE_A2, 0, NOTE_E2, NOTE_C2, NOTE_D2, NOTE_H1, NOTE_C2, NOTE_A1, NOTE_GS1, NOTE_H1, NOTE_E2, NOTE_C2, NOTE_D2, NOTE_H1, NOTE_C2, NOTE_E2, NOTE_A2, NOTE_GS2 }; // note durations: 4 = quarter note, 8 = eighth note, etc.: int durationsTrack1[] = { 4,8,8,8,16,16,8,8, 4,8,8,4,8,8, 4,8,8,4,4, 4,4,2, 8,4,8,4,8,8, 4,8,8,4,8,8, 4,8,8,4,4, 4,4,4,4, 2,2,2,2,2,2,2,2, 2,2,2,2,4,4,2,1 };//59
Aufruf: playMelody( melodyTrack2, durationsTrack2, 70 )
int melodyTrack2[] = { NOTE_H2, NOTE_GS2, NOTE_A2, NOTE_H2, NOTE_E3, NOTE_D3, NOTE_A2, NOTE_GS2, NOTE_E2, NOTE_E2, NOTE_A2, NOTE_C3, NOTE_H2, NOTE_A2, NOTE_GS2, NOTE_E2, NOTE_GS2, NOTE_A2, NOTE_H2, NOTE_C3, NOTE_A2, NOTE_E2, NOTE_E2, 0, NOTE_F2, NOTE_A2, NOTE_C3, NOTE_C3, NOTE_C3, NOTE_H2, NOTE_A2, NOTE_G2, NOTE_G2, NOTE_E2, NOTE_G2, NOTE_A2, NOTE_G2, NOTE_F2, NOTE_E2, NOTE_GS2, NOTE_E2, NOTE_GS2, NOTE_A2, NOTE_H2, NOTE_GS2, NOTE_C3, NOTE_GS2, NOTE_A2, NOTE_E2, NOTE_E2, NOTE_E2, 0, NOTE_C2, NOTE_A1, NOTE_H1, NOTE_GS1, NOTE_A1, NOTE_E1, NOTE_E1, NOTE_G1, NOTE_C2, NOTE_A1, NOTE_H1, NOTE_GS1, NOTE_A1, NOTE_C2, NOTE_E2, NOTE_E2 }; int durationsTrack2[] = { 4,8,8,8,16,16,8,8, 4,8,8,4,8,8, 8,8,8,8,4,4, 4,4,2, 8,4,8,8,16,16,8,8, 4,8,8,8,16,16,8,8, 8,8,8,8,8,8,8,8, 8,8,4,4,4, 2,2,2,2,2,2,2,2, 2,2,2,2,4,4,2,1 };//77
Aufruf: playMelody( melodyTrack3, durationsTrack3, 126 )
int melodyTrack3[] = { NOTE_E1, NOTE_E2, NOTE_E1, NOTE_E2, NOTE_E1, NOTE_E2, NOTE_E1, NOTE_E2, NOTE_A1, NOTE_A2, NOTE_A1, NOTE_A2, NOTE_A1, NOTE_A2, NOTE_A1, NOTE_A2, NOTE_GS1, NOTE_GS2, NOTE_GS1, NOTE_GS2, NOTE_GS1, NOTE_GS2, NOTE_GS1, NOTE_GS2, NOTE_A1, NOTE_A2, NOTE_A1, NOTE_A2, NOTE_A1, NOTE_A2, NOTE_H1, NOTE_C2, NOTE_D2, NOTE_D1, 0, NOTE_D1, 0, NOTE_D1, NOTE_A1, NOTE_F1, NOTE_C1, NOTE_C2, 0, NOTE_C2, NOTE_C1, NOTE_G1, 0, NOTE_G1, NOTE_H1, NOTE_H2, 0, NOTE_H2, 0, NOTE_E2, 0, NOTE_GS2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, 0, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_A1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2, NOTE_GS1, NOTE_E2 }; int durationsTrack3[] = { 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,4,4, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8, };//126
Das Basisprogramm hat – wie immer – eine Setup- und eine Loop-Funktion
void setup() { // put your setup code here, to run once: pinMode( 8, OUTPUT ); pinMode( 6, OUTPUT ); pinMode( 4, OUTPUT ); pinMode( 2, OUTPUT ); } // loop spielt hier die Hauptmelodie (Sopran) in einer Endlosschleife. void loop() { playMelody( melodyTrack1, durationsTrack1, 59 ); }
Die playMelody
Funktion bekommt die Melodie, die Notenlängen und die Anzahl der Töne in der Melodie.
void playMelody( int* melody, int* noteDurations, int length ) { // iterate over the notes of the melody: for( int thisNote = 0; thisNote < length; 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 ); // stop the tone playing: noTone(8); } }