Electronic – arduino – Can the Arduino Leonardo act as a keyboard, mouse, and serial port at the same time

arduinoserialuartusb

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.

Best Answer

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.