Questions re. analog to keybord, LEDs and MIDI

Hi

I´m new into Arduino, and I´m afraid, that my questions are basic knowledge. Just can´t figure it out :blush:

I´m using an Audrino Pro Micro, 5Volt. Sketch programmer: Audrino version 1.6.1.

Questions:

  1. I want to use 20 buttons. I can make it work with 10 buttons on A0, but I need 10 more.
    Then I try to put in a code for A1, but no luck with that.
    I also wonder, if it is possible to add an analog potentiometer to A2?

Please, can anyone give me some advice?

  1. LEDs: How can I assign 20 LEDs - one for each button?

  2. Is it possible to use the same Pro Micro for the above and MIDI to USB?
    (I have tryed some MIDI to USB examples from the forums, but I can´t make it work, or worse - I don´t understand...) :sob:

Hope it´s giving any meaning for you, and I apologise for my bad English.

Thanks in advance!

Here is the code I´m struggling with:

DIY Guitar Rig 5 Footpedal 




        Analog pin A0
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
          btn1     btn2     btn3    btn4    btn5   btn6    btn7    btn8     btn9    btn10
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K      33K     47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V


Analog pin A1
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
         btn11    btn12    btn13   btn14   btn15  btn16   btn17   btn18    btn19   btn20
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K      33K     47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V


           
           
 */         



int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for assigning the buttons and there high and low values
int Button[21][3] = {{1, 834, 840}, // button 1  *1
                    {2, 734, 738}, // button 2  *2
                    {3, 604, 611}, // button 3  *3
                    {4, 314, 319}, // button 4  *4
                    {5, 189, 191}, // button 5  *5
                    {6, 107, 109}, // button 6  *6
                    {7, 75, 77}, // button 7  *7
                    {8, 45, 47}, // button 8  *8
                    {9, 882, 990}, // button 9  *9
                    {10, 17, 19}}; // button 10  *A
                   

int analogpin = A1; // analog pin to read the buttons


int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int ButtonVal;

int val = 0;

const int yellowled = 16;
const int greenled = 15;
const int redled = 14;
const int Bled = 10;
const int Yled = 9;
const int Gled = 8;
const int Rled = 7;
const int bled = 6;
const int yled = 5;
const int gled = 4;
const int rled = 3;

boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
boolean b4 = false;
boolean b5 = false;
boolean b6 = false;
boolean b7 = false;
boolean b8 = false;
boolean b9 = false;
boolean b10 = false;
boolean b11 = false;
boolean b12 = false;
boolean b13 = false;
boolean b14 = false;
boolean b15 = false;
boolean b16 = false;
boolean b17 = false;
boolean b18 = false;
boolean b19 = false;
boolean b20 = false;


long randNumber;

void setup()
{
 Serial.begin(9600);
 Keyboard.begin();
  randomSeed(analogRead(1));
  randNumber = random(0,2);
  
  val = analogRead(analogpin);    // read the input pin

  Serial.println(val);             // debug value
  delay(100); // a little delay to not hog serial monitor


  int analogpin = A2; // analog pin to read the buttons
  
  
int Button[21][3] = {
                    {11, 834, 840}, // button 1  *B
                    {12, 734, 738}, // button 2  *C
                    {13, 604, 611}, // button 3  *D
                    {14, 314, 319}, // button 4  *E
                    {15, 189, 191}, // button 5  *F
                    {16, 107, 109}, // button 6  *G
                    {17, 75, 77}, // button 7  *H
                    {18, 45, 47}, // button 8  *I
                    {19, 882, 990}, // button 9  *J
                    {20, 17, 19}}; // button 10  *K
                    
                    
  
  Keyboard.begin();
 
  pinMode(yellowled, OUTPUT);
  pinMode(greenled, OUTPUT); 
  pinMode(redled, OUTPUT);
  pinMode(Bled, OUTPUT);
  pinMode(Yled, OUTPUT);
  pinMode(Gled, OUTPUT);
  pinMode(Rled, OUTPUT);
  pinMode(bled, OUTPUT);
  pinMode(yled, OUTPUT);
  pinMode(gled, OUTPUT);
  pinMode(rled, OUTPUT);
 
  randomSeed(analogRead(1));
  randNumber = random(0,2);
 
 
    }

}

void loop()
{
  static float in = 4.712;
  float out;
   // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin);
    if(ButtonVal == current_state && counter >0)
    {
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }
 
  if (b1==true)
  {
  in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 64 + 64;
  analogWrite(rled,out);
  }
  if (b1==false)
  {
    out = 0;
    analogWrite(rled,out);
  }
  if (b2==true)
  {
    in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 264 + 264 + 132;
  analogWrite(gled,out);
  }
  if (b2==false)
  {
    out = 0;
    analogWrite(gled,out);
  }
  if (b3==true)
  {
    in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 64 + 64;
  analogWrite(yled,out);
  }
  if (b3==false)
  {
    out = 0;
    analogWrite(yled,out);
  }
  if (b4==true)
  {
    in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 64 + 64;
  analogWrite(bled,out);
  }
  if (b4==false)
  {
    out = 0;
    analogWrite(bled,out);
    }
    
    
  if (b5==true)
  {
    in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 64 + 64 + 32;
  analogWrite(Rled,out);
  }
  if (b5==false)
  {
    out = 0;
    analogWrite(Rled,out);
  }
 
 if (b6==true)
  {
    in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 264 + 264 + 264;
  analogWrite(Gled,out);
  }
  if (b6==false)
  {
    out = 0;
    analogWrite(Gled,out);
  }
  
  if (b7==true)
  {
  in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 264 + 264 + 132;
  analogWrite(Yled,out);
  }
  if (b7==false)
  {
    out = 0;
    analogWrite(Yled,out);
  }
  
  if (b8==true)
  {
  in = in + 0.0005;
  if (in > 10.995)
    in = 4.712;
  out = sin(in) * 64 + 64;
  analogWrite(Bled,out);
  }
  if (b8==false)
  {
    out = 0;
    analogWrite(Bled,out);
  }


}

void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 21; i++)
  {
    // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      // stores the button number to a variable
      label = Button[i][0];
      Action();     
    }
  }


}

void Action()
{
  if(label == 1)
  {
   b1=true;
   b2=false;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   Keyboard.print('1');
   }
  if(label == 2)
  {
   b1=false;
   b2=true;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   Keyboard.print('2');
 }
  //  if(label == 2)  evt. ect.  

}
  1. I want to use 20 buttons. I can make it work with 10 buttons on A0, but I need 10 more. Then I try to put in a code for A1, but no luck with that.

The buttons on A1 should work the same as the buttons on A0.

I also wonder, if it is possible to add an analog potentiometer to A2?
Yes.

  1. LEDs: How can I assign 20 LEDs - one for each button?
    Since you don't have 20 spare output pins you will need to either add hardware or find 10 spare pins to make a matrix (5 rows and 5 columns) or find five spare pins and learn about Charlieplexing.
  1. Is it possible to use the same Pro Micro for the above and MIDI to USB?
    Depends on what you mean by "MIDI to USB". Can you point to an example?
randomSeed(analogRead(1));

As you are wiring up analog 1 then it is not going to give you a random seed is it.

Then I try to put in a code for A1, but no luck with that.

But that code only ever reads A1, never A0.

Are you saying that if you change it and the wires to A0 this works?
If so you are not swapping over the wiring correctly or your A1 input channel is shot.

Also that code has an unmatched number of braces so it doesn't even compile. It is important you post your code not something "like" it.

int Button[21][3]

Is defined in the setup function and so is not valid anywhere else.

There is lots more wrong with that code. Start simple and test and build up code do not try and write it all at once.

Thank you for your advice and questions!
Hope it´s allright with you, if we can take one question at a time?

Multiply (20) buttons:

I have two 10 resistor ladder, as showen i the following code (I have left out the LED code and some of the code due to maximun characters)

I´m using the same resistor value in the two ladders.
The reason for that is to get the analog read so stabil as possible.

My problem is to define the code for:

"int Button[21][3] = "
and the analog inputs:
"int analogpin = A1; // analog pin to read the buttons"

Can´t compile, because it only can be defined one time.

In addition to this, I also want to attach an analog 10K potentiometer.
It´s also the same problem I´m running into with this.

How can I make this work?

Thank you in advance for your patience and help!

/*


        Analog pin 1
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
          btn1     btn2     btn3    btn4    btn5   btn6    btn7    btn8     btn9  |  btn10 |
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K    |  33K   |  47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V

        Analog pin 2
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
          btn1     btn2     btn3    btn4    btn5   btn6    btn7    btn8     btn9  |  btn10  |
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K   |   33K   |  47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V

          Analog pin 0
      Potemtiometer 10k (Not included in this code)
           


*/


int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for assigning the buttons and there high and low values
int Button[21][3] = {{1, 834, 840}, // button 1  *1
                    {2, 734, 738}, // button 2  *2
                    {3, 604, 611}, // button 3  *3
                    {4, 314, 319}, // button 4  *4
                    {5, 189, 191}, // button 5  *5
                    {6, 107, 109}, // button 6  *6
                    {7, 75, 77}, // button 7  *7
                    {8, 45, 47}, // button 8  *8
                    {9, 882, 990}, // button 9  *9
                    {10, 17, 19}}; // button 10  *A

int Button[21][4] = {{11, 834, 840}, // button 11  *B
                    {12, 734, 738}, // button 12  *C
                    {13, 604, 611}, // button 13  *D
                    {14, 314, 319}, // button 14  *E
                    {15, 189, 191}, // button 15  *F
                    {16, 107, 109}, // button 16  *G
                    {17, 75, 77}, // button 17  *H
                    {18, 45, 47}, // button 18  *I
                    {19, 882, 990}, // button 19  *J
                    {20, 17, 19}}; // button 20  *K


int analogpin = 1; // analog pin to read the buttons
int analogpin = 2; // analog pin to read the buttons
int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int ButtonVal;


boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
boolean b4 = false;
ect.

long randNumber;

void setup()
{
  Keyboard.begin();
 
 
  randomSeed(analogRead(1));
  randNumber = random(0,2);
 

}

void loop()
{
  static float in = 4.712;
  float out;
   // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin);
    if(ButtonVal == current_state && counter >0)
    {
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }
 
}

void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 21; i++)
  {
    // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      // stores the button number to a variable
      label = Button[i][0];
      Action();     
    }
  }
}

void Action()
{
  if(label == 1)
  {
   b1=true;
   b2=false;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   b11=false;
   b12=false;
   b13=false;
   b14=false;
   b15=false;
   b16=false;
   b17=false;
   b18=false;
   b19=false;
   b20=false;
   Keyboard.print('1');
   }
  if(label == 2)
  {
   b1=false;
   b2=true;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   b11=false;
   b12=false;
   b13=false;
   b14=false;
   b15=false;
   b16=false;
   b17=false;
   b18=false;
   b19=false;
   b20=false;
   Keyboard.print('2');
  }
 ect.


}

I´m using the same resistor value in the two ladders.
The reason for that is to get the analog read so stabil as possible.

Using the same resistor value is not going to improve stability.
This seems to be at odds with the graphic comment in your code.

Can´t compile, because it only can be defined one time.

You can't compile it because you are attempting to define the same variable twice within the same scope.
You need the Button variable defined in one go, it will not split up like this.

Then this:-

int analogpin = 1; // analog pin to read the buttons
int analogpin = 2; // analog pin to read the buttons

You are again trying to redefine a variable within the same scope. Not only can you not do this in the C language, it makes no sense at all.

Finally the Action function is just stupid. You should never write code like this. You should use an array to define the Boolean states of your switches and not use a case statement with as many cases as you have buttons. This is just fundamental programming and I would suggest that at the moment you are way out of your depth trying to do this project. I suggest you take more time to learn about code first.

jeor:
How can I make this work?

OK, so if I'd make a guess, the values in your array represent the minimum and maximum 'analogRead()' readings when pressing a single button, right?

int Button[21][3] = {{1, 834, 840}, // button 1  *1
                    {2, 734, 738}, // button 2  *2
                    {3, 604, 611}, // button 3  *3
                    {4, 314, 319}, // button 4  *4
                    {5, 189, 191}, // button 5  *5
                    {6, 107, 109}, // button 6  *6
                    {7, 75, 77}, // button 7  *7
                    {8, 45, 47}, // button 8  *8
                    {9, 882, 990}, // button 9  *9
                    {10, 17, 19}}; // button 10  *A
,..

First of all, your data structure is wasting a lot of RAM. One int per button is more than enough, you won't need more than the 'middle value' of analogRead() that one button creates. But the button list has to be sorted in ascending or descending order, to save most RAM.
You declare 'int Button[21][3]' = 213sizeof(int) = 2132 = 126 bytes.
That's much too much RAM wasting!

Perhaps you want to save valuable RAM and use a sorted list like that:

[code]
int Button[] = {
   18,  // button 1
   46,  // button 2
   76,  // button 3
 108,  // button 4
 190,  // button 5 
 306,  // button 6
 607,  // button 7
 736,  // button 8
 837,  // button 9
 886,  // button 10
}

OK?

BTW: Do you see how distorted the value range is?
You have three buttons in the range 0...99, two buttons in the range 100...199, but for example not a single button in the range 200...299 or 400...499, 500...599 or 900...1023. But that has to do with your resistor ladder values and is another thing.

Before stepping deep into code and just to affirm your knowledge:

Is it clear to you, that this kind of button readings is limited to detect one button pressed on one analog pin? And that you cannot detect multiple buttons pressed at the same time on one analog pin?

So the handling of the buttons on the A0 input and its 10 buttons has to be:
One button pressed ==> one button detected ==> all buttons released
Another button pressed ==> another button detected ==> all buttons released
No multiple keypresses may appear.
Or if multiple keypresses take place, a wrong button pressed will be detected.
This is clear to you, isn't it?

The same with the other 10 buttons on the A1 pin.
Only one pin at a time can be detected. OK that way?

Just to make it clear...

Old code
int analogpin = 1; // analog pin to read the buttons
int analogpin = 2; // analog pin to read the buttons. // 2 definitions for the same variable = unhappy compiler

New code
int analogpin_bank1 = 1; // analog pin to read the buttons
int analogpin_bank2 = 2; // analog pin to read the buttons. // 2 places so 2 names

Your program now has to look at both

Hi, thank you all for your answers.

I have to mention, that I found this: https://sites.google.com/site/mtthacks/
and that is his work and code I´m trying to use and learn from.

Grumpy_Mike:

Using the same resistor value is not going to improve stability.
This seems to be at odds with the graphic comment in your code.

Well, I read it in a tuturial - with of course, I can´t find again.
About the graphic, something must have gone wrong, when I inserted the code.

You are again trying to redefine a variable within the same scope. Not only can you not do this in the C language, it makes no sense at all.

I know. Thats why I´m asking you guys, how to do it?

Finally the Action function is just stupid. You should never write code like this. You should use an array to define the Boolean states of your switches and not use a case statement with as many cases as you have buttons. This is just fundamental programming and I would suggest that at the moment you are way out of your depth trying to do this project. I suggest you take more time to learn about code first.

You are right about my basic programming skills! But I´m trying to getting started, using this little project. Maybe not the best way to do it...

jurs:

OK, so if I'd make a guess, the values in your array represent the minimum and maximum 'analogRead()' readings when pressing a single button, right?

Yes, thats correct.

First of all, your data structure is wasting a lot of RAM. One int per button is more than enough, you won't need more than the 'middle value' of analogRead() that one button creates. But the button list has to be sorted in ascending or descending order, to save most RAM.
You declare 'int Button[21][3]' = 213sizeof(int) = 2132 = 126 bytes.
That's much too much RAM wasting!

I confess my ignorance into basic programming. My plan at first, is to make it work, and then optimize the code afterwards. But of course, I´m appreciating all the comments I can get!

Before stepping deep into code and just to affirm your knowledge:

Is it clear to you, that this kind of button readings is limited to detect one button pressed on one analog pin? And that you cannot detect multiple buttons pressed at the same time on one analog pin?

So the handling of the buttons on the A0 input and its 10 buttons has to be:
One button pressed ==> one button detected ==> all buttons released
Another button pressed ==> another button detected ==> all buttons released
No multiple keypresses may appear.
Or if multiple keypresses take place, a wrong button pressed will be detected.
This is clear to you, isn't it?

The same with the other 10 buttons on the A1 pin.
Only one pin at a time can be detected. OK that way?

Yes, I´m aware about that.

rcorr:

Just to make it clear...

Old code
int analogpin = 1; // analog pin to read the buttons
int analogpin = 2; // analog pin to read the buttons. // 2 definitions for the same variable = unhappy compiler

New code
int analogpin_bank1 = 1; // analog pin to read the buttons
int analogpin_bank2 = 2; // analog pin to read the buttons. // 2 places so 2 names

Your program now has to look at both

Thank you for this! That is something like this, I think I´m looking for.

I have done what you describe and the compiling went good, but I think I´m still missing to figure out how to separate the two analog inputs. It only works on one input. When I press button 1, it send out keystrokes for button 1 (A1) and 11 (A2).
I know, that every code before void setup and void loop is common for the setup and loop. So I think it maybe it could be something about that, I´m doing wrong or missing?

Thanks

The way this forum works is you post code and we suggest corrections. If you have made those and are still having trouble post your latest code again and say what your trouble is.

Sorry, the code was on another computer.
Here it is:

/*


        Analog pin 1
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
          btn1     btn2     btn3    btn4    btn5   btn6    btn7    btn8     btn9    btn10
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K      33K     47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V

        Analog pin 2
           |
Ground--1K--|--------|--------|-------|-------|------|-------|-------|--------|-------|----
           |        |        |       |       |      |       |       |        |       |
       btn11  btn12  btn13 btn14  btn15 btn16 btn17 btn18 btn19 btn20
           |        |        |       |       |      |       |       |        |       |
        220 Ohm  390 Ohm  680 Ohm   2.2K    4.3K   8.2K    12K     20K      33K     47K
           |--------|--------|-------|-------|------|-------|-------|--------|-------|------ +5V
           


*/

int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for assigning the buttons and there high and low values


// int analogpin_bank0 = A0; //analog pin to read the potentiometer
int analogpin_bank1 = A1; //analog pin to read the button
int analogpin_bank2 = A2; //analog pin to read the button



int Buttonanalogpin_bank1[21] [3] = {
                    {1, 834, 840}, // button 1  *1
                    {2, 734, 738}, // button 2  *2
                    {3, 604, 611}, // button 3  *3
                    {4, 314, 319}, // button 4  *4
                    {5, 189, 191}, // button 5  *5
                    {6, 107, 109}, // button 6  *6
                    {7, 75, 77}, // button 7  *7
                    {8, 45, 47}, // button 8  *8
                    {9, 882, 990}, // button 9  *9
                    {10, 17, 19}}; // button 10  *A


int Buttonanalogpin_bank2[21] [3] = {
                    {11, 834, 840}, // button 11  *B
                    {12, 734, 738}, // button 12  *C
                    {13, 604, 611}, // button 13  *D
                    {14, 314, 319}, // button 14  *E
                    {15, 189, 191}, // button 15  *F
                    {16, 107, 109}, // button 16  *G
                    {17, 75, 77}, // button 17  *H
                    {18, 45, 47}, // button 18  *I
                    {19, 882, 990}, // button 19  *J
                    {20, 17, 19}}; // button 20  *K


int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int ButtonVal;



boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
boolean b4 = false;
boolean b5 = false;
boolean b6 = false;
boolean b7 = false;
boolean b8 = false;
boolean b9 = false;
boolean b10 = false;
boolean b11 = false;
boolean b12 = false;
boolean b13 = false;
boolean b14 = false;
boolean b15 = false;
boolean b16 = false;
boolean b17 = false;
boolean b18 = false;
boolean b19 = false;
boolean b20 = false;

long randNumber;

void setup()
{
  Keyboard.begin();
 
 
 randomSeed(analogRead(1));
 randNumber = random(0,2);


}

void loop()
{
  static float in = 4.712;
  float out;
   // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin_bank1);
    if(ButtonVal == current_state && counter >0)
    {
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }
 
 {
  static float in = 4.712;
  float out;
   // If we have gone on to the next millisecond
  if (millis() != time)
 {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin_bank2);
    if(ButtonVal == current_state && counter >0)
    {
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }
  
}
}
void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 21; i++)
  {
     // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Buttonanalogpin_bank1[i][j] && ButtonVal <= Buttonanalogpin_bank1[i][j+1])
    {
      // stores the button number to a variable
      label = Buttonanalogpin_bank1[i][0];
      Action();     
    }
    

    if(ButtonVal >= Buttonanalogpin_bank2[i][j] && ButtonVal <= Buttonanalogpin_bank2[i][j+1])
    {
      // stores the button number to a variable
      label = Buttonanalogpin_bank2[i][0];
      Action();     
    }
   }
}

void Action()
{
  if(label == 1)
  {
   b1=true;
   b2=false;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   b11=false;
   b12=false;
   b13=false;
   b14=false;
   b15=false;
   b16=false;
   b17=false;
   b18=false;
   b19=false;
   b20=false;
   Keyboard.print('1');
   }
  if(label == 2)
  {
   b1=false;
   b2=true;
   b3=false;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   b11=false;
   b12=false;
   b13=false;
   b14=false;
   b15=false;
   b16=false;
   b17=false;
   b18=false;
   b19=false;
   b20=false;
   Keyboard.print('2');
  }
  if(label == 3)
  {
   b1=false;
   b2=false;
   b3=true;
   b4=false;
   b5=false;
   b6=false;
   b7=false;
   b8=false;
   b9=false;
   b10=false;
   b11=false;
   b12=false;
   b13=false;
   b14=false;
   b15=false;
   b16=false;
   b17=false;
   b18=false;
   b19=false;
   b20=false;
   Keyboard.print('3');
  }

// ect. ect. Left out due to maximum characters




}

The whole thing is screwed and you have not been paying attention to what we have been saying.

You think the way to read two banks is to duplicate the whole code from reading one bank. Well coding doesn't work like that you have to apply a little thinking.

You are still having two variables with the same name and yet you want then to do different things. Different variables should have different names.

In the two duplicated half's of the code you are still calling the same function ButtonCheck(). You need to expand one half of that code to cope with two banks not duplicate the code.

You do this by looking at the loop index and reading one analogue input if the numbers are below half way the other if it is above. So that is an if ... else ... structure.

The same with your button check function.

Well, I have totally lost my confidence because of this project and my skills for coding. :cry:
But anyway, I decided to start all over.

I found a 4X4 matrix keypad on Youtube, and I have made my own 4X4 matrix keypad, using my heavy duty foot switdhes. Please see the attatched picture for schematic.

After compiling the code, I noticed this:
Global variables use 193 bytes (7%) of dynamic memory, leaving 2,367 bytes for local variables.
Maximum is 2,560 bytes

Is that something I should be worried about and what´s the reason for this?

Perhaps this:

ret=15-(log((analogRead(A0)-183.9)/58.24)/0.1623)+0.5;

And I also noticed, that it sometimes don´t send the expected character. Don´t know if it is the code or my homemade keypad, there is causing this. - any suggestions would be very much appreciated.

I have to mention, that I´m planning to add an LED matrix (John suggested Charliepkexing), just to show which button there have been pushed.

What do you think about it?

// Reading a 4x4 matrix keypad with a single pin

String keys="123A456B789C*0#DQZE";
int key;
boolean key_lockout=false;


void setup()
{
//   Serial.begin(9600); 
    Keyboard.begin();
}

void loop()
{
  key=getKeypad();
  if(key!=-1)
{
    Keyboard.print(keys[key]);
    delay(250);(
 }
}

int getKeypad()
{
  int ret=-1;
  boolean reset_lockout=false;
  if(analogRead(A0)==0)
    key_lockout=false;
  else if(!key_lockout){
    delay(20);
    ret=15-(log((analogRead(A0)-183.9)/58.24)/0.1623)+0.5;
    key_lockout=true;
  }
  return ret;
}

Reading a 4x4 matrix keypad with a single pin.PNG

If you have made those and are still having trouble post your latest code again and say what your trouble is.