Help With Arduino Pro Micro

If anyone is experienced with Arduino, and more specifically the Sparkfun Arduino Pro Micro I could use your help. I’m working on some basic keyboard emulation and found some code online that I modified a little for more inputs. Here it is.

#include <Keyboard.h> // This is a “built-in” library no need to install

//---------------------------------------------------------
// Setup
//---------------------------------------------------------

void setup() {
pinMode(3,INPUT_PULLUP); // sets pin 3 to input & pulls it high w/ internal resistor
pinMode(4,INPUT_PULLUP); // sets pin 4 to input & pulls it high w/ internal resistor
pinMode(5,INPUT_PULLUP); // sets pin 5 to input & pulls it high w/ internal resistor

Serial.begin(9600); // begin serial comms for debugging

}

//---------------------------------------------------------
// Loop
//---------------------------------------------------------

void loop() {

Keyboard.begin(); //begin keyboard
if (digitalRead(2) == 0) // if buton 3 is pushed
{
Keyboard.write(‘1’); // send single character "A"
delay(250); // delay so you don’t get 20 A’s
}
else if (digitalRead(3) == 0){
Keyboard.write(‘2’); // send string
delay(250);
}
else if (digitalRead(4) == 0){
Keyboard.write(‘3’); // send string
delay(250);
}
else if (digitalRead(5) == 0){
Keyboard.write(‘4’); // send string
delay(250);
}
else if (digitalRead(6) == 0){
Keyboard.write(‘5’); // send string
delay(250);
}
else if (digitalRead(7) == 0){
Keyboard.write(‘6’); // send string
delay(250);
}
else if (digitalRead(8) == 0){
Keyboard.write(‘7’); // send string
delay(250);
}
else if (digitalRead(9) == 0){
Keyboard.write(‘8’); // send string
delay(250);

}
Keyboard.end(); //stops keybord
}

(You can tell by all the commenting that this isn’t mine)
While this sketch technically does work Im running into an issue where pin 7 is in an endless loop of triggering every quarter second. The button connected to pin 7 and ground is not pressed and is not defective, it works fine when tested with a multimeter.
So what’s wrong? I’m at a loss and if anyone reading this may have a clue on what to do I’d greatly appreciate the input.

Put “[ code ]” and “[/ code ]” (without the spaces) arround the source code. Makes it easier to read.

For pin 7, there is no definition in the code, so it gets some default values (I think). On that note, triggering in the sense of gets set to high?

could try the following:

//add to setup:
pinMode(7,OUTPUT); //sets pin 7 as output
boolean on = false;

//add to loop:
if(on == false){
  digitalWrite(7,HIGH);
  on = true;
} else {
  digitalWrite(7,LOW);
  on = false;
}
delay(250);

Hooking up an LED with 300ohm resistor to 7 and GND should clear it up it it works.

Could also take a look at the documentation: https://www.sparkfun.com/products/12640

why not make things a little easier on yourself and use a switch

switch (digitalRead)
 {
  case 2:
  Keyboard.write(‘1’); // send single character "A"

    break;
  case 3:
  Keyboard.write(‘2’);
    break;
 
}

Not a hundred percent certain, but doing “digitalRead” in conjunction with “if” is optimized by the compiler in some way anyway.

Would this work with a button press as well, it’s worth noting that this is practice for a guitar hero controller conversion, from wii to pc for clone hero. I still need to figure out how to accept a button press from the strum and the colored buttons on the neck of the guitar at the same time. If there’s a better way of doing this I’m completely open to input.

Do a bunch of “digitalRead” to an array and then convert from there.

1 Like

Thanks, this helps a lot.

How many buttons are we talking here?

8 individual buttons but I need several to be pressed at once, like say Im playing a note that uses 3 of the colored note keys, it needs to send a signal for all of those to the computer and it also needs to send a signal for the strum button.

That may be easy. With Youth Underwater Robotics team that I used to mentor, we needed to be able to read multiple controller inputs from the pilot at once.

The best thing to do would be to read all of your input and then submit that to a bitwise function that would then interpret what to submit since you are using digital input. Each button represents a different column (place) on the binary number system.

For example. Button 1 = int 1 , button 2 = int 2, button 3 = 4, etc
If I need to submit button presses for buttons 1, 2, and 3 but not buttons 5-8, then the function would see what the combined button presses equal.
(1)1 +(1)2 + (1)4 + (0)8 + (0)16 + (0)32 + (0)64 + (0)128 = 7

For Button 1 and button 5
(1)1 +(0)2 + (0)4 + (0)8 + (1)16 + (0)32 + (0)64 + (0)128 =17

The combination can only represent as specific set of buttons that have been pressed. 7 can only represent buttons 1, 2, and 3 being pressed. 17 can only represent buttons 1 and 5 being pressed. Math is a powerful thing.

If you need to add more buttons change the base of your number system.

This will allow you to use digitalRead without need to wait 250ms to prevent duplicate keys; in a game like Guitar Hero the delays per key press check can hurt once you add those delays up when pressing multiple keys. You will get a more realistic On/Off scenario as well which is how the game was designed.
Your Function would then use a set of cases (recommended) or if statements to translate the bit sum to button presses and fire them off at once. Then return back to the loop and repeat.
If you do need the delay, you only have to delay after submitting the translated button presses.

It also looks a little cleaner than “if( digitalRead(1) && digitalRead(2) …&& digitalRead(8) ) …”"

Yes, it will work for any single event, for example, I have a arduino checking temp and humidity for plants. the lcd screen shield has buttons on it, and here is a little snip of the code, it sets and reads temperature values the user inputs. It works well. I think much more organised then a bunch of if statements, and maybe even less work in the long run.


  lcd_key = read_LCD_buttons(); 

  switch (lcd_key)               
  {
    case btnRIGHT:
      {
        EEPROM.write(selectmode, tempval);
        break;
      }
  
    case btnLEFT:
      {
        int Eread = EEPROM.read(selectmode);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("TEMPSET  ");
        lcd.print(selectmode);
        lcd.setCursor(0, 1);
        lcd.print(Eread);
        break;
      }
 
    case btnUP:
      {
        tempval++;
        lcd.setCursor(0, 1);
        lcd.clear();
        lcd.print(tempval);
        delay(250);
        break;
      }
  
    case btnDOWN:
      {
        tempval--;
        lcd.setCursor(0, 1);
        lcd.clear();
        lcd.print(tempval);
        delay(250);
        break;
      }