Take 3 pin ping sensor
The keyboard functions enable 32u4 or SAMD micro based boards to send keystrokes to an attached computer through their micro’s native USB port.
Note: Not every possible ASCII character, particularly the non-printing ones, can be sent with the Keyboard library.
The library supports the use of modifier keys. Modifier keys change the behavior of another key when pressed simultaneously. See here for additional information on supported keys and their use.
Notes and Warnings
These core libraries allow the 32u4 and SAMD based boards (Leonardo, Esplora, Zero, Due and MKR Family) to appear as a native Mouse and/or Keyboard to a connected computer.
A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move()
and Keyboard.print()
will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.
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.

Connect signal pin to 7th pin on Arduino.
Runt the following Code
#include <Keyboard.h>
/*
Ping))) Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the distance
to the closest object in range. To do this, it sends a pulse to the sensor to
initiate a reading, then listens for a pulse to return. The length of the
returning pulse is proportional to the distance of the object from the sensor.
The circuit:
- +V connection of the PING))) attached to +5V
- GND connection of the PING))) attached to ground
- SIG connection of the PING))) attached to digital pin 7
created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Ping
*/
const int pingPin = 7;
int mode=0;
boolean flag =true;
int rem=2;
void setup() {
// initialize serial communication:
Keyboard.begin();
//Keyboard.print('V');
Serial.begin(9600);
}
void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;
String alph[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
/** Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();**/
if(cm>26*rem){
flag=true;
}
if(cm<=26*rem && flag){
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);n
Serial.print("cm, ");
Serial.print(duration);
Serial.print(" duration, ");
Serial.print((cm/rem)-1);
Serial.print(" th of alpha");
Serial.println();
// Keyboard.print(getCharacterValInch(inches));
Keyboard.print(alph[(cm/rem)-1]);
flag=false;
//if(cm<=10){
//
//if(mode>=5){
// mode=0;
//}else{
// ++mode;
//}
//}else if(cm<=12 && cm>10){
//
//Keyboard.press(KEY_BACKSPACE);
//
//}else if(cm<=14 && cm>12){
//
//Keyboard.press(KEY_RETURN);
//}
if(mode==0){
//Keyboard.print(getCharacterVal(cm));
}
}
delay(100);
}
char getCharacterValInch(long inch) {
if(inch == 3){
return 'a';
}else if(inch == 4 ){
return 'b';
}else if(inch == 5){
return 'c';
}else if(inch == 6){
return 'd';
}else if(inch == 7){
return 'e';
}else if(inch == 8){
return 'f';
}else if(inch == 9){
return 'g';
}else if(inch == 10){
return 'h';
}else if(inch == 11){
return 'i';
}else if(inch == 12){
return 'j';
}else if(inch == 13){
return 'k';
}else if(inch == 14){
return 'l';
}else if(inch == 15){
return 'm';
}else if(inch == 16){
return 'n';
}else if(inch == 17){
return 'o';
}else if(inch == 18){
return 'p';
}else if(inch == 19){
return 'q';
}else if(inch == 20){
return 'r';
}else if(inch == 21){
return 's';
}else if(inch == 22){
return 't';
}else if(inch == 23){
return 'u';
}else if(inch == 24){
return 'v';
}else if(inch == 25){
return 'w';
}else if(inch == 26){
return 'x';
}else if(inch == 27){
return 'y';
}else if(inch == 28){
return 'z';
}
}
char getCharacterVal(long cm) {
if(cm>14 && cm<=15){
return 'a';
}else if(cm>15 && cm<=16){
return 'b';
}else if(cm>16 && cm<=17){
return 'c';
}else if(cm>17 && cm<=18){
return 'd';
}else if(cm>18 && cm<=19){
return 'e';
}else if(cm>19 && cm<=20){
return 'f';
}else if(cm>20 && cm<=21){
return 'g';
}else if(cm>21 && cm<=22){
return 'h';
}else if(cm>22 && cm<=23){
return 'i';
}else if(cm>23 && cm<=24){
return 'j';
}else if(cm>24 && cm<=25){
return 'k';
}else if(cm>25 && cm<=26){
return 'l';
}else if(cm>26 && cm<=27){
return 'm';
}else if(cm>27 && cm<=28){
return 'n';
}else if(cm>28 && cm<=29){
return 'o';
}else if(cm>29 && cm<=30){
return 'p';
}else if(cm>30 && cm<=31){
return 'q';
}else if(cm>31 && cm<=32){
return 'r';
}else if(cm>32 && cm<=33){
return 's';
}else if(cm>33 && cm<=34){
return 't';
}else if(cm>34 && cm<=35){
return 'u';
}else if(cm>35 && cm<=36){
return 'v';
}else if(cm>36 && cm<=37){
return 'w';
}else if(cm>37 && cm<=38){
return 'x';
}else if(cm>38 && cm<=39){
return 'y';
}else if(cm>39 && cm<=40){
return 'z';
}
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}