Problem of button

Hello,

I recently find a code that allow me to make a keyboard with an Arduino card.
(here is the video: Tutorial: How to use Arduino Uno as HID | Arduino Keyboard Emulation - YouTube)
But when I press a key keeping a finger on another key, it somehow unselect it.
Like if my finger was no longer on it.
So I was wondering what to change in the code to allow me to use several keys at the same time.

Thanks for your help!

Hello Matis7507
Post your current sketch, well formated, with comments and in so called code tags "</>" and a schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.

I watched the first minute of the video before I got bored. But I saw enough to guess what the problem could be.

You need to put diodes in series with each button. Small ones like 1n4148 would be ideal.

EDIT: no, that's not it, your buttons are not wired as a matrix.

How yeah sorry I've forgotten to send the code ^^'


uint8_t buf[8] = {
  0
};  // Keyboard Report Buffer: 8 bytes

#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3

//#define SERIAL_DEBUG  // for serial debug: remove //
                        // for actual HID: put //

bool currState_W = HIGH;
bool currState_A = HIGH;
bool currState_S = HIGH;
bool currState_D = HIGH;
          
bool prevState_W = HIGH; 
bool prevState_A = HIGH; 
bool prevState_S = HIGH; 
bool prevState_D = HIGH; 

unsigned long prevTime_W = 0;
unsigned long prevTime_A = 0;
unsigned long prevTime_S = 0;
unsigned long prevTime_D = 0;

unsigned long waitTime_W = 50;
unsigned long waitTime_A = 50;
unsigned long waitTime_S = 50;
unsigned long waitTime_D = 50;

void setup() 
{
  Serial.begin(9600);

  pinMode(PIN_W, INPUT_PULLUP);
  pinMode(PIN_A, INPUT_PULLUP);
  pinMode(PIN_S, INPUT_PULLUP);
  pinMode(PIN_D, INPUT_PULLUP);
  
  delay(200);
}

void loop() 
{
  checkButton();
}


void checkButton() {

  bool currRead_W = digitalRead(PIN_W);
  bool currRead_A = digitalRead(PIN_A);
  bool currRead_S = digitalRead(PIN_S);
  bool currRead_D = digitalRead(PIN_D);

  if (currRead_W != prevState_W) {
    prevTime_W = millis();
  }
  if (currRead_A != prevState_A) {
    prevTime_A = millis();
  }
  if (currRead_S != prevState_S) {
    prevTime_S = millis();
  }
  if (currRead_D != prevState_D) {
    prevTime_D = millis();
  }

  if ((millis() - prevTime_W) > waitTime_W) {
    if (currRead_W != currState_W) {
      currState_W = currRead_W;
      if (currState_W == LOW) {
        // Send the code
        buf[2] = 26;    // HID: W key
#ifdef SERIAL_DEBUG
        buf[2] = 'W';     // Serial: W key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey();
      }
    }
  }
  if ((millis() - prevTime_A) > waitTime_A) {
    if (currRead_A != currState_A) {
      currState_A = currRead_A;
      if (currState_A == LOW) {
        // Send the code
        buf[2] = 4;   // HID: A key
#ifdef SERIAL_DEBUG
        buf[2] = 'A';   // Serial: A key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey();
      }
    }
  }
  if ((millis() - prevTime_S) > waitTime_S) {
    if (currRead_S != currState_S) {
      currState_S = currRead_S;
      if (currState_S == LOW) {
        // Send the code
        buf[2] = 22;    // HID: S key
#ifdef SERIAL_DEBUG
        buf[2] = 'S';     // Serial: S key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey();
      }
    }
  }
  if ((millis() - prevTime_D) > waitTime_D) {
    if (currRead_D != currState_D) {
      currState_D = currRead_D;
      if (currState_D == LOW) {
        // Send the code
        buf[2] = 7;   // HID: D key
#ifdef SERIAL_DEBUG        
        buf[2] = 'D';   // Serial: D key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey();
      }
    }
  }

  prevState_W = currRead_W;
  prevState_A = currRead_A;
  prevState_S = currRead_S;
  prevState_D = currRead_D;

}

void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
}

My advice would be: do not worry about it. The code is trash. Find a better code/tutorial.

If you want to use an Arduino as a HID device, Uno is not the way. Get an Arduino Pro Micro. Do not confuse that with Pro Mini.

do you mean computer keyboard where only one button should be pressed at a time or a music keyboard where all "key" can be pressed at the same time?

the code for a computer keyboard won't usually work for a music keyboard

Each time a button is pressed, an 8-byte messages is sent to the HID firmware that has been installed on the 16u2 chip. I'm guessing that message can contain multiple key codes but the sketch doesn't currently use that feature. It only uses byte 2. When it sends a single keycode that means that all the other keycodes are no longer pressed. Similarly, it sets byte 2 to 0 and sends the message to indicate that all keys have been released, whenever any key has been released.

Try this and see if you can get more than one key to be pressed at the same time:

uint8_t buf[8] =
{
  0
};  // Keyboard Report Buffer: 8 bytes

#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3

//#define SERIAL_DEBUG  // for serial debug: remove //
// for actual HID: put //

bool currState_W = HIGH;
bool currState_A = HIGH;
bool currState_S = HIGH;
bool currState_D = HIGH;

bool prevState_W = HIGH;
bool prevState_A = HIGH;
bool prevState_S = HIGH;
bool prevState_D = HIGH;

unsigned long prevTime_W = 0;
unsigned long prevTime_A = 0;
unsigned long prevTime_S = 0;
unsigned long prevTime_D = 0;

unsigned long waitTime_W = 50;
unsigned long waitTime_A = 50;
unsigned long waitTime_S = 50;
unsigned long waitTime_D = 50;

void setup()
{
  Serial.begin(9600);

  pinMode(PIN_W, INPUT_PULLUP);
  pinMode(PIN_A, INPUT_PULLUP);
  pinMode(PIN_S, INPUT_PULLUP);
  pinMode(PIN_D, INPUT_PULLUP);

  delay(200);
}

void loop()
{
  checkButton();
}


void checkButton()
{

  bool currRead_W = digitalRead(PIN_W);
  bool currRead_A = digitalRead(PIN_A);
  bool currRead_S = digitalRead(PIN_S);
  bool currRead_D = digitalRead(PIN_D);

  if (currRead_W != prevState_W)
  {
    prevTime_W = millis();
  }
  if (currRead_A != prevState_A)
  {
    prevTime_A = millis();
  }
  if (currRead_S != prevState_S)
  {
    prevTime_S = millis();
  }
  if (currRead_D != prevState_D)
  {
    prevTime_D = millis();
  }

  if ((millis() - prevTime_W) > waitTime_W)
  {
    if (currRead_W != currState_W)
    {
      currState_W = currRead_W;
      if (currState_W == LOW)
      {
        // Send the code
        buf[2] = 26;    // HID: W key
#ifdef SERIAL_DEBUG
        buf[2] = 'W';     // Serial: W key
#endif
        Serial.write(buf, 8); // Send keypress
      }
      else
      {
        // Remove the code
        buf[2] = 0;    // HID: release W key
        Serial.write(buf, 8); // Send keypress
      }
    }
  }

  
  if ((millis() - prevTime_A) > waitTime_A)
  {
    if (currRead_A != currState_A)
    {
      currState_A = currRead_A;
      if (currState_A == LOW)
      {
        // Send the code
        buf[3] = 4;   // HID: A key
#ifdef SERIAL_DEBUG
        buf[3] = 'A';   // Serial: A key
#endif
        Serial.write(buf, 8); // Send keypress
      }
      else
      {
        // Release the key
        buf[3] = 0;   // HID: A key
        Serial.write(buf, 8); // Send keypress
      }
    }
  }
  
  if ((millis() - prevTime_S) > waitTime_S)
  {
    if (currRead_S != currState_S)
    {
      currState_S = currRead_S;
      if (currState_S == LOW)
      {
        // Send the code
        buf[4] = 22;    // HID: S key
#ifdef SERIAL_DEBUG
        buf[4] = 'S';     // Serial: S key
#endif
        Serial.write(buf, 8); // Send keypress
      }
      else
      {
        // Release the keyboard
        buf[4] = 0;    // HID: S key
        Serial.write(buf, 8); // Send keypress
      }
    }
  }
  
  if ((millis() - prevTime_D) > waitTime_D)
  {
    if (currRead_D != currState_D)
    {
      currState_D = currRead_D;
      if (currState_D == LOW)
      {
        // Send the code
        buf[5] = 7;   // HID: D key
#ifdef SERIAL_DEBUG
        buf[5] = 'D';   // Serial: D key
#endif
        Serial.write(buf, 8); // Send keypress
      }
      else
      {
        // Release the keyboard
        buf[5] = 0;   // HID: D key
        Serial.write(buf, 8); // Send keypress
      }
    }
  }

  prevState_W = currRead_W;
  prevState_A = currRead_A;
  prevState_S = currRead_S;
  prevState_D = currRead_D;
}

void releaseKey()
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key
}

Hey! Thanks for the time you took but the code doesn't work. When I click on one key nothing is happening. Even for 2, 3 ,4 ... btw

I guess I guessed wrong.

I would expect the W button to work if you don't press any of the other buttons. The W button is doing the same thing it was doing before. It is sending 0 0 26 0 0 0 0 0 when pressed and 0 0 0 0 0 0 0 0 when released.

Hi @johnwasser and @Matis7507,

I found this

The 6 bytes (after the first two bytes) represent the keys that are concurrently pressed (up to 6 in this case). If no keys are currently pressed then all 6 bytes should contain 0x00. Each byte is an index between LOGICAL_MINIMUM and LOGICAL_MAXIMUM that is mapped one-for-one to a range of keyboard usages that are (usually) defined by USAGE_MINIMUM and USAGE_MAXIMUM, or a list of individual USAGEs, or a combination of both. If, for example, the A and B keys are pressed then the buffer will contain the index for those two usages (in any order) and the remaining bytes should be 0x00. If A is subsequently released then the buffer should still contain the index for the B usage (with the remaining bytes set to 0x00). If you forget to signal key releases by clearing the buffer to zeros then you will most likely see the last changed key being repeated on your host device (usually a PC). The key repeat delay and rate is purely a host function.

Source:

https://stackoverflow.com/questions/24055687/hid-usb-keyboard

So the easiest solution for 4 keys would be to reserve a separate place for each single key in the 6 byte keyboard buffer like this (which I tested in debug mode):

uint8_t buf[8] = {
  0
};  // Keyboard Report Buffer: 8 bytes

#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3

//#define SERIAL_DEBUG  // for serial debug: remove //
                        // for actual HID: put //

bool currState_W = HIGH;
bool currState_A = HIGH;
bool currState_S = HIGH;
bool currState_D = HIGH;
          
bool prevState_W = HIGH; 
bool prevState_A = HIGH; 
bool prevState_S = HIGH; 
bool prevState_D = HIGH; 

unsigned long prevTime_W = 0;
unsigned long prevTime_A = 0;
unsigned long prevTime_S = 0;
unsigned long prevTime_D = 0;

unsigned long waitTime_W = 50;
unsigned long waitTime_A = 50;
unsigned long waitTime_S = 50;
unsigned long waitTime_D = 50;

void setup() 
{
  Serial.begin(9600);

  pinMode(PIN_W, INPUT_PULLUP);
  pinMode(PIN_A, INPUT_PULLUP);
  pinMode(PIN_S, INPUT_PULLUP);
  pinMode(PIN_D, INPUT_PULLUP);
  
  delay(200);
}

void loop() 
{
  checkButton();
}


void checkButton() {

  bool currRead_W = digitalRead(PIN_W);
  bool currRead_A = digitalRead(PIN_A);
  bool currRead_S = digitalRead(PIN_S);
  bool currRead_D = digitalRead(PIN_D);

  if (currRead_W != prevState_W) {
    prevTime_W = millis();
  }
  if (currRead_A != prevState_A) {
    prevTime_A = millis();
  }
  if (currRead_S != prevState_S) {
    prevTime_S = millis();
  }
  if (currRead_D != prevState_D) {
    prevTime_D = millis();
  }

  if ((millis() - prevTime_W) > waitTime_W) {
    if (currRead_W != currState_W) {
      currState_W = currRead_W;
      if (currState_W == LOW) {
        // Send the code
        buf[2] = 26;    // HID: W key
#ifdef SERIAL_DEBUG
        buf[2] = 'W';     // Serial: W key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey(2);
      }
    }
  }
  if ((millis() - prevTime_A) > waitTime_A) {
    if (currRead_A != currState_A) {
      currState_A = currRead_A;
      if (currState_A == LOW) {
        // Send the code
        buf[3] = 4;   // HID: A key
#ifdef SERIAL_DEBUG
        buf[3] = 'A';   // Serial: A key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey(3);
      }
    }
  }
  if ((millis() - prevTime_S) > waitTime_S) {
    if (currRead_S != currState_S) {
      currState_S = currRead_S;
      if (currState_S == LOW) {
        // Send the code
        buf[4] = 22;    // HID: S key
#ifdef SERIAL_DEBUG
        buf[4] = 'S';     // Serial: S key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey(4);
      }
    }
  }
  if ((millis() - prevTime_D) > waitTime_D) {
    if (currRead_D != currState_D) {
      currState_D = currRead_D;
      if (currState_D == LOW) {
        // Send the code
        buf[5] = 7;   // HID: D key
#ifdef SERIAL_DEBUG        
        buf[5] = 'D';   // Serial: D key
#endif
        Serial.write(buf, 8); // Send keypress
      } else {
        // Release the keyboard
        releaseKey(5);
      }
    }
  }

  prevState_W = currRead_W;
  prevState_A = currRead_A;
  prevState_S = currRead_S;
  prevState_D = currRead_D;

}

void releaseKey(byte index) 
{
  buf[0] = 0;
  buf[index] = 0;
  Serial.write(buf, 8); // Release key  
}

As the sketch uses lots of repetitions I have "cleaned up" a little bit by using a structure and for-loops (also tested in the debug version):

uint8_t buf[8] = {0};  // Keyboard Report Buffer: 8 bytes

#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3

//#define SERIAL_DEBUG  // for serial debug: remove //
// for actual HID: put //

struct keyType {
  char Key;
  byte Pin;
  byte HiD;
  byte currState = HIGH;
  byte prevState = HIGH;
  unsigned long prevTime = 0;
  unsigned long waitTime = 50;
};

const int NoOfKeys = 4;
keyType k[NoOfKeys];

void setup()
{
  Serial.begin(9600);
  k[0].Key = 'W';
  k[0].Pin = A0;
  k[0].HiD = 26;
  k[1].Key = 'A';
  k[1].Pin = A1;
  k[1].HiD =  4;
  k[2].Key = 'S';
  k[2].Pin = A2;
  k[2].HiD = 22;
  k[3].Key = 'D';
  k[3].Pin = A3;
  k[3].HiD =  7;
  for (int i = 0; i < NoOfKeys; i++)
         pinMode(k[i].Pin, INPUT_PULLUP);
    delay(200);
}

void loop()
{
  checkButton();
}


void checkButton() {
  for (int i = 0; i < NoOfKeys;i++) {
    byte currRead =   digitalRead(k[i].Pin);
    if (currRead != k[i].prevState) k[i].prevTime = millis();
    if ((millis() - k[i].prevTime) > k[i].waitTime) {
      if (currRead != k[i].currState) {
        k[i].currState = currRead;
        if (k[i].currState == LOW) {
          // Send the code
          buf[2+i] = k[i].HiD;    // HID: key
#ifdef SERIAL_DEBUG
          buf[2+i] = k[i].Key;     // Serial: key
#endif
          Serial.write(buf, 8); // Send keypress
        } else {
          // Release the keyboard
          buf[0] = 0;
          buf[2+i] = 0;
          Serial.write(buf, 8); // Release key
        }
      }
    }
    k[i].prevState = currRead;
  }
}

Both sketches should do it ... (unless the HID function does ignore data after recognizing a 0 character. In that case the software had to fill the array always from "left to right" which would need some changes to the software).

@Matis7507, could give it a try?

[Edit]: The following sketch is moving the keys "to the left" leaving no 0s in between:

uint8_t buf[8] = {0};  // Keyboard Report Buffer: 8 bytes

#define PIN_W A0
#define PIN_A A1
#define PIN_S A2
#define PIN_D A3

//#define SERIAL_DEBUG  // for serial debug: remove //
// for actual HID: put //

struct keyType {
  char Key;
  byte Pin;
  byte HiD;
  byte currState = HIGH;
  byte prevState = HIGH;
  unsigned long prevTime = 0;
  unsigned long waitTime = 50;
};

const int NoOfKeys = 4;
keyType k[NoOfKeys];

void setup()
{
  Serial.begin(9600);
  k[0].Key = 'W';
  k[0].Pin = A0;
  k[0].HiD = 26;
  k[1].Key = 'A';
  k[1].Pin = A1;
  k[1].HiD =  4;
  k[2].Key = 'S';
  k[2].Pin = A2;
  k[2].HiD = 22;
  k[3].Key = 'D';
  k[3].Pin = A3;
  k[3].HiD =  7;
  for (int i = 0; i < NoOfKeys; i++)
         pinMode(k[i].Pin, INPUT_PULLUP);
    delay(200);
}

void loop()
{
  checkButton();
}

void SendBuf(){
    for (byte i=2;i<6;i++){
#ifdef SERIAL_DEBUG
      if (buf[i] == '-')
#else      
      if (buf[i] == 0)
#endif      
        for (byte j=0;j<6-i;j++)
            buf[i+j] = buf[i+j+1];
      }
    Serial.write(buf, 8); // Send keypress
  }

void checkButton() {
  for (int i = 0; i < NoOfKeys;i++) {
    byte currRead =   digitalRead(k[i].Pin);
    if (currRead != k[i].prevState) k[i].prevTime = millis();
    if ((millis() - k[i].prevTime) > k[i].waitTime) {
      if (currRead != k[i].currState) {
        k[i].currState = currRead;
        if (k[i].currState == LOW) {
          // Send the code
#ifdef SERIAL_DEBUG
          buf[2+i] = k[i].Key;     // Serial: key
#else          
          buf[2+i] = k[i].HiD;    // HID: key
#endif
         SendBuf(); 
        } else {
          // Release the keyboard
#ifdef SERIAL_DEBUG
          buf[2+i] = '-';
#else          
          buf[0] = 0;
          buf[2+i] = 0;
#endif
          SendBuf();
        }
      }
    }
    k[i].prevState = currRead;
  }
}