English 中文(简体)
Arduino - Keyboard Serial
  • 时间:2024-10-18

Arduino - Keyboard Serial


Previous Page Next Page  

This example pstens for a byte coming from the serial port. When received, the board sends a keystroke back to the computer. The sent keystroke is one higher than what is received, so if you send an "a" from the serial monitor, you will receive a "b" from the board connected to the computer. A "1" will return a "2" and so on.

Warning − When you use the Keyboard.print() command, the Leonardo, Micro or Due board takes over your computer s keyboard. To ensure you do not lose control of your computer while running a sketch with this function, set up a repable control system before you call Keyboard.print(). This sketch is designed to only send a Keyboard command after the board has received a byte over the serial port.

Components Required

You will need the following components −

    1 × Arduino Leonardo, Micro, or Due board

Procedure

Just connect your board to the computer using USB cable.

Keyboard Serial Breadboard

Sketch

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by cpcking New.

Sketch

Notes − You must include the keypad pbrary in your Arduino pbrary file. Copy and paste the keypad pbrary file inside the file with the name ‘pbraries’ highpghted with yellow color.

Arduino pbrary file

Arduino Code

/*
   Keyboard test
   For the Arduino Leonardo, Micro or Due Reads
      a byte from the serial port, sends a keystroke back. 
   The sent keystroke is one higher than what s received, e.g. if you send a, you get b, send
      A you get B, and so forth.
   The circuit:
   * none
*/

#include "Keyboard.h"

void setup() {
   // open the serial port:
   Serial.begin(9600);
   // initiapze 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();
      // Type the next ASCII value from what you received:
      Keyboard.write(inChar + 1);
   }
}

Code to Note

Once programed, open your serial monitor and send a byte. The board will reply with a keystroke, that is one number higher.

Result

The board will reply with a keystroke that is one number higher on Arduino IDE serial monitor when you send a byte.

Advertisements