1
\$\begingroup\$

I am doing a project that requires me to control both a mouse/keyboard combination and a serial port to activate it. (I know, it can prevent me from using my computer, etc. but I have precautions already in place.) However, can the Arduino Leonardo be two devices (or technically three) at once without any additional hardware? I know Arduino has example on how to use both the keyboard and mouse at once, but is it possible for the Leonardo to compute the "triathlon of serial communications?"

I know there is one output that goes to the USB and one that can go to the TX/RX, so I could technically buy a serial to USB converter and hook it up for the serial side, but that can get pricey and hog two USB ports instead of one (very valuable on a laptop and a hub is not economical when you add shipping and the price for the UART module, and it is more fun figuring this out.

\$\endgroup\$
3
\$\begingroup\$

It looks like the keyboard and mouse example on the arduino site already has a method for reading the serial port.



/*
  KeyboardAndMouseControl

 Controls the mouse from five pushbuttons on an Arduino Leonardo or Micro.

 Hardware:
 * 5 pushbuttons attached to D2, D3, D4, D5, D6


 The mouse movement is always relative. This sketch reads 
 four pushbuttons, and uses them to set the movement of the mouse.

 WARNING:  When you use the Mouse.move() command, the Arduino takes
 over your mouse!  Make sure you have control before you use the mouse commands.

 created 15 Mar 2012
 modified 27 Mar 2012
 by Tom Igoe

 this code is in the public domain

 */

// set pin numbers for the five buttons:
const int upButton = 2;     
const int downButton = 3;        
const int leftButton = 4;
const int rightButton = 5;
const int mouseButton = 6;

void setup() { // initialize the buttons' inputs:
  pinMode(upButton, INPUT);       
  pinMode(downButton, INPUT);       
  pinMode(leftButton, INPUT);       
  pinMode(rightButton, INPUT);       
  pinMode(mouseButton, INPUT);

  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
  Keyboard.begin();
}

void loop() {

right here:


  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inChar = Serial.read();

    switch (inChar) {   
    case 'u':
      // move mouse up
      Mouse.move(0, -40);
      break; 
    case 'd':
      // move mouse down
      Mouse.move(0, 40);
      break;
    case 'l':
      // move mouse left
      Mouse.move(-40, 0);
      break;
    case 'r':
      // move mouse right
      Mouse.move(40, 0);
      break;
    case 'm':
      // perform mouse left click
      Mouse.click(MOUSE_LEFT);
      break;
    }
  }

  // use the pushbuttons to control the keyboard:
  if (digitalRead(upButton) == HIGH) {
    Keyboard.write('u'); 
  }
  if (digitalRead(downButton) == HIGH) {
    Keyboard.write('d'); 
  }
  if (digitalRead(leftButton) == HIGH) {
    Keyboard.write('l'); 
  }
  if (digitalRead(rightButton) == HIGH) {
    Keyboard.write('r'); 
  }
  if (digitalRead(mouseButton) == HIGH) {
    Keyboard.write('m'); 
  }

}


If you can read the serial port, it seems like you can write to it too. I don't have a leonardo board to test this out myself, but inside of the loop() function you should be able to add something like


Serial.print("hello world");

and verify this while the Mouse and Keyboard are running too.

\$\endgroup\$
2
\$\begingroup\$

Using an Arduino Leonardo and a modified sketch based on the code of the KeyboardSerial reference example I was able to verify that it is possible to use the serial connection over the same USB cable as the HID keyboard simulation.

I used the following code:

#include "Keyboard.h"

void setup() {
  // open the serial port:
  Serial.begin(9600);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // check for incoming serial data:
  if (Serial.available() > 0) {
    // read incoming serial data:
    char inChar = Serial.read();
    Serial.write(inChar);
    // Type the next ASCII value from what you received:
    // Keyboard.write(inChar + 1);
    Keyboard.press(KEY_CAPS_LOCK);
    delay(100);
    Keyboard.releaseAll();
  }
}

When connecting to the COM port of the Leonardo (eg. using PuTTY) I got:

THe cApSlOcK lEd Is tOgGlInG WiTh eVeRy kEy sTrOkE.

Note: The output of the terminal is an echo of the input sent by the Arduino

Serial.write(inChar);

statement.

\$\endgroup\$
1
\$\begingroup\$

It cannot do serial at the same time it is acting as an HID device. I got around this by using a usb-5v serial cable and using the second serial port on the Leonardo (i.e. Serial1.begin(9600) )

\$\endgroup\$
  • \$\begingroup\$ I wonder why the Arduino example code has that... \$\endgroup\$ – Anonymous Penguin May 9 '13 at 23:39
  • \$\begingroup\$ It can. By being a composite usb device with multiple profiles. \$\endgroup\$ – Passerby Feb 12 '17 at 2:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.