Here are two Arduinos. A Mega (provides data) and a Leonardo (transmits them to the PC)
I try to use an Arduino Leonardo as an "auto USB keyboard" connected to a PC.
It gets data from another Arduino ( Mega) via serial port. The data are read from a file on SD card located on the Mega. They are containing plain text. The transfer of bytes is OK to the Leonardo because I see them in the Serial monitor.
BUT....
Using the Keyboard.h and writing them out on Leonardo (to act as USB keyboard,) it goes crazy if the transmitted bytes more than 120 characters. In this case I see only some "letter salad"
I open a textfile.
-Byte by byte send its content to the Serial1 of Mega.
-When I reach the end of file I close it.
The content of file appears truly and correctly on Serial1 of Mega, and ALSO in Serial1 of Leonardo. So it works properly.
The Leonardo code is simple(based on an internal example)
#include "Keyboard.h"
void setup()
{
Serial.begin(9600); // for testing purpose
Serial1.begin(9600); //the incoming port. Data from the Mega
Keyboard.begin(); // HUH! THE PROBLEM MAKER!
}
void loop()
{
char incoming; // the character
if(Serial.available()>0) //more than 0 bytes in the serial buffer
{
incoming=Serial1.read(); // reading in
Serial.write(incoming); // the testing port to be monitored.
Keyboard.write(incoming); //writing out as USB keyboard
}
}
This is so simple. If the number of incoming characters is less than 120 then all works fine If more than 120 everything goes mad! In the Leonardo's own serial port ( Serial) everything appears well. Yet in case of more than 120 incoming bytes! This makes me think that maybe the issue is in the Keyboard library....
#include "Keyboard.h"
void setup() {
Serial.begin(9600); // for testing purpose
Serial1.begin(9600); //the incoming port. Data from the Mega
Keyboard.begin(); // HUH! THE PROBLEM MAKER!
}
void loop() {
static char incoming; // the character
static byte counter = 0;
if (Serial1.available() > 0) { //more than 0 bytes in the serial buffer
incoming = Serial1.read(); // reading in
counter++;
Serial.write(incoming); // the testing port to be monitored.
Keyboard.write(incoming); //writing out as USB keyboard
}
if (counter > 119) {
delay(10);
counter = 0;
}
}
I tried to slow down the Mega's transmission exactly as you recommended. In my experiments I added extreme delays- ONE SECOND between bytes. But it caused only slower crazyness- in other words, the letters appearing in the PC formed the SAME letter salad as before- but arrived by 1 second each.
Is any alternative of keyboard.h?
How could I access the developer of the original library?
Or to obtain its source code?
Maybe my problem is a bug of this library?
What kind of text is in the file? The Keyboard library can only handle ASCII characters (0-127), anything beyond that would be translated to raw key codes.
The cause is buffer overrun. The transmitter is quicker as receiver could process bytes. When I inserted 3 ms delay in transmitter side everything became correct!