Playing with Speed

The timing for each note is calculated based on variables, as such we can tweak the sound of each note or the timing. To change the speed of the melody, we only need to change one line:

     int tempo = 300; --> int tempo = (new #)

Change new # to a larger number to slow the melody down, or a smaller number to speed it up!

Tuning the Notes

If you are worried about the notes being a little bit out of tune, this can be fixed as well. The notes have been calculated based on a formula from the comment block at the top of the program. But to tune individual notes just adjust their values in the tones[] array up or down until they sound right. Each note is matched by its name in the names[] array.

For example: If we want a c (c=1915) to be a higher pitch, we need to find it's initial value:

     char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

     int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

and then change the number within tones[] to something larger, let's go with 1975:

     char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

     int tones[] = { 1975, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

Composing your own Melodies:

While the program is pre-set to play 'Twinkle Twinkle Little Star', the program can easily be changed to play something else. 

A song takes the format of one integer (int length) and two arrays (char notes[] and int beats[]). 

int length - defines the number of notes

char notes[] - defines each note

int beat[] - defines how long each note will be played for

Twinkle Twinkle Little Star

int length = 15;
char notes[] = {"ccggaagffeeddc"};
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };

Happy Birthday (First line)

int length = 13;
char notes[] = {"ccdcfeccdcgf"};
int beats[] = {1,1,1,1,1,2,1,1,1,1,1,2,4};

To become familiar with how code works, it's always a good idea to look at examples. Above are two songs. Run them, and then modify them to compose your own melody.

This guide was first published on Aug 18, 2017. It was last updated on Jul 13, 2024.

This page (Make It Better) was last updated on Jun 26, 2017.

Text editor powered by tinymce.