Arduino Piano - Using 2 speakers for 2 voices?

Hello,

I'm currently working on an Arduino Piano thing and I have a little problem;
So I currently have 8 buttons on my breadboard so it can play a full octave (C4 - C5).
After getting that to work using this crappy code:

byte NOTE_C = 262;
byte NOTE_D = 294;
byte NOTE_E = 330;
byte NOTE_F = 349;
byte NOTE_G = 392;
byte NOTE_A = 440;
byte NOTE_B = 494;  
byte NOTE_C1 = 523;

#define ACTIVATED LOW 

const byte PIEZO = 11;
const byte LED = 0;    // <--  ignore this thing for now

const byte BUTTON_C = 12;
const byte BUTTON_D = 13;
const byte BUTTON_E = 10;
const byte BUTTON_F = 9;
const byte BUTTON_G = 8;
const byte BUTTON_A = 7;
const byte BUTTON_B = 6;
const byte BUTTON_C1 = 5;


void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(BUTTON_C, INPUT);
  digitalWrite(BUTTON_C,HIGH);
  pinMode(BUTTON_D, INPUT);
  digitalWrite(BUTTON_D,HIGH);
  pinMode(BUTTON_E, INPUT);
  digitalWrite(BUTTON_E,HIGH);
  pinMode(BUTTON_F, INPUT);
  digitalWrite(BUTTON_F,HIGH);
  pinMode(BUTTON_G, INPUT);
  digitalWrite(BUTTON_G,HIGH);
  pinMode(BUTTON_A, INPUT);
  digitalWrite(BUTTON_A,HIGH);
  pinMode(BUTTON_B, INPUT);
  digitalWrite(BUTTON_B,HIGH);
  pinMode(BUTTON_C1, INPUT);
  digitalWrite(BUTTON_C1,HIGH);
  
  digitalWrite(LED,LOW);
}

void loop()
{
  while(digitalRead(BUTTON_C) == ACTIVATED)
  {
    tone(PIEZO,NOTE_C);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_D) == ACTIVATED)
  {
    tone(PIEZO,NOTE_D);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_E) == ACTIVATED)
  {
    tone(PIEZO,NOTE_E);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_F) == ACTIVATED)
  {
    tone(PIEZO,NOTE_F);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_G) == ACTIVATED)
  {
    tone(PIEZO,NOTE_G);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_A) == ACTIVATED)
  {
    tone(PIEZO,NOTE_A);
    digitalWrite(LED,HIGH);
  }

  while(digitalRead(BUTTON_B) == ACTIVATED)
  {
    tone(PIEZO,NOTE_B);
    digitalWrite(LED,HIGH);
  }
  while(digitalRead(BUTTON_C1) == ACTIVATED)
  {
    tone(PIEZO,NOTE_C1);
    digitalWrite(LED,HIGH);
  }

  noTone(PIEZO);
  digitalWrite(LED,LOW);

}

I wanted to implement a feature, where you could have 2 voices using 2 different speakers; you press a button, it plays a note. If you press a different button at the same time as the first button you get 2 tones. So my problem is that I can't figure out how to make 2 speakers work with my current code without making ten billion if statements.
Also, I don't want to mess with the Tone Library thing I saw elsewhere as I think that doesn't use 2 speakers and also I'm not sure if the default timer in my Arduino Uno board can handle both speakers.
(I have an NE555 THT timer if that would come helpful).

I hope you understood my question and the way I wrote it in,
cause I don't.

Side question: I'm also running out of digital ports on the board for my buttons and I want to add more in the future. Is there a way to make some sort of button matrix so they can run on less ports?

Any help would be greatly appreciated,

Sincerely,

  • some guy with an internet connection

have a look at this library; its fundamentally does what the builtin 'tone' function does but it appear that you can do multiple instances of it here: