5 buttons

Hello,

I am trying to do something very simple, to connect inline five buttons in order to control it after in BOME Midi Translator. I just need each button to release a different signal in my console.

I succeed to connect one button, but it does not work when there are five buttons.

Here is the code and the schema of the connection.

It doesn't work because when I open the console, I have random 1 to 5 number, as if I pressed all buttons without a break.

const int buttonPin1 = 13;     
const int buttonPin2 = 12; 
const int buttonPin3 = 10; 
const int buttonPin4 = 9; 
const int buttonPin5 = 8; 




int buttonState1 = LOW;         
int buttonState2 = LOW; 
int buttonState3 = LOW; 
int buttonState4 = LOW;
int buttonState5 = LOW;  


void setup() {

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  Serial.begin(9600);
}

void loop() {

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);


  
  if (buttonState1 == HIGH) {
   
       Serial.write('1');
    delay(300);
    
    
  } else {


   
  }

 if (buttonState2 == HIGH) {
   
      Serial.write('2');
    delay(300);
     
    
  } else {


   
  }

   if (buttonState3 == HIGH) {
   
      Serial.write('3');
    delay(300);
     
    
  } else {


   
  }

   if (buttonState4 == HIGH) {
   
       Serial.write('4');
    delay(300);
    
  } else {


    
   
  }

  if (buttonState5 == HIGH) {
   
       Serial.write('5');
    delay(300);
    
  }
  else {

    
   
  }



 }

Well done for using code tags and posting something like a schematic.

Your digital inputs are "floating" and could read high or low at random. Pressing the buttons may or may not have any effect. When a button is pressed, the Arduino's pin will read low, but when the button is released, it may read low anyway. Change your code to use "INPUT_PULLUP" instead of "INPUT".

Also you should learn to use arrays and for() loops to make your code less repetitive.

You have the buttons switching to ground but you're checking for them being HIGH. And you're not using INPUT_PULLUP nor do you have any external pullup resistors so your results are about what I'd expect. You need one or the other. Probably easiest just to change the pinmodes to INPUT_PULLUP and the Ifs to check for LOW.

And what is the point of all those "else do nothing" statements?

Steve

It works !

Thank you very much :slight_smile: