A Piano able to play more than one note at a time

At first it sounded easy however it became very clearly hard. I have an idea that would suggest I need to edit registers, however I can't seem to grasp the idea. I currently have two buttons two speakers able to play there notes seperately but together they are distorted. I am using a mkr 1010. Any help is welcome, let me know if more information is required. Thank you. Here is my code:

#define C 3830
#define G 2550
#define speaker1 0
#define button1 3
#define speaker2 1
#define button2 5
int buttonState1;
int buttonState2;

void setup() {
  pinMode(speaker1, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(speaker2, OUTPUT);
  pinMode(button2, INPUT);

void loop() {
  if (buttonState1 == HIGH)
      delayMicroseconds(C / 2);
      digitalWrite(speaker1, LOW);
      delayMicroseconds(C / 2);
  if (buttonState2 == HIGH)
      delayMicroseconds(G / 2);
      digitalWrite(speaker2, LOW);
      delayMicroseconds(G / 2);

void buttonstates() {
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);

Have you seen the Tone library (not to be confused with the tone() function)? It can play multiple tones at once. See the documentation for details.

From my understanding the main problem with mine, is attempting to play two different notes on two different speakers at the same time. Yes I know you can play different notes on one speaker. However the goal of this is to create harmonic notes. And have it play almost exactly like a piano could multiple notes at the same time together. However I will look at this code and try it. Maybe I am misunderstanding it.

If you play C and G together on a piano, what you hear is both tones mixed together. Not one after the other. But that is what your code does. When you press both buttons, it plays one cycle of C through speaker1 and then one cycle of G through speaker2, then one cycle of C through speaker1, etc. You need to figure out a way of playing both tones simultaneously, preferably through the same speaker.


Perhaps my speakers are too low quality. But with the arduino it is impossible in it's current form. As it runs through the loop it alternates notes. Because it cannot actually play two tones at the same time. You have to get the delay so short that it appears to be playing simultaneously. However as you get to the 15 millisecond mark it severely distorts the tone and is no longer the sound it should be. This is why is my code I tried using pwm instead of tone(). But when you try this the same problem occurs. The alternating notes is clearly obvious. You need to get it more precise in order to get it to sound correct. However I believe, you need to change the registers to alter the timing in order to accomplish this.

This code uses the Tone library and plays 2 tones, simultaneity, on 2 different pins, that is each tone has its own pin.

#include <Tone.h>

const byte tone6Pin = 6;
const byte tone7Pin = 7;

Tone tone6;
Tone tone7;

void setup()

void loop()

Would this still work after integrating buttons?

Give it a try. If it does not work the way you want, post the code, a description of what the code acctually does and a description of how what it does is different that what you want.

Try my piano project here. Coded to play 13 notes from 13 buttons on a '1284P (which has 32 IO). Pretty well commented, really just a big expansion of Blink Without Delay for 13 outputs.


Youtube video of me playing 8 notes (run out of buttons) https://www.youtube.com/watch?v=4c8idXN4Pg0 The 13 outputs go thru a some resistors and caps to be mixed analog-wise, then played thru a self powered computer speaker, schematic is shown at the topic page also.

A piano has one tone generator for each key, and the tones reach the ear mixed up. A computer can produce the same effect with that number of output signals and the same number of speakers, or by a single speaker for the summed up tones, or by summing up the tones before output as one signal. The latter is how e.g. MP3 players work, and what an Arduino can produce with a DAC for output of an analog signal. Summing up square waves is possible as well, as demonstrated by @@CrossRoads, but the sound then is different from piano sound.

@groundFungus I got an error of 'Tone' does not name a type

Did you install the Tone library? The library is available for install from the IDE Library Manager. Look for the Tone library by Brett Hagman. Or download and install from here.

Otherwise, post the code that gives you the error. The code that I posted was tested on my Uno, so I know that the posted code compiles.

I just copied your code exactly to test it. And it gave me the error.

OK, did you install the library?

Post the entire message(s). You are leaving a lot out when you paraphrase.

I just test compiled the posted code and there were no errors or warnings.