Using Pro Micro to send keyboard strokes with a switch

Hi

Sorry if I´m in the wrong forum.
I´m just a newbee in this, so I´m sorry for my ignorance! :confused:

I want to make a simple foot controller to control Guitar Rig 5.

I came across Mat´s Hacks ( Matt's Hacks ), and I will try to copy his great work. (Hope he dont mind)

I have used his code, and made an 4 switch board like this: (Please see the attached image), but without the LED´s.

It just dont work :sob:

I would like to ask Matt him self, but can´t find any way to get in contact with him, so therefore I hope, that some of you guys can help me and see, what I´m doing wrong.

Thanks in advance!
Jens

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, 837, 845},// button 1
                     {2, 730, 738}, // button 2
                     {3, 603, 612}, // button 3
                     {4, 318, 323}, // button 4
                     {7, 896, 900}, // button 1 + button 2
                     {8, 877, 882}, // button 1 + button 3
                     {9, 851, 857}, // button 1 + button 4
                     {10, 816, 822}, // button 2 + button 3
                     {11, 760, 770}}; // button 2 + button 4
    

int analogpin = 0; // 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;

const int bled = 9;
const int yled = 6;
const int gled = 5;
const int rled = 3;

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

long randNumber;

void setup()
{
  Keyboard.begin();
 
  pinMode(bled, OUTPUT);
  pinMode(yled, OUTPUT);
  pinMode(gled, OUTPUT);
  pinMode(rled, OUTPUT);
 
  randomSeed(analogRead(1));
  randNumber = random(0,2);
 
  switch(randNumber){
    case 0:
    digitalWrite(rled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    digitalWrite(yled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(bled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(bled,LOW);
    digitalWrite(yled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    digitalWrite(yled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(bled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(bled,LOW);
    digitalWrite(yled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    digitalWrite(yled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(bled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(bled,LOW);
    digitalWrite(yled,HIGH);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,LOW);
    delay(50);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    delay(50);
    break;
    case 3:
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    digitalWrite(rled,HIGH);
    digitalWrite(gled,HIGH);
    digitalWrite(yled,HIGH);
    digitalWrite(bled,HIGH);
    delay(50);
    digitalWrite(rled,LOW);
    digitalWrite(gled,LOW);
    digitalWrite(yled,LOW);
    digitalWrite(bled,LOW);
    delay(50);
    break;

    }

}

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) * 64 + 64;
  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);
  }
 
}

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;
   Keyboard.print('1');
   }
  if(label == 2)
  {
   b1=false;
   b2=true;
   b3=false;
   b4=false;
   Keyboard.print('2');
  }
  if(label == 3)
  {
   b1=false;
   b2=false;
   b3=true;
   b4=false;
   Keyboard.print('3');
  }
  if(label == 4)
  {
   b1=false;
   b2=false;
   b3=false;
   b4=true;
   Keyboard.print('4');
  }
}

The project page tells you what values of resistors he used but not the order they were wired.

Write a little sketch that displays the analogRead(A0); value every second and write down the values you get for each combination of buttons you want to detect. Sort those numbers in order and make sure all are sufficiently separated to detect individually. If not, you will need to change your resistors.

Thank you for replying, John.

Well, I´m totally new to this, so sorry for any stupid questions.

If I understand you correct. Do you mean, that I shall measure the voltage between GND (on Pro Micro) and the VCC out ( A0) from the switches? And if thats the case, how do I transform the readouts in volt to a value in the programming? But the difference in voltage, when I measure the above, is nearly notthing - If I instead of, measure after the R5 (1K) between GND and switch, I get a much better result.

Hope any of this gives any meaning for you :-[

Thanks

jeor: If I understand you correct. Do you mean, that I shall measure the voltage between GND (on Pro Micro) and the VCC out ( A0) from the switches? And if thats the case, how do I transform the readouts in volt to a value in the programming? But the difference in voltage, when I measure the above, is nearly notthing - If I instead of, measure after the R5 (1K) between GND and switch, I get a much better result.

analogRead(A0) gives you a number proportional to the voltage on the A0 pin. What numbers do you get for each combination of buttons?

johnwasser: analogRead(A0) gives you a number proportional to the voltage on the A0 pin. What numbers do you get for each combination of buttons?

I hope I understand you correctly. I measure 4,490 Volt without any keys pressed and then following: Key 1: 4,416 Key 2: 4,447 Key 3: 4,463 Key 4: 4,478

jeor: I hope I understand you correctly. I measure 4,490 Volt without any keys pressed and then following: Key 1: 4,416 Key 2: 4,447 Key 3: 4,463 Key 4: 4,478

It's probably not a good idea to convert the number to 'voltage'. The Arduino works much better with integers than it does with floating-point numbers. I'm surprised you don't get 0V (0 from analogRead()) when no button is pushed. In general, one resistor goes from A0 to Ground. The example uses 1K but 10K would probably be better. The other resistors each go from A0 to a pushbutton with the other side of all pushbuttons going to +5V. When no button is pushed the A0 pin should be at 0V and give you a nice clear "no button pushed" indication. As various buttons are pushed the different resistors become part of a voltage divider. If a button has a 10K resistor then that button would produce a signal around 512 (10K / 10K+10K * 1024). If the button has a 4K resistor you'd get about 731 (10K / 10K+4K * 1024). That way by using different resistors you can tell which button is pushed. When two (or more) buttons are pushed the rule for parallel resistors is used to figure out the effective resistance. If you didn't need to check for combinations of buttons then one of the buttons wouldn't need a resistor. It would short the pin to +5v and would always read 1023. It would also read 1023 when used in combination with any resistor.

Thank you, John! :slight_smile:

Now it starts to make sense for me.
I made it like this (like showed in the diagram), and now it works, but only with B4. It is now giving me an “1” keystroke.
So I assume, that it must be the values in the code or the value of the resistors, that I will have to look into.

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, 837, 845},// button 1
                     {2, 730, 738}, // button 2
                     {3, 603, 612}, // button 3
                     {4, 318, 323}, // button 4
                     {7, 896, 900}, // button 1 + button 2
                     {8, 877, 882}, // button 1 + button 3
                     {9, 851, 857}, // button 1 + button 4
                     {10, 816, 822}, // button 2 + button 3
                     {11, 760, 770}}; // button 2 + button 4
    

int analogpin = 0; // 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;

Here is a previous topic that goes into some detail on the wiring and other details: http://forum.arduino.cc/index.php?topic=8558.0

Hi john

I will check it out :-)

Thanks for your inputs - it was much appreciated!

/Jens