Looking for popular song codes

I am sorry if there is any answer to this question that I am too silly to find. I'm doing my first coding project with Adafruit Pro Trinket 3V 12MHz with Arduino ICE (obviously). I am trying to play music with Piezo buzzer. I have tried to find codes for some particular songs but didn't get any success. I have no knowledge of sheet notes too.

Is there a place on this site where I can find already written code for ANY songs or tune? Or any other method in general where I can convert music into codes? I am using Windows.

Thank you!!

Songs: My Neighbor Totoro
River Flow in You
Kiss the Rain
The Path of Wind

How are you playing music now, using the buzzer? Post whatever code you have, using code tags.

jremington:
How are you playing music now, using the buzzer? Post whatever code you have, using code tags.

Yes, playing through the piezo buzzer. And I am getting errors.

int melody[] = {
 NOTE_C6, NOTE_D6,NOTE_E6, NOTE_F6, NOTE_G6,0, NOTE_E6, NOTE_C6,0,NOTE_G6,0,NOTE_F6,NOTE_D6,0,
 NOTE_F6,0, NOTE_D6, NOTE_B5,0,NOTE_F6,0,NOTE_E6,NOTE_C6,0,
 NOTE_G5,0,NOTE_C6,0,NOTE_F6,0,NOTE_E6,NOTE_G6,NOTE_C6,0,
 NOTE_E6,NOTE_F6,NOTE_E6,NOTE_F6,NOTE_E6,NOTE_F6,NOTE_E6,NOTE_C6,NOTE_D6,0,
 NOTE_C6,NOTE_D6,NOTE_E6,NOTE_F6,NOTE_G6,0,NOTE_E6,NOTE_C6,0,NOTE_G6,0,NOTE_F6,NOTE_D6,0,
 NOTE_F6,0,NOTE_D6,NOTE_B5,0,NOTE_F6,0,NOTE_E6,NOTE_C6,0,
 NOTE_A5,0,NOTE_A6,0,NOTE_G6,NOTE_F6,NOTE_E6,NOTE_F6,NOTE_G6,0,NOTE_C6,NOTE_C6,0,
 NOTE_E6,NOTE_F6,NOTE_E6,NOTE_C6,NOTE_F6,NOTE_E6,NOTE_C6,NOTE_A6,NOTE_G6,0,
 NOTE_G5,NOTE_G5,NOTE_F6,NOTE_E6,NOTE_D6,NOTE_E6,NOTE_C6,0,0,

 NOTE_C6,NOTE_B5,NOTE_C6,NOTE_G5,0,
 NOTE_C6,NOT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E_G5,NOTE_G5,NOTE_F6,NOTE_E6,NOTE_D6,NOTE_E6,NOTE_C6,0,0};

 int noteDurations[] = {
   8,8,8,8,8,10,8,8,10,8,10,4,8,2,
   8,10,8,8,10,8,10,4,8,2,
   8,10,8,10,8,10,4,8,4,6,
   8,8,8,8,8,8,8,8,4,4,
   8,8,8,8,8,10,8,8,10,8,10,4,8,2,
   8,10,8,8,10,8,10,4,8,2,
   8,10,8,10,8,8,8,8,4,10,8,8,4,
   8,8,8,8,8,8,8,8,2,4,
   8,8,8,8,8,8,6,1,4,

   4,4,6,2,5,
   4,4,6,2,5,
   4,4,4,8,4,8,4,4,4,2,8,2,1,
   4,4,6,2,5,
   4,4,6,2,5,
   8,8,8,8,8,8,8,4,4,
   8,8,8,4,4,8,8,8,2,4,
   4,4,4,8,4,8,4,8,8,8,1,4,

   8,8,8,8,8,10,8,8,10,8,10,4,8,2,
   8,10,8,8,10,8,10,4,8,2,
   8,10,8,10,8,10,4,8,4,6,
   8,8,8,8,8,8,8,8,4,4,
   8,8,8,8,8,10,8,8,10,8,10,4,8,2,
   8,10,8,8,10,8,10,4,8,2,
   8,10,8,10,8,8,8,8,4,10,8,8,4,
   8,8,8,8,8,8,8,8,2,4,
   8,8,8,8,8,8,6,1,4,};

void setup() {

 for (int thisNote = 0; thisNote < 264; thisNote++) {

   int noteDuration = 1000/noteDurations[thisNote];
   tone(8, melody[thisNote],noteDuration);

   int pauseBetweenNotes = noteDuration * 1.30;
   delay(pauseBetweenNotes);

   noTone(8);
 }
}

void loop () {

}

Please edit your post to add code tags, as requested.

If you want help with error messages, it would be a great idea to post them.

Also I am getting this error:

trial:25: error: stray '\357' in program

NOTE_G5,0,NOTE_C6,0,NOTE_F6,0,NOTE_E6,NOTE_G6,NOTE_C6,0,

^

trial:25: error: stray '\273' in program

trial:25: error: stray '\277' in program

exit status 1
stray '\357' in program

So delete the stray unprintable characters on that line and try again.

But if that's really your complete code it's not going to work with all those undefined NOTE_XX variables. Have you perhaps missed out an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8,8,8,8,6,1,4,

    4,4,6,2,5,
    4,4,6,2,5,
    4,4,4,8,4,8,4,4,4,2,8,2,1,
    4,4,6,2,5,
    4,4,6,2,5,
    8,8,8,8,8,8,8,4,4,
    8,8,8,4,4,8,8,8,2,4,
    4,4,4,8,4,8,4,8,8,8,1,4,

    8, 8, 8, 8,8,10,8,8,10,8,10,4,8,2,
    8,10,8,8,10,8,10,4,8,2,
    8,10,8,10,8,10,4,8,4,6,
    8,8,8,8,8,8,8,8,4,4,
    8, 8, 8, 8,8,10,8,8,10,8,10,4,8,2,
    8,10,8,8,10,8,10,4,8,2,
    8,10,8,10,8,8,8,8,4,10,8,8,4,
    8,8,8,8,8,8,8,8,2,4,
    8,8,8,8,8,8,6,1,4,};

void setup() {

  for (int thisNote = 0; thisNote < 264; thisNote++) {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(8);
  }
}

void loop () {

}

and it is still showing the same error and I couldn't find where the code is wrong?

Totoro:26: error: stray '\357' in program

NOTE_G5,0,NOTE_C6,0,NOTE_F6,0,NOTE_E6,NOTE_G6,NOTE_C6,0,

^

Totoro:26: error: stray '\273' in program

Totoro:26: error: stray '\277' in program

exit status 1
stray '\357' in program

The third line of the third block of notes has an extra something at the start of the line.
Looks like a superscripted '-' or something in my IDE. Delete it.

Totoro:26: error: stray '\357' in program

This comes about by copying something, maybe from a web site that includes a non printable ASCII character.
To remove it.
Go to the line where NOTE_G5, is in black not blue. Put your cursor before the N in note and hit delete until the line jumps up and joins the previous one. Then hit enter and put it on its own line again.

Given that the code is very poorly written.
Why is their a #include "Playtune.h" - you do nothing with it.
Why is everything written in the setup?
Why are you defining a new variables called noteDuration and pauseBetweenNotes every time through the loop, that is 264 variables of the same name you are creating. That is 1056 bytes you are using, over half of the available memory!
You waste more memory by defining the noteDurations array as integers not bytes.

Unfortunately it looks like that code, apart from the Playtune.h, is lifted wholesale from this site's tutorial for using tone() https://www.arduino.cc/en/Tutorial/ToneMelody

People do tend to use what they're given.

Steve

Now the code is working. I am new into it so didn't know whether it was poorly written or perfectly. I just found it and copied it.

Okay, so what is the best way to play sound with lesser memory? I am assuming playing audio from an SD card or from a USB stick? But then what is the best way to do that? Also, I have no knowledge of reading music sheet notes and midi files. Sometimes I didn't even get any midi file so I have looked how to convert audio and video to a midi file and end up with worst tones.

Is it possible to connect Adafruit Trinket Pro to an OTG USB 2.0 Micro Card Reader instead of using SD card shield? or to connect a US stick through OTG micro USB cable? or micro USB OTG cable to connect USB stick?

http://www.amazon.in/Ophion-Reader-Android-Mobiles-Tablets/dp/B01MF6WWU2/ref=sr_1_1?ie=UTF8&qid=1497197590&sr=8-1-spons&keywords=OTG&psc=1

so what is the best way to play sound with lesser memory?

Make

int noteDurations[] into  byte noteDurations[]

change the setup function into:-

void setup() {
  int noteDuration, pauseBetweenNotes;
  for (int thisNote = 0; thisNote < 264; thisNote++) {

    noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(8);
  }
}

Sadly it is one of a number of tutorials written on this web site that is poorly written and try as we might we have not managed to get them changed.

am assuming playing audio from an SD card or from a USB stick?

SD cards are normal, USB cards are very hard to work with. But thir is no need for simple tunes like this. If you want more space than you put the note numbers into an array in program memory.

I have no knowledge of reading music sheet notes

Then look it up it is very very simple to get a note number from sheet music. Then look up what MIDI note number corresponds to the notes read from the music stave.

http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm

But there is no need for simple tunes like this. If you want more space than you put the note numbers into an array in program memory.

This is what I found on the web. But I didn't like it as it plays only one sound. SD card because I am assuming I can play .wav or .mp3 through Arduino and using a speaker might help me with it. Plus, I don't need to worry about notes or midi files, all I have to do it to program it so it plays random songs from SD card once I connect it power source. Something like this:

I have read a couple of forums and I now know using USB with Arduino is not easy for newbies like me so I am going with SD card shield.

In which case get the WAV shield from Adafruit. Adafruit Wave Shield for Arduino Kit [v1.1] : ID 94 : $22.00 : Adafruit Industries, Unique & fun DIY electronics and kits. That not only contains the SD card reader but also a D/A and audio amplifier. You can play WAV files from it. If you want MP3 then you need a separate player as the Arduino is not powerful enough to decode MP3 files.

rajangohil:
Is there a place on this site where I can find already written code for ANY songs or tune?

This is something I found on Github:

But the coded tunes are different, nothing of which you did ask for.

@Grumpy_mike: How about adding SD card breakout with Trinket Pro 3v and a speaker? Do I need other components with it like a resistor or a capacitor or a DAC and an audio amplifies?

@jurs: I saw it but didn't want it. Thanks though.

How about adding SD card breakout with Trinket Pro 3v

Yes if you want.

and a speaker

Well speaker and amplifier, you don't get much sound from a direct speaker, and you need to connect it through a resistor and capacitor to avoid creating damage.

Okay, can you recommend me any informative videos or sites where I can get reliable information on similar projects with a diagram how to assemble it and necessary library?

Just google
Arduino SD card

Then google
Arduino audio amplifier

If you find something you like then shair the link and we can check it out. Do not look at anything with Instructables in the URL because that means it has a 99% chance of being crap.

hey rajangohil you are getting the error because mistakenly you typed a "?"(question mark) in your notes search for ? in your program and remove that your program will run for sure