Any way to program to play multiple tones?

So I see I can play sounds by connecting force sensitive resistors. But how can I play multiple tons by programming them in?

EDIT: just to clarify, multiple, as in more than one tone playing at one time.

Any guides or help would be appreciated.

The tone function uses a timer. That timer can only do one thing at a time. If you want to play more than one note at a time, you need a piano.

The Tone Library...

http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

...supports playing more than one note at a time.

But, as PaulS said, you are limited to one tone per timer. On the Arduino Uno there are three timers. One is used by the core (essentially not available to you) so you can play up to two tones at once.

If you want to play more than one note at a time, you need a piano.

:smiley: XD :smiley:

So I see I can play sounds by connecting force sensitive resistors. But how can I play multiple tons by programming them in?

I assume you’re refering to this example code?

void setup() {
// initialize serial communications (for debugging only):
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the pitch to the range of the analog input.
// change the minimum and maximum input numbers below
// depending on the range your sensor’s giving:
int thisPitch = map(sensorReading, 400, 1000, 100, 1000);

// play the pitch:
tone(9, thisPitch, 10);

}

Notice the lines I highlighted - they are the lines that change the pitch. If you program the tomes in you will want to change these lines. Check out this example: http://arduino.cc/en/Tutorial/Tone

Onions.

Does anyone remember the ZX Spectrum (48K)? It made all of those sounds (plus the rest) with a single port pin. Oh, and it had a weaker CPU that was equivalent to an arduino running at 0.885 MHz. This doesn’t actually solve your problem, but it did appear to be a good opportunity to say it, sorry :slight_smile:

Update: ok, it is possible to make lots of tones with one timer, but it does require some programming skills. You need a fast timer. Then you need some data structure to manage the state of the several channels (one channel = one tone). The trick is, to play a tone in one channel, you need to toggle a port bit every few timer calls, such that you get the desired tone frequency. The timer frequency needs to be such that you your tone frequency error is small. Then you put a diode and a resistor in series with all “tone” pins and connect together at the end; that’s where your mixed channels will come out.
There’s another alternative, using the same technique used by those ancient MOD files on the PC using the speaker. This technique uses 1 PWM port per channel. You can play complex sounds with this technique (like playing a WAV file, a “sample”). Set a high pitch PWM frequency and then modulate the duty cycle; this creates a DAC. Then you just need to have a timer to dump a signal into the DACs, at a fixed frequency (the sampling frequency). You’ll have to figure out the rest for yourself…

fyi, I got three tones to play on the arduino uno

Using the library given by Coding Badly

http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

#include <Tone.h>

Tone tone1;
Tone tone2;
Tone tone3;

void setup()
{
  tone1.begin(13);
  tone2.begin(12);
  tone3.begin(11);
  tone1.play(NOTE_C4);
  tone2.play(NOTE_E4);
  tone3.play(NOTE_G4);
}

void loop()
{
}

Very simple, modified the code they had just to test to see how many tones played and it was three tones. For what I wanted to do, this is great.

I had a nice little C chord playing. Note that I had to play a separate tone out of each pin and I set up the breadboard as a Resistor ladder. Playing the tone out of one pin distorted the tone drastically.

Well Thank you all for helping me.

If you want to play more than one note at a time, you need a piano.

I have two upright pianos, i’m simply curious and just trying to see what Ardunio is capable of :wink: and I couldn’t find the answer online.

Actually, the MOD playing think needs only 1 PWM channel for “any” number of music channels.

Here’s an interesting experience if you’d like to try it. I’m writing from the top of my head, so you’ll have to fill-in the gaps and do some searches.

1st thing you need to find out how to increase the PWM frequency. There’s at least one article on it on the arduino site. Aim for a high frequency, ideally above 20KHz, but anything smaller will do, just with more noise. Then pick a PWM channel and attach a low pass filter to it, to remove the high frequency. This filter can be an RC filter, which takes a resistor and a capacitor. Google it, you’ll find a lot, let’s say on wikipedia. The values should be calculated for a cut off frequency below the PWM frquency. Experiment here. Start with a 10nF - 100nF capacitor and calculate an appropriate resistor.

Now your “analogWrite()” is a DAC. And on a DAC you can play any waveform you want, by feeding it samples at a certain frequency. Let’s say a sawtooth wave (ramps up from 0, then falls suddenly to 0):

for (int loops = 0; loops < 256*500; loops++)  {   // ~500ms tone duration
    for (int sample = 0; sample < 255; i++)  {
        analogWrite(sample);
        delayMicroseconds(4);   // 256 * 4us = 1ms -> 1KHz tone frequency = sampling frequency
    }
}

By controlling the time between samples you control the tone’s frequency. There are some caveats, I’ll let you find out as you go.

You can mix several sounds. Mixing is just the mathematical equivalent of averaging. If you have 2 samples (2 channels) to play now, the mixed sound is the average of the 2, sample by samples. More simultaneous channels, mean average of all of them, sample by sample.

Before you can easily mix channels, you’ll have to make a more flexible structure for the code loops exemplified above. And instead of having the loops running like I showed, in the main loop(), blocking execution, you should set up an interrupt timer running at your sampling frequency, and on each timer call you mix your channels and output the mixed sample to the DAC. You would need data structures to manage how the things are running, and to manage the “tracking”. This is like “wave table”. You can have sound samples and play them at different rates to get different tones; actually, to play a sound sample faster (since it has a fixed length, that is, a fixed number of (byte) samples) you just skip some. Calculations can be made to know how many must be skipped in order to achieve a certain frequency. To play it slower, you repeat the same sample. Etc etc etc… I talk many times about “sample”, this can be confusing because there are 2 types of “sample”, but since you’r einto music, I suppose you know what I’m referring to.

This is a lot of work, especially for whom is not very acquainted with programming.

I’m working on a project playing with frequencies and the golden ratio. I can get 3 frequencies to play at once, but then my formula to increase the base frequency by 10 each step stops working. With the base frequency +1 other frequency it will iterate, but once I add the 3rd it stops. Is this just due to an arduino unos limitations? Another weird thing that happens when I am playing only the base frequency +1 other is that the delay between iterations will change. It will loop slowly, then faster and faster and faster and the pitch will keep increasing. So at this point Im thinking I must be doing something wrong.

I’d appreciate any feedback. I know this threads old.

#include <Tone.h>

Tone freq1;
Tone freq2;
Tone freq3;
int x = 100;

void setup() {
freq1.begin(11);
freq2.begin(12);
freq3.begin(13);
}

void loop() {

  float (x, x < 500, x+=10);
freq1.play(x);
freq2.play(x*1.618);
freq3.play(x/1.618);
delay(1000);
}

With that library, you can only play as many notes as the number of timers available on the microprocessor that your version of Arduino has. Yours probably only has 2.

Edit - by the way, I knew nothing of this 5 minutes ago. I just googled and RTFM.