Keyboard presses over serial using Arduino

Hello everyone :slight_smile:

I am currently working on controlling multiple tablets with a single keyboard and using Arduinos to get everything to talk. I currently have an Arduino Due acting as the USB host for the keyboard, which is working fine. I then have an Arduino Pro Micro pretending to be the keyboard at the tablet end, which is also working fine. The problem is getting them to talk to each other over serial.

Currently I am using the following code on each board.

This is the Due code. Whenever it receives a key press from the keyboard it sends the keypress info out on the first serial port TX1. It also prints to the serial monitor on the computer. Both appear to be working correctly (computer serial monitor shows correct decimal ascii codes for each key pressed, oscilloscope on TX line shows a byte transmitted whenever a key is pressed.)

#include <address.h>
#include <adk.h>
#include <confdescparser.h>
#include <hid.h>
#include <hidboot.h>
#include <hidusagestr.h>
#include <KeyboardController.h>
#include <MouseController.h>
#include <parsetools.h>
#include <Usb.h>
#include <usb_ch9.h>

//#include <Keyboard.h>

bool LED;

// Initialize USB Controller
USBHost usb;

// Attach Keyboard controller to USB
KeyboardController keyboard(usb);

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

void loop(){
  usb.Task();
}

void keyPressed() {
  Serial.print("Pressed:  ");
  Serial.print(keyboard.getKey());
  Serial1.write(keyboard.getKey());
  Serial.println();
}

Now here is the code that I am using to monitor the incoming received serial messages on the Pro Micro. It is literally just printing whatever is received directly to the serial monitor. Also I made it so that if pin 8 goes high it just sends the message "Button" to the serial monitor so that I can check if it is working correctly.

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

void loop() 
{
    if (digitalRead(8)) {
      Serial.print ("Button");
      Serial.println();
    }
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    Serial.print (inChar);
    Serial.println();
    }
  }

I also am using a CD4051 as a level shifter (it's what I had at hand) since the Due is a 3.3V device and the Pro Micro is a 5V device. The output signal looks to be quite clean, going quickly between 0-5V. Just to be safe I tried the circuit without the level shifter and it still didn't work so I doubt that it is the cause of my problems.

Anyways when I have the two arduinos hooked up, Due Tx1 -> level shifter -> Pro Micro Rx1 I get nothing on the serial monitor, even though my test "button" on the Pro Micro works and when I scope the signal going into Rx on the Pro Micro it appears to look healthy.

Since this is such a simple set up I am guessing that I made a stupid mistake. I am a little new to serial communication on the Arduino so I am basically copy-pasting from example codes. Can one of you arduino wizards out there see what I may have overlooked?

Thank you!

No, the micro is connected via USB to the computer but connected via the serial port to the Due (through the logic level shifter) physically through wires/ a breadboard.

It only has one port but it is for 2 way communication, I am only using the receive port to get data from the Due. While the transmit port is disconnected in the circuit I am basically using it for debugging/ printing data to the computer.

So Due - > Rx, Tx -> serial monitor.

No, the Tx is still connected. I was under the impression that using serial.print just prints directly to the serial monitor, whereas serial.write prints to the Tx pin.

Either way the Tx pin on the Pro Micro is not connected to anything (except the computer internally to the board) so I am not sure how that would be an issue.

Sorry for being dense, I believe that you are not just "giving me the answer" so that I will learn (which I appreciate.)

Does the fact that the computer is connected to the serial port somehow interfere with the way that the arduino normally interfaces with other serial devices? I was under the impression that the computer connection to the arduino is only used for programming/ debugging.

Does this mean that it would be better to handle the inter-arduino communication using one of the other serial ports such as the SPI port?

Hmm, well looking at the arduino pro micro schematic it looks like the USB port is actually not directly connected to the TX and RX pins, but rather the D+ and D- dedicated USB pins as well as power. Im looking through the block diagram on the ATmega16U4-32U4 datasheet to see whether these two ports interact or not.

I assume that it's handled within the Arduino lower level code. All that I know is when I manually activate the command (i.e. by using a button press to print the message "button" to the serial monitor) it works fine, but the chunk of code that passes whatever is received by the serial port Rx pin and sends it to the serial monitor does not work even though it is virtually the same as most of the example code that is posted on the arduino site. Here is another example: Serial.available() - Arduino Reference

I would hope that merely monitoring the traffic coming over the serial port on the computer would not cause the serial port to behave differently than before or else there would be little point to having this monitoring feature.