Use a 3x3 LED matrix, each led individually, with a 3x3 button matrix.

Arduino Community

I am an enthusiast of music and technology, and trying to unite both passions I have considered developing this project.

There’s the proyect:

It is a MIDI FootController, a MIDI controller, used with the feet, for guitarists.

is made to control music software (for guitarists) like the amplitube or guitar rig

  • 9 pushbuttons (A 3x3 pushbutton array)
  • 9 Leds (An array of 3x3 LEDs)
  • 2 potentiometers that will act as expression pedals (for volume and / or effects like wah)
  • all controlled by an ARDUINO UNO)

Each button will send its corresponding MIDI signal to activate or deactivate the effects and other characteristics of the software.

I also want that if, by pressing a pushbutton (the (0) (0), for example) the corresponding LED (the (0) (0) of the LED array) will turned on or off according to match. as in the program of switch button.

I have managed to have a correct reading of the push-button array, using the Keypad library.

but, and here’s my first question:

I can not control the array of LEDs properly. Individually, they turn on and off properly but ONLY IF IT WORKS ONE AT A TIME

I read something about multiplexing on Led arrays, but I have not been able to incorporate it into the code with any success. For example, by pressing the button (0)(0) lights the Led (0)(0) on, pressing it agains, the Led turns off (right up to that point). But, if I turn on the led (0)(0), then the led (0)(1) and turn off the LED (0)(1) again, the Led (0)(0) also goes off, even if the status of the pushbutton (0)(0) indicates that it is turned on.

My goal is for each LED to turn on and off independently with each corresponding button, even though LEDs and pushbuttons are in an array.

I have seen an example of what I want to achieve, but there is not much information about it.

I am attaching the code I have developed so far:

#include <Keypad.h>

int state[]={1,2,3,4,5,6,7,8,9};      // the current state of the output pin
int reading[]={1,2,3,4,5,6,7,8,9};    // the current reading from the input pin
int previous[]={1,2,3,4,5,6,7,8,9};   // the previous reading from the input pin

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

int led_rows[]={8,9,10};
int led_cols[]={11,12,13};
const byte rows = 3; 
const byte cols = 3; 
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
};

byte rowPins[rows] = {5, 6, 7}; 
byte colPins[cols] = {2, 3, 4}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup(){
  Serial.begin(9600);
  
  for(int i=1;i<10;i++){
  state[i]=LOW;
  }
  
  for(int i=1;i<10;i++){
  reading[i]=LOW;
  }
  
  for(int i=1;i<10;i++){
  previous[i]=LOW;
  }
  
  for(int i=0;i<3;i++){
  pinMode(led_cols[i], OUTPUT);
  digitalWrite (led_cols[i], HIGH);
  }
  
  for(int i=0;i<3;i++){
  pinMode(led_rows[i], OUTPUT);
  digitalWrite (led_rows[i], LOW);
  }
  
}

void Led1_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led1_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led2_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led2_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led3_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led3_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void Led4_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led4_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led5_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led5_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led6_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led6_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void loop(){
  char key = keypad.getKey();
  
  if (key == '1' && previous[1] == LOW && millis() - time > debounce) {
    if (state[1] == HIGH)
     { 
      state[1] = LOW;
      Led1_Off();
     }
    else
      {
      state[1] = HIGH;
      Led1_On();
      Serial.println(state[1]);
      }

    time = millis();    
  }
  
  previous[1] = reading[1];
  
  if (key == '2' && previous[2] == LOW && millis() - time > debounce) {
    if (state[2] == HIGH)
     { 
      state[2] = LOW;
      Led2_Off();
     }
    else
      {
      state[2] = HIGH;
      Led2_On();
      Serial.println(state[2]);
      }

    time = millis();    
  }
  
  previous[2] = reading[2];
  
  if (key == '3' && previous[3] == LOW && millis() - time > debounce) {
    if (state[3] == HIGH)
     { 
      state[3] = LOW;
      Led3_Off();
     }
    else
      {
      state[3] = HIGH;
      Led3_On();
      Serial.println(state[3]);
      }

    time = millis();    
  }
  
  previous[3] = reading[3];
  
  if (key == '4' && previous[1] == LOW && millis() - time > debounce) {
    if (state[4] == HIGH)
     { 
      state[4] = LOW;
      Led4_Off();
     }
    else
      {
      state[4] = HIGH;
      Led4_On();
      Serial.println(state[4]);
      }

    time = millis();    
  }
  
  previous[4] = reading[4];
  
  if (key == '5' && previous[5] == LOW && millis() - time > debounce) {
    if (state[5] == HIGH)
     { 
      state[5] = LOW;
      Led5_Off();
     }
    else
      {
      state[5] = HIGH;
      Led5_On();
      Serial.println(state[5]);
      }

    time = millis();    
  }
  
  previous[5] = reading[5];
  
  if (key == '6' && previous[6] == LOW && millis() - time > debounce) {
    if (state[6] == HIGH)
     { 
      state[6] = LOW;
      Led6_Off();
     }
    else
      {
      state[6] = HIGH;
      Led6_On();
      Serial.println(state[6]);
      }

    time = millis();    
  }
  
  previous[1] = reading[6];
}

You can enter HERE to see the project in [TINKERCAD

](https://www.tinkercad.com/things/1NzvNunmhAw-pedalera-2/editel?sharecode=Md7r7Qeq6zxKAxLWZAAeVdTlSVWjhT2iyzJhEJvz8QM=)

I also attached a diagram of the connection I made.

Thank you very much, I am open to any idea, help, suggestion since my knowledge in electronics and programming are limited to what I learned with Arduino.

Again, muchas gracias.

Eduardo Robelly.
Quito- Ecuador.

Individually, they turn on and off properly but ONLY IF IT WORKS ONE AT A TIME

Yes that is because your key pad matrix is not designed to read more than one key press at a time. You need a diode in series with each key and some code that looks for more than one key at a time.

You also need to learn how to multiplex an LED display, see Arduino Workshop
Basically the key presses need putting into an array and you multiplex that array, not the long turged piecemeal code you have written.

This project of mine illustrates the sort of thing you need but it is a bit more complex because the LEDs are not single colour but RGB LEDs. Mini Monome