Combination of many switches with different results


i'm new here. Can you help me please. I think it isn't so difficult, but I don't know the answer now.
If I would create a MIDI Piano (I don't do it) I would have many switches and every switch would have one sound.
My Problem is, that I have to create an instrument that can play sounds with combinations of switches. Like a trumpet. You have three valves but with combination of these three valves. So you can have 8 different combinations to press the three valves.

One example:
Switch 1 = LED A
Switch 2 = LED B
Switch 1 and Switch 2 together = LED C

Has anyone an idea how to say that to my Arduino?

Thank you so much! :slight_smile:

Read and save the values from all the switches. Then use some suitable IF statements to decide what to do.

The Arduino is very fast. The time taken to do all that would not be noticed by a human.


Just a little thought...
A, B or C isn't always A+B = C

A+B may form beats/harmonics or other known artefacts. It all depends on the frequencies of A & B (among other things)

Umm.. So A & B are not binary values but perhaps functions of time? Like frequencies? Or inputs to a synth filter?

-jim lee

byte buttonA = 2; // pin assignments
byte buttonB = 3;
byte buttonC = 4;

byte outputNote = 6;

byte buttonsPressed;

void setup(){
pinMode (buttonA, INPUT_PULLUP);
pinMode (buttonB, INPUT_PULLUP);
pinMode (buttonC, INPUT_PULLUP);

pinMode (outputNote, OUTPUT);

void loop(){
buttonsPressed = (digitalRead(buttonC) * 4) + (digitalRead(buttonB) * 2) + digitalRead(buttonA); 
// result is a unique number from 0 to 7

switch (buttonsPressed){
case 0:
// play note for no buttons, or maybe no buttons = no note
case 1:
// play note for A
case 2:
// B
case 3:
// B & A
case 4:
// C
case 5:
// C & A
case 6;
// C & B
case 7:
// C & B & A
} // end switch
} //end loop

I'd add a fourth button, only play one of the 8 notes (0 to 7) when the 4th button was pressed, be like playing a note only when wind was blowing thru the trumpet. So only go into the switch:cases when the 4th button was pressed.

See my "piano tones from micros()" sketch here for a way to create the notes & be very responsive to buttons being pressed: