1 function when pushing 2 buttons?

Hello
I have been programming my arduino to play certain musical notes when you push a button.
Basically you can say I have programmed it to act as a basic square wave synth.

Not to the question:
At the moment the synth is monophonic, but I want to add an arpeggiator function so that if I press two notes at the same time there will be an arpeggio between these notes.

I don't know how to write this in code and I've been googling around but I can't find a solution.

Here's the code. Help me if you can! :slight_smile:

const int button1Pin = 2;  // button 1 connected via I/0-pin no.2
const int button2Pin = 3;  // etc
const int button3Pin = 4;
const int button4Pin = 5;
const int button5Pin = 6;
const int button6Pin = 7;
const int button7Pin = 8;
const int button8Pin = 9;
const int speakerPin =  13;    // speaker pin


void setup()
{
  
  pinMode(button1Pin, INPUT);           // setting up the buttons to become inputs
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(button5Pin, INPUT);
  pinMode(button6Pin, INPUT);
  pinMode(button7Pin, INPUT);
  pinMode(button8Pin, INPUT);
  pinMode(speakerPin, OUTPUT);          // setting the speakerpin as output (speaker connected to I/0 no.13
  Serial.begin(9600);                   // starting serial messaging
}


void loop()
{
  int button1State, button2State, button3State, button4State, button5State, button6State, button7State, button8State;  // variables to hold the pushbutton states

  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Pin);
  button5State = digitalRead(button5Pin);
  button6State = digitalRead(button6Pin);
  button7State = digitalRead(button7Pin);
  button8State = digitalRead(button8Pin);

  if (button1State == HIGH){         
    tone(speakerPin, 130.81);         // C ----- (((  variables = output pin and number value is frequenzy in hertz  )))
    Serial.println("C");              // Sending the name of the note via serial
  }else if (button2State == HIGH){
    tone(speakerPin, 146.83);         // D
    Serial.println("D");
  }else if (button3State == HIGH){
    tone(speakerPin, 164.81);         // E
    Serial.println("E");
  }else if (button4State == HIGH){
    tone(speakerPin, 174.61);         // F
    Serial.println("F");
  }else if (button5State == HIGH){
    tone(speakerPin, 196);            // G
    Serial.println("G");
  }else if (button6State == HIGH){
    tone(speakerPin, 220);            // A
    Serial.println("A");              
  }else if (button7State == HIGH){
    tone (speakerPin, 246.94);        // B
    Serial.println("B");
  }else if (button8State == HIGH){
    tone(speakerPin, 261.63);         // C2
    Serial.println("C2");
    }else{
      
    noTone(speakerPin);               // if no button is pushed - no sound will be played
    
  
    
  }
}

I don't know how to write this in code and I've been googling around but I can't find a solution.

It seems pretty obvious to me.

if(button1State == HIGH && button2State == HIGH)
{
   // Two switches are pressed...

PaulS:
It seems pretty obvious to me.

if(button1State == HIGH && button2State == HIGH)

{
  // Two switches are pressed...

Well, now I tried. All tones sounds scrambled and the only thing that is functioning is the arpeggio between the two tones.
I want to be able to play all tones, but be able to play arpeggio with sertain intervals like button2 and button5 (a quint), and button 2, 4, 6 (A-minor arpeggio).

Well, now I tried.

But failed to post the new code.

All tones sounds scrambled

All of them? Or all where you try to play arpeggio? (Sorry, I have no idea what that means.)

ok so I asked my daughter who plays music what you are asking for and her explanation is that when you press 2 buttons you want the first tone (first button pressed) to stop and the next tone (second button that's being pressed at the same time but pressed later than the first) to take over

that means you will have to know which button is pressed first then scan for a second button being pushed then make the switch from playing one tone to the other

I know if I was going to attempt this I would need to understand how to use a "for" loop "array" for holding the input pin numbers and probably a "switch" for holding the tones being played

might be something like read button pressed when you see a different button ignore (flag) what ever the first button read until the first button is released.

hopefully someone like pauls can come up with a better idea

@gpop1, no, but you have a smart daughter. That is called key rollover. If you can do it with 2 keys, it's called 2 key rollover. If more, "n" key rollover. It's essential logic for a smoothly responding keyboard. In other words, a key doesn't have to be released before another one can be pressed. At least, that's the way I understand it. I had an old 8 bit computer once that didn't have rollover. It was a royal pain to type on.

But I don't think that is the OP's intent. I think they want a rapid alternation between the notes that are held down. Like a "trill".

might be something like read button pressed when you see a different button ignore (flag) what ever the first button read until the first button is released.

It's a simple matter of separating the determining of which switch(es) are pressed from acting on the result.

On any given pass through loop, one would choose what to do based on the most complex state (6 keys pressed in order, 5 keys pressed, 4 keys, etc.) first, then the simpler state.

We can't tell where OP added the code to deal with multiple keys being pressed simultaneously (before the single key code or after it) or whether the multiple and single key cases are handled together or separately. And, of course, "it doesn't work" doesn't mean squat.