Project tutorial

A very simple method programming music compared to what's largely available (no complicated math required)

• 9 views
• 0 respects

## Components and supplies

 Rotary potentiometer (generic)
×1
 Speaker, Piezo
×1
 Arduino UNO & Genuino UNO
×1

THEBACKGROUND:

I have always loved music, as well as learning new things, and every time I become aware of something else I can create with, the first question I ask myself is, 'how can I twist this to make something fun?' Such was the case with Arduino and music.

I found several programs offered online to create music, and one I liked more than others, but unfortunately even this one was not very user friendly, and I couldn't bring in a note longer than a whole note. Nor could I use various musical notation such as ties.

So I took the program I preferred, and started to tweak it. Now not only can a note of any length be used, but ties can be added! It is also very user friendly, all one needs to know how to do is count how many beats a note holds, whether it's three and a quarter or seven, and type it in.

### HOW IT WORKS:

This is a very simple program to use to create your own music. Nearly everything works here, from ties/slurs, to staccatos, to the tempo, to notes of nearly any length you desire. This includes notes as long as you desire, down to 32nd notes. You just have to be able to add up simple numbers. And with a simple copy and paste, you can even change it from playing in a loop indefinitely to playing just once.

As opposed to most programs, rather than the whole note getting the value of 1, the quarter note (QN) gets it. Thus, anything smaller than a quarter note is a negative number and the decimal is moved one place over. (IE: the eighth note, has the value of.5. This is turned into the first whole integer, and then made negative due to being less than a QN, leaving it as -5. A 16th note is.25 of a QN. this, made whole, becomes -25.

Numbers higher than 1 are added together to the total number of beats covered. A quarter note is 1, a half note 2. A dotted half note, as it goes through 3 beats, is a 3. A dotted quarter note, having 1.5 beats, becomes 15.

The basic melody is in groups of three: the identified note, the length of the note, and then whether it's a tied note or not. More details on how to use and change this and the rest of the coding are written in the code itself.

### A quick summary:

• 999 to -100: 32nd:.125 -> -125. 32nd + 16th:.375 -> -375
• 99 to -10: 16th:.25 -> -25. 16th + 8th:.75 -> -75
• 9 to -1: 8th=.5 -> -5 two 16ths:.5 -> -5

1 to 9: QN: 1. two 8ths: 1. half note: 2. half + QN: 4. whole: 4. double whole: 8.

10 to 99: QN + 8th: 1.5 -> 15.

100 to 999: QN + 16th: 1.25 -> 125

## Code

##### Musical Program TemplateC/C++
Paste it in, there are further instructions within the code on how to alter it to your preferences.
```#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST  0

int tempo = 169; //change this to whatever speed you want
int buzzer = 11; //change this to whatever pin you want

int melody[] = {
NOTE_C4,1,2, NOTE_D4,-5,2, NOTE_E4,-5,2, NOTE_F4,-5,2, NOTE_G4,-5,2, NOTE_A4,-5,2, NOTE_B4,-5,2, NOTE_C5,1,2,
NOTE_C5,1,1, NOTE_B4,-5,1, NOTE_A4,-5,1, NOTE_G4,-5,1, NOTE_F4,-5,1, NOTE_E4,-5,1, NOTE_D4,-5,1, NOTE_C4,1,1,
REST,1.5,2
/*The first of each group is the note called from pitches.h (this will need to be a separate open tab.)
The second is the length of the note. Look to the top for more details on that.
The third dictates whether it is a tie. 0 means no and 1 means yes. This goes on the first note, but not the second.
Stacattos can be made: IE, a stacatto quarter note is an eighth note and an eighth rest. It sounds the same. */
};

int notes = sizeof(melody) / sizeof(melody[0]) / 2; //technical stuff, leave lines 19-21 be.
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0, tie = 0;

void setup(){
//this is empty, unless you only want the song playing once.
//if you DO only want it playing once, move all from loop to here, so loop is empty instead.//
}

void loop() {
/* This is all technical stuff. Don't really mess with it. Unless you want to play it only once. Or get rid
of the ties. To get rid of ties, change note*3 to note*2, and thisNote+3 to thisNote+2. Don't bother with
the third grouping in the commas of melody, and simply delete the int tie=0 somewhere above. Ah yes, and with
the if...else below, keep the internals of tie==0, delete the loops, and paste where the loops were. */

for (int thisNote = 0; thisNote < notes * 3; thisNote = thisNote + 3) {
divider = melody[thisNote + 1];
tie = melody[thisNote + 2];

if (divider >0 && divider <10){
noteDuration = (wholenote)/4;
noteDuration *= divider;
//this is for quarter notes and larger
}
else if (divider >=10 && divider <100){
noteDuration = (wholenote)/40;
noteDuration *= divider;
//for larger than quarter with one decimal (5.5:55)
}
else if (divider >=100 && divider <1000){
noteDuration = (wholenote)/400;
noteDuration *= divider;
//for larger than quarter with two decimals (3.75:375)
}
else if (divider <0 && divider > -10){
noteDuration = (wholenote)/40;
noteDuration *= abs(divider);
//for less than quarter with one decimal (.5:-5)
}
else if (divider <= -10 && divider > -100){
noteDuration = (wholenote)/400;
noteDuration *= abs(divider);
//for less than quarter with two decimals (.25:-25)
}
else if (divider <= -100 && divider > -1000){
noteDuration = (wholenote)/4000;
noteDuration *= abs(divider);
//for less than quarter with three decimals (.125:-125)
}

if (tie==2){
tone(buzzer, melody[thisNote], noteDuration * 0.9);
delay(noteDuration);
noTone(buzzer);}
else if (tie==1){
tone(buzzer, melody[thisNote], noteDuration);
delay(noteDuration);
noTone(buzzer);}
else if (tie==0){
return;
}
}
}
```

## Schematics

• 1 project
• 0 followers

#### Published on

March 19, 2020
See similar projects
you might like

#### Make Musical Instrument Using Arduino and Flick Large

Project tutorial by LanmiLab

• 3,532 views
• 16 respects

#### Motion-Activated Musical Trophy

Project tutorial by Alex Wulff

• 1,963 views
• 5 respects

#### Musical Cube

Project showcase by FabLab of Legends

• 2,175 views
• 1 respect

#### Xmas musical robot

Project showcase by benoitdr

• 1,090 views
• 4 respects

#### Arduino Musical Tesla Coil

Project in progress by Brandon Michelsen

• 10,918 views