# Play a random Tone string

Hello,

For my mothers christmas shop i have build an IR gate with RF transmitter. When someone walks in the barn i would like to hear a melody. This is working now but it seem nice to me if the melody can be random each time. I tried a lot but this step wont work… Can you gus help?

Kind regards!

This is the part where I want to select my new melody for this time.

``````void subSelectMelody(){
curMelody = 3; //random(0.5 , 3.4);

// Tannenbaum
if (curMelody == 1){
int melody[] = { NC3, NF3, NF3, NF3, NG3, NA3, NA3, NA3,
NA3, NG3, NA3, NAS3, NE3, NG3, NF3};
int noteDurations[] = {
2, 2, 4, 2, 2, 2, 4, 2,
4, 4, 4, 1, 2, 2, 2 };

cntNotes = 15;
}

// Stille Nacht
if (curMelody == 2){
int melody[] = {NG3, NA3, NG3, NE3, NG3, NA3, NG3, NE3,
ND4, ND4, ND4, NB3, NC4, NC4, NC4, NG3};

int noteDurations[] = {
1.385, 4, 2, 1.2,1.385, 4, 2, 1.2,
2, 1, 2, 1, 2, 1, 2, 1 };
cntNotes = 16;
}

// we wish you a merry christmas  http://arduinomelodies.blogspot.nl/
if (curMelody == 3){
int melody[] = {
NG4,NC5,NC5,ND5,NC5,NB4,NA4,NA4,NA4,ND5,ND5,NE5,ND5,NC5,NB4,NG4,
NG4,NE5,NE5,NF5,NE5,ND5,NC5,NA5,NG4,NG4,NA4,ND5,NB4,NC5,
NG4,NC5,NC5,ND5,NC5,NB4,NA4,NA4,
NA4,ND5,ND5,NE5,ND5,NC5,NB4,NG4,
NG4,NE5,NE5,NF5,NE5,ND5,NC5,NA5,NG4,NG4,NA4,ND5,NB4,NC5,
NG4,NC5,NC5,NC5,NB4,NB4,NC5,NB4,NA4,NG4,
ND5,NE5,ND5,ND5,NC5,NC5,NG5,NG4,NG4,NG4,NA4,ND5,NB4,NC5
};

int noteDurations[] = {
4,4,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,4,8,8,8,8,4,4,8,8,4,4,4,2,
4,4,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,4,8,8,8,8,4,4,8,8,4,4,4,2,
4,4,4,4,2,4,4,4,4,2,4,4,8,8,8,8,4,4,8,8,4,4,4,2,
};
cntNotes = 84;
}
}
``````
``````if (curMelody == 3){
int melody[] = {
``````

This creates a new local variable called melody. This is exactly the same name as the global array melody. This confuses both you and the compiler. This local variable is also lost when you go outside the if statement so you will think it never changes.

You can not redefine an array so you are best to declare an array for each tune and pass that array to the routine that plays the tones.

Thanks for the advice!!! Stupid that I ovelooked that option. Go try it tomorow:)