1

I am working on a project where I am taking the the key presses from a PS/2 keyboard and passing them to several pro micro boards so I can type the same commands on multiple computers at the same time. I have a master board that is a Leonardo that is connected to a keyboard. Using the TX pin I am connecting to a pro micro and trying to receive keystrokes on the micro and output them to a computer. I can get a the keyboard to work with the Leonardo connected to computer but not the pro micro. What do I have wrong about communication between these devices.(I get maybe every other key correctly on the computer but a also other keys or even function keys.) Code for the "Master":

#include <PS2Keyboard.h>
#include <Keyboard.h>

const int DataPin = 8;
const int IRQpin =  3;

PS2Keyboard keyboard;


void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Keyboard Test:");
}

void loop() {
  if (keyboard.available()) {

    // read the next key
    char c = keyboard.read();

    // check for some of the special keys
    if (c == PS2_ENTER) {
      Serial.println();
    } else if (c == PS2_TAB) {
      Serial.print("[Tab]");
    } else if (c == PS2_ESC) {
      Serial.print("[ESC]");
    } else if (c == PS2_PAGEDOWN) {
      Serial.print("[PgDn]");
    } else if (c == PS2_PAGEUP) {
      Serial.print("[PgUp]");
    } else if (c == PS2_LEFTARROW) {
      Serial.print("[Left]");
    } else if (c == PS2_RIGHTARROW) {
      Serial.print("[Right]");
    } else if (c == PS2_UPARROW) {
      Serial.print("[Up]");
    } else if (c == PS2_DOWNARROW) {
      Serial.print("[Down]");
    } else if (c == PS2_DELETE) {
      Serial.print("[Del]");
    } else if (c == PS2_F1) {
      Serial.print("[F1]"); 
      //Serial1.print("[F1 ]");      
    } else {

      // otherwise, just print all normal characters
      Serial1.print(c);
    }
  }
}

Code for the "Slave":

#include <HID.h>
#include <Keyboard.h>

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

void loop(){
   if (Serial1.available() > 0) {
   // read incoming serial data:
   char c = Serial1.read();
   Keyboard.print(c);
  }
}
  • Could you try to use Serial1.write with a fixed byte. For example remove the Serial1.print(c); and before or after char c = keyboard.read(); a Serial1.write('H'); – Jot Jul 10 '17 at 22:56
  • Interesting, I actually tried Serial1.write but where my keyboard.print(c) was located, but that actually is kind of making sense. I thought I needed to convert the data to a byte instead of a Serial1.print. No changes are necessary on the slave device correct? – landres Jul 11 '17 at 12:47
  • Correct for normal characters, like 'a', 'b', 'A', 'B', '?'. The PS2 library returns those normal ASCII characters, they are transmitted over the serial line and send to the computer. Everything as normal ASCII characters. You need to translate the function-keys and other special keys. Is this good enough for you ? Or do you need to simulated a key being pressed for a long time and also the Alt, Ctrl and media keys as well ? – Jot Jul 11 '17 at 17:32
  • I am going to need to use the alt, Ctrl and function keys as well. I was hoping to get the regular keys working first as it seemed that special or modifier keys might be more difficult. The PS2Keyboard.h file is missing the bits for the function keys but I was able to get those working and I found the if else statements in an example to test the functions keys. What would be the best way to go about this? – landres Jul 11 '17 at 20:54
  • Instead of the if-else-statements you can use a table. However, nothing is wrong with the if-else-statements. Try to make it work with the Arduino Keyboard library. Perhaps you have to detect the pressing down and releasing a key as two seperate events. To create a keyboard that can do everything, you might need github.com/NicoHood/HID/wiki If you are going to use that, start by reading about the three different types of keyboard. Perhaps the keyboard leds can be added as well, but I'm not sure of that. How is the problem going, can you get normal characters in the computers ? – Jot Jul 11 '17 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.