Those two resistors, marked RN4A and RN4B, are used to protect the uart line from conflicts, while also assigning priority to the external header. They act like weak pullups, in that anything before (In this case, left of) the resistors will only affect the line after them, if the lines are not being used.
Anything serial connection plugged into the header, will take priority over the Arduino's usb/serial connection, as they can directly power the lines.
But this will also provide issues during programming, so use an enable pin, or power switch, or simply remove the bluetooth module while programming.
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.
Best Answer
You seem to have a major misconception what "serial" means. Serial means that bits of data are transmitted serially, meaning after each other in time. The number of bits you consider a word in a serial transmission scheme has nothing at all to do with the number of wires and therefore the number of microcontroller pins needed to connect to those wire.
There are various types of serial interfaces, but RS-232 uses a single signal for each direction. The most basic RS-232 interface for bi-directional communication uses only 3 wires: TX, RX, and GND. It is common to use a protocol that logically sends 8 bits at a time, but that has nothing to do with the number of wires since each of those bits are sent in series (hence the name "serial") over the same wire.
If you want to add "hardware flow control" to the basic RS-232 interface, then you need two more wires, RTS and CTS for the bi-directional case. Some microcontrollers come with fancy enough UARTs built in that they can handle RTS and CTS directly in the hardware. On others you have to implement this in the firmware, although that's not all that hard.
Software flow control is done with in-band signalling and uses only the basic two wires, RX and TX (plus GND, but that's implied so we don't normally say that). Usually this is done by sending XOFF to tell the other side to stop sending, then XON to say it's OK to send more stuff. If your data never contains these codes otherwise, then there is nothing more you have to do. Note that XON and XOFF are in the control codes space from 0 to 31, so would not be used if the protocol is text-based. If you are using a binary protocol where all of the codes 0-255 are possible, then you usually use a escape mechanism to add back the codes used by XON and XOFF.
Keep in mind that microcontrollers can't connect directly to RS-232. The convention is for logic-level UARTs, such as included in microcontrollers, to have the RS-232 data inverted on their pins and at the normal logical level. For example, 0 V at the UART would translate to over 5 V on the RS-232 wire, and logic high (like 3.3 or 5 V) at the UART would translate to under -5 V on the RS-232 wire.