I am developing a keyboard using the Arduino Keyboard library.
How to test output using Serial.print()?
Quote from these two pages:
When using the Mouse or Keyboard library, it may be best to test your output first using Serial.print(). This way, you can be sure you know what values are being reported. Refer to the Mouse and Keyboard examples for some ways to handle this.
I could not find the examples. Where are these examples located?
I agree this is an incorrect statement. If you want an example of how to use Serial.print(), you only need to check the reference page for Serial:
I think what they intended by "Refer to the Mouse and Keyboard examples for some ways to handle this" is that those examples show you how to use a digital input to enable the Mouse/Keyboard emulation. The reason this is recommended is because if you have a bug in your program that makes it just continually typing on the keyboard or moving/clicking the mouse then it can be difficult to upload a fixed sketch to your Arduino board since every time you plug it into your computer it just takes over. Whereas if you have that code that requires an input pin to be pulled low before the emulation will start then you can easily recover from those situations (unless your input pin code also has a bug).
Could you please show me "code that requires an input pin to be pulled low before the emulation will start"?
I don't understand how a pin pulled low is useful. If I see example code maybe I would understand it and be able to use it.
(I assume it only takes one or two lines of code)
Where would the code fit in this simplified keyboard sketch?:
void setup()
{
}
void loop()
{
//This bug continually types on the keyboard,
//so it is difficult to upload a fixed sketch,
//because every time I plug in my Arduino board it just takes over.
Keyboard.press(KEY_A);
Keyboard.release(KEY_A);
}
#include <Keyboard.h>
// use this option for OSX:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
// char ctrlKey = KEY_LEFT_CTRL;
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
while (digitalRead(2) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
delay(1000);
// new document:
Keyboard.press(ctrlKey);
Keyboard.press('n');
delay(100);
Keyboard.releaseAll();
// wait for new window to open:
delay(1000);
}
Do you see how pin 2 is used to control this code?
So with your sketch it would look like this:
void setup()
{
pinMode(2, INPUT_PULLUP);
}
void loop()
{
while (digitalRead(2) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
Keyboard.press(KEY_A);
Keyboard.release(KEY_A);
}
I will usually take a more simple approach:
void setup()
{
// wait a little while before starting any keyboard emulation to allow easy recovery from a bug
// comment out this line once the program has been tested
delay(8000);
}
void loop()
{
Keyboard.press(KEY_A);
Keyboard.release(KEY_A);
}
You only need a delay that's long enough to plug in the board and start the upload before the keyboard/mouse starts going wild. This is not quite so safe as the input pin thing but I have an ISP programmer and a Pro Micro programming jig right next to my desk so I can always just spend 30 seconds doing a Burn Bootloader to erase the problematic program from memory.
This example sketch was tested on Arduino 1.85 and Teensy LC:
/*
If a bug is continually typing or mousing soon as the controller is plugged in,
then it is difficult to upload a fixed sketch.
This SAFETY_PIN feature will stop the keyboard program if the SAFETY_PIN is not grounded.
Usage:
Ground the SAFETY_PIN with a jumper.
If a bug is typing or mousing soon as the controller is plugged in, then:
1. disconnect the SAFETY_PIN jumper
2. upload the fixed sketched
3. reconnect the SAFETY_PIN jumper
If SAFETY_LED is blinking, that means pin SAFETY_PIN is not grounded and keyboard program stopped.
Remove the SAFETY_PIN feature after the sketch is tested.
*/
const int SAFETY_PIN=23;
const int SAFETY_LED=13; //Teensy LC pin 13 is onboard LED
void setup()
{
pinMode(SAFETY_PIN, INPUT_PULLUP);
pinMode(SAFETY_LED, OUTPUT);
}
void loop()
{
//if SAFETY_PIN is not grounded
while (digitalRead(SAFETY_PIN) == HIGH)
{ //stop the keyboard program, and blink LED once per second
digitalWrite(SAFETY_LED, HIGH);
delay(100);
digitalWrite(SAFETY_LED, LOW);
delay(900);
}
//This bug will type continually:
Keyboard.press(KEY_A);
Keyboard.release(KEY_A);
delay(1000);
}