Lighting LED when button pressed

Hello Arduiners, I'm making now an controller and I want to make LED lights when I push any button but I've got problem with more than 1 button. My code look like this

 if (buttonState2 == HIGH) {
       digitalWrite(led, HIGH);   
      } else {
      digitalWrite(led, LOW);    
      }

This one works corectly but when I make more buttons by this way effect look like this (when code is assigned to 2 buttons 1 (1 of 2 buttons make correct effect) lights corectly and 2nd like on photo, with all buttons (code assigned to all buttons) LED don't lights corectly but still lights like on picture):

Perhaps you need to head over to http://snippets-r-us.com with your snippets.

You need to post your code in order to get any feedback.

Hi, can you also post a copy of how you have you project connected, a schematic either CAD or pic of hand drawn will be fine.

Thanks..Hope to help..
Tom... :slight_smile:

Sorry for my mistake :stuck_out_tongue: Im new into this magic and im bit confused. Here is code of buttons (I know there's a lot off mess but i need some work)

    #include <MIDI.h>
     
    //led
    int led = 13;
    //pady read digital pins number 2, 3, 4, 6, 7, 8, 9, 10
    int pushButton2 = 2;
    int pushButton3 = 3;
    int pushButton4 = 4;
    int pushButton6 = 6;
    int pushButton7 = 7;
    int pushButton8 = 8;
    int pushButton9 = 9;
    int pushButton10 = 10;

    // checks if the button is pressed

    int buttonState2 = 0;
    int buttonState3 = 0;
    int buttonState4 = 0;
    int buttonState6 = 0;
    int buttonState7 = 0;
    int buttonState8 = 0;
    int buttonState9 = 0;
    int buttonState10 = 0;

    // play/stop notes in relation to buttons pressed
    int note2 = 0;
    int note3 = 0;
    int note4 = 0;
    int note6 = 0;
    int note7 = 0;
    int note8 = 0;
    int note9 = 0;
    int note10 = 0;
    //potencjometry
    int analogValue = 0;
    int analogValue2 = 0;
    //wart
    int lastAnalogValue = 128;
    int lastAnalogValue2 = 128;
    
    void setup() {

      MIDI.begin(4);
      // 115200 MIDI
      Serial.begin(115200);

      pinMode(pushButton2, INPUT);
      pinMode(pushButton3, INPUT);
      pinMode(pushButton4, INPUT);
      pinMode(pushButton6, INPUT);
      pinMode(pushButton7, INPUT);
      pinMode(pushButton8, INPUT);
      pinMode(pushButton9, INPUT);
      pinMode(pushButton10, INPUT);
      pinMode(led, OUTPUT);

    }
     
    void loop() {
  
      // read state of buttons
      int buttonState2 = digitalRead(pushButton2);
      int buttonState3 = digitalRead(pushButton3);
      int buttonState4 = digitalRead(pushButton4);
      int buttonState6 = digitalRead(pushButton6);
      int buttonState7 = digitalRead(pushButton7);
      int buttonState8 = digitalRead(pushButton8);
      int buttonState9 = digitalRead(pushButton9);
      int buttonState10 = digitalRead(pushButton10);

      //potencjometr 1
      int analogValue = analogRead(A0)/8;     
      if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
      if (analogValue != lastAnalogValue) {
      // send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
      // more info: http://arduinomidilib.sourceforge.net/a00001.html
      MIDI.sendControlChange(1, analogValue, 1);
      lastAnalogValue = analogValue;
    }
  }   
 
 //potencjometr2
  int analogValue2 = analogRead(A1)/8;
  if ((analogValue2-lastAnalogValue2) > 1 || (analogValue2-lastAnalogValue2) < -1) {
  if (analogValue2 != lastAnalogValue2) {
  MIDI.sendControlChange(2, analogValue2, 2);
      lastAnalogValue2 = analogValue2;
    }
  } 

      // Button 1
     
      // when button pressed:
      if (buttonState2 == HIGH) {
      digitalWrite(led, HIGH);
      // if note not playing
      if (note2 == 0) {
      MIDI.sendNoteOn(60,127,1);
          // note is playing
      note2 = 1;
      }
      // when button released
      } else {
      digitalWrite(led, LOW);
        // if note playing
      if (note2 == 1) {
          // if playing - stop
      MIDI.sendNoteOff(60,0,1);
      }
        // if button released note is off
      note2 = 0;
      }
     
     //button 2
     if (buttonState3 == HIGH) {
     digitalWrite(led, HIGH);
     if (note3 == 0) {
     MIDI.sendNoteOn(61,127,1);
     note3 = 1;
     }
     } else {
     digitalWrite(led, LOW);       
     if (note3 == 1) {
     MIDI.sendNoteOff(62,0,1);
     }
     note3 = 0;
     }
    
    // Button 3
      if (buttonState4 == HIGH) {
      digitalWrite(led, HIGH);
      if (note4 == 0) {
      MIDI.sendNoteOn(63,127,1);
      note4 = 1;
      }
      } else {
      digitalWrite(led, LOW);
      if (note4 == 1) {
      MIDI.sendNoteOff(63,0,1);
      }
      note4 = 0;
      }     
      
     //button 4
     
     if (buttonState6 == HIGH) {
     digitalWrite(led, HIGH);
     if (note6 == 0) {
     MIDI.sendNoteOn(64,127,1);
     note6 = 1;
     }
     } else {
     digitalWrite(led, LOW);
     if (note6 == 1) {
     MIDI.sendNoteOff(64,0,1);
     }
     note6 = 0;
     }
      delay(1);
    }

Wiring look like this

Global variables:

    int buttonState2 = 0;
    int buttonState3 = 0;
    int buttonState4 = 0;
    int buttonState6 = 0;
    int buttonState7 = 0;
    int buttonState8 = 0;
    int buttonState9 = 0;
    int buttonState10 = 0;

Local variables with the same names:

      int buttonState2 = digitalRead(pushButton2);
      int buttonState3 = digitalRead(pushButton3);
      int buttonState4 = digitalRead(pushButton4);
      int buttonState6 = digitalRead(pushButton6);
      int buttonState7 = digitalRead(pushButton7);
      int buttonState8 = digitalRead(pushButton8);
      int buttonState9 = digitalRead(pushButton9);
      int buttonState10 = digitalRead(pushButton10);

A REALLY bad idea.

      if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
      if (analogValue != lastAnalogValue) {

If the difference between the values is greater than one, there's not a snowball's chance in hell that they will then be equal.

Your piss-poor indenting makes your code hard to follow. There is a reason that the Tools menu has an Auto Format item. Learn what that reason is.

Instead of 8 switches, get rid of 6 of them. Get two working. Then, put the others back.

Using the internal pullup resistors is so much simpler. Connect one leg of the switch to ground. Connect the other leg to the pin. Turn on the internal pullup resistor by using INPUT_PULLUP in the pinMode statement. Then, LOW is pressed and HIGH is released, just like the top of the switch.

This code is a prime example for using arrays - whenever you have several similar/identical
things to process, use an array, then you only have to write the code once for all of them,
which also means fewer places for bugs and typos to creep in.

Ok... I got another question, is there any option to make external led that blinks when TX led blinks?

is there any option to make external led that blinks when TX led blinks?

No. The TX LED isn't even controlled by the ATMega328 chip. It is controlled by the USB-to-serial converter chip.

Anyway thanks for help :slight_smile: