Simultaneous sounds on Arduino – Melody, harmony, chords

After a long research LinuxCircle team found this neat piece of code to play notes on two speakers simultaneously. This is very usefull when you want to add ornaments to the songs, such as harmony and chords.

The problems with the current tone() function is that it only plays one tone at a time, and also it interferes with PWM pins, so we can’t use it to play buzzers on Rasinro which uses PWM to control the motors.

The file uses complex logic of loops, timer, and interrups and allows the main melody and harmony tones played at the same time. So if there is two set of tunes, it can be set to play even at a slightly different intervals or slower, to create a “echoing” effect.

Please take a look at 2_voices and and post questions here.

(Visited 2,636 times, 1 visits today)

4 thoughts on “Simultaneous sounds on Arduino – Melody, harmony, chords”

  1. MerFunk says:

    This is exactly what I was wondering if arduino was capable of, thanks for posting. I’m new to the whole arduino environment, may I ask how you would loop this? So far from what I’ve tested basically moving the setup code to the loop section works for most things, but for this it still played just the once.

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      Hi,

      You can group the ‘singing’ parts into one function, and then call that function from within the loop() section.

  2. sim says:

    hi! i am programming a melody and a harmony with the arduino, and i am trying to repeat the melody with a pause in between, but instead of pausing, the harmony plays a random note and then plays something other than I programmed. I tried making the melody all rests and left the harmony alone, but the harmony then played something really different than I programmed. why isn’t the harmony working the way I want it to?

    1. Hi, You must make sure that the melody notation actually match with the tempo notations. As you can see in all the songs, each melody would have a corresponding tempo. The numbers in the tempo reflect the time delay, e.g. how long you want to play a note.

      Supposed you want to have a long delay, you would put:
      int underworld_melody1[] = {
      0
      }

      int underworld_tempo[] = {
      800
      }

      Try and let us know!

Comments are closed.