Combine several notes.

Howdie peeps.

At the moment Im making a simple drumset with buttons, its working great I can make sounds and such BUT it can only play a note at a time. What Id like to have help with is, HOW do you make it play sounds in parallell?

Cheers

Start by posting your code - lets see if it's modifiable to play multiple sounds, or if a rewrite is needed.

If it seems a bit dizzy or something, please remember im just getting familiar with all the parts.

  #include "pitches.h"
  
int potPin = 0;
int inPin5 = 5;
int inPin4 = 4;
int inPin3 = 3;
int inPin2 = 2; 
int readVal = 0;

void setup() {
  pinMode(inPin5,INPUT);
  pinMode(inPin4,INPUT);
  pinMode(inPin3,INPUT);
  pinMode(inPin2,INPUT);
 }

void loop() {
     if(digitalRead(inPin5) == HIGH){
    tone(8, NOTE_C4,10);
  }
  else if(digitalRead(inPin4) == HIGH){
    tone(8, NOTE_G3,10);
  }
  else if(digitalRead(inPin3) == HIGH){
    tone(8, NOTE_A3,10);
  }
  else if(digitalRead(inPin2) == HIGH){
    tone(8, NOTE_B3,10);
  }
  else if(analogRead(potPin) > 0){
    readVal = analogRead(potPin);
    tone(8, readVal,10);
  }
}

Edit: The problem must be cause to the IF sentences, I believe.

Try this instead http://arduino.cc/playground/Code/Summer

Or some of the Audio things here http://arduino.cc/playground/Main/InterfacingWithHardware#Audio

Thanks for the fast reply, really appreciated.

The summer thing, thats just to make the codes a bit more “good looking” to ur eyes, correct?
You can play mutiple “summers” but it does not say that u can combine em together.

I will take a deeper look into the other link, however, does some1 has some thoughts?

Edit:
Is the only way of doing that, to have several speakers?
Cause when I get my MIDI cable I will get the sound from the computer and wont be able to use several speakers from the outputs.

Thanks

How about the Octosynth from the Audio page section then?

http://www.cs.nott.ac.uk/~jqm/?p=605

CrossRoads: How about the Octosynth from the Audio page section then?

http://www.cs.nott.ac.uk/~jqm/?p=605

I just took a look into the source code, waah that was very much for my mind I must say. I dunno where to look in the code, I went to the void loop where everything actually happens but there was to much other stuffs combined together. So that is way to much for me.

Once again, thanks for helping me out dude!

Bump!

Presumably you have read the reference page for tone() which states that only one tone can be played at a time.

Your other option is to make your own tones, blink without delay style. Try this:

unsigned long note1time = 1136; // Middle A tone 440 Hz period/2, microseconds
unsigned long note2time = 1100; // do the math for other notes 
unsigned long note3time = 1060;
unsigned long note4time = 1020;

unsigned long note1end; // end of period/2
unsigned long note2end;
unsigned long note3end;
unsigned long note4end;

byte note1button = 2; // keypress button, input pin with pullup resistor
byte note2button = 3;
byte note3button = 4;
byte note4button = 5;

byte note1pin = 6; //the output pin - need external mix via opamp
byte note2pin = 7;
byte note3pin = 8;
byte note4pin = 9;

byte note1state; // state of the note, 1 or 0
byte note2state;
byte note3state;
byte note4state;

byte note1playing; // flag to show if note is playing or not
byte note2playing;
byte note3playing;
byte note4playing;


void setup(){
  pinMode (note1button, INPUT);  // key press button
  digitalWrite (note1button, HIGH); // internal pullup
  pinMode (note1pin, OUTPUT); // output pin
  pinMode (note2button, INPUT);
  digitalWrite (note2button, HIGH);
  pinMode (note2pin, OUTPUT);
  pinMode (note3button, INPUT);
  digitalWrite (note3button, HIGH);
  pinMode (note3pin, OUTPUT);
  pinMode (note4button, INPUT);
  digitalWrite (note4button, HIGH);
  pinMode (note4pin, OUTPUT);
}
void loop(){
  if (digitalRead (note1button) == LOW) {
    if (note1playing == 0)  // if key is pressed and note is not started
    { 
      note1playing = 1; // set note flag to playing
      note1end = micros() + note1time; // capture time to make note change state
      digitalWrite (note1pin, (1-note1state)); // start the note at high level (replace the write with something faster)
    }
    if (note1playing == 1){ // if key is pressed and note is already playing
      if (micros()>=note1end){ // see if its time to change note state
        note1end = note1end + note1time; // set time for next change
        digitalWrite (note1pin, 1-note1state); //  change the note state (1->0, 0->1)
      } 
    }
  }
  else {
    note1playing = 0; //set up for next note press
    note1state = 0;  // set up for next note press
    digitalWrite (note1pin, LOW); // turn off output
  } 
  // repeat for for notes 2,3,4
  if (digitalRead (note2button) == LOW) {
    if (note2playing == 0)  // if key is pressed and note is not started
    { 
      note2playing = 1; // set note flag to playing
      note2end = micros() + note2time; // capture time to make note change state
      digitalWrite (note2pin, (1-note2state)); // start the note at high level (replace the write with something faster)
    }
    if (note2playing == 1){ // if key is pressed and note is already playing
      if (micros()>=note2end){ // see if its time to change note state
        note2end = note2end + note2time; // set time for next change
        digitalWrite (note2pin, 1-note2state); //  change the note state (1->0, 0->1)
      } 
    }
  }
  else {
    note2playing = 0; //set up for next note press
    note2state = 0;  // set up for next note press
    digitalWrite (note2pin, LOW); // turn off output
  }
  if (digitalRead (note3button) == LOW) {
    if (note3playing == 0)  // if key is pressed and note is not started
    { 
      note3playing = 1; // set note flag to playing
      note3end = micros() + note3time; // capture time to make note change state
      digitalWrite (note3pin, (1-note3state)); // start the note at high level (replace the write with something faster)
    }
    if (note3playing == 1){ // if key is pressed and note is already playing
      if (micros()>=note3end){ // see if its time to change note state
        note3end = note3end + note3time; // set time for next change
        digitalWrite (note3pin, 1-note3state); //  change the note state (1->0, 0->1)
      } 
    }
  }
  else {
    note3playing = 0; //set up for next note press
    note3state = 0;  // set up for next note press
    digitalWrite (note3pin, LOW); // turn off output
  }
  if (digitalRead (note4button) == LOW) {
    if (note4playing == 0)  // if key is pressed and note is not started
    { 
      note4playing = 1; // set note flag to playing
      note4end = micros() + note4time; // capture time to make note change state
      digitalWrite (note4pin, (1-note4state)); // start the note at high level (replace the write with something faster)
    }
    if (note4playing == 1){ // if key is pressed and note is already playing
      if (micros()>=note4end){ // see if its time to change note state
        note4end = note4end + note4time; // set time for next change
        digitalWrite (note4pin, 1-note4state); //  change the note state (1->0, 0->1)
      } 
    }
  }
  else {
    note4playing = 0; //set up for next note press
    note4state = 0;  // set up for next note press
    digitalWrite (note4pin, LOW); // turn off output
  }
} // end void loop

Thanks again for taking your time to help me through, I really appreciate it.

Ive taken a look through the code and tried to understand whats really going on.

What kind of "mix" does he use? What is a mix? :P I assume he has several mixes that each one output a sound.

  if (digitalRead (note1button) == LOW) {
1.    if (note1playing == 0)  // if key is pressed and note is not started
2.    { 
3.      note1playing = 1; // set note flag to playing
4.      note1end = micros() + note1time; // capture time to make note change state
5.      digitalWrite (note1pin, (1-note1state)); // start the note at high level (replace the write with something faster)
6.    }
7.    if (note1playing == 1){ // if key is pressed and note is already playing
8.      if (micros()>=note1end){ // see if its time to change note state
9.        note1end = note1end + note1time; // set time for next change
10.        digitalWrite (note1pin, 1-note1state); //  change the note state (1->0, 0->1)
      } 
    }
  }

If we take a look at this part of the code. Line 4 and 9 = Keeps track for how long we have been holding down the button. Line 5 and 10 = This is the part where we make the output sound, and also the part that is confusing me a bit. He uses his "mix" here with the opamp, but I cannot see where he really output the specific tone.

I hope Im not making myself look dumb or something.

Cheers

There are 4 outputs: note1pin, note2pin, note3pin, note4pin.
These will be oscillating up & down at hopefully audio rates.
Line 4 determines when to change from Low to High after the note key is pressed.
Line 9 determines when it subsequently changes from High to Low, Low to High, High to Low, Low to High, until the note key is released.

Lines 5 and 10 determine whether the note is High or Low.
Assuming it starts Low (0), then in line 5 the output changes from Low to High (1-0 = 1)
In Line 10 it subsequently changes state (1-0 = 1, 1-1 = 0, 1-0 =1, 1-1=0…)

Outputs are mixed with an op-amp:

http://masteringelectronicsdesign.com/how-to-derive-the-summing-amplifier-transfer-function/

The sketch I wrote will only make tone-like sounds. For drum-like sounds, envelope control is needed also. Maybe add SPI control of a quad DAC with separate Vref inputs for each DAC.

http://www.analog.com/static/imported-files/data_sheets/AD5334_5335_5336_5344.pdf

The created tone feeds the Vref, the DAC will act as a digital gain control so the envelope can be manipulated. Fast rise time, asymptotic fall, for instance.

notexpin connects to the Vref inputs, the op-amp mixer goes after the quad DAC.