Arduino Nano
Project
Goal: Create a physical interface to send keypresses to PC. So pressing a button labelled with 1 simulates a keypress "1" on PC.
The way it works is:
-
AnalogPin
A1
is wired to button labelled "1" -
Arduino sets pin read mode to
INPUT_PULLUP
-
When push button is pressed, Arduino gets a LOW signal on that pin
-
That pin is programmed to wirte to Serial buffer
kb:1
-
kb
is a prefix, meaning keyboard -
:
is just a delimiter -
1
after the delimiter is the actual payload, ie data determining what button to simulate
-
-
Serial2Keypress is listening to Serial port
-
it reads
kb:1
and understands that the key needing to be pressed is 1 -
Serial2Keypress simulates 1 keypress
Hardware List
-
Arduino Nanao
-
ATmega 328p microcontroller
-
-
Some breadboards and various length jumper wires
-
Cardboard and double sided tape
Arduino code
< Click here to see full code >
// Analog pins
#define APIN_1 A1
#define APIN_2 A2
#define APIN_3 A3
#define APIN_4 A4
#define APIN_5 A5
// Digital pins
#define DPIN_2 2
#define DPIN_3 3
#define DPIN_4 4
#define DPIN_5 5
#define DPIN_6 6
#define DPIN_7 7
#define DPIN_8 8
#define DPIN_9 9
#define DPIN_10 10
#define DPIN_11 11
#define DPIN_12 12
const int writeDelay = 200;
const int amountOfPins = 16;
typedef struct {
uint8_t pinId;
char *keypress;
} Pin;
Pin pins[amountOfPins] = {
{APIN_1, "1"},
{APIN_2, "2"},
{APIN_3, "3"},
{APIN_4, "4"},
{APIN_5, "5"},
{DPIN_2, "6"},
{DPIN_3, "7"},
{DPIN_4, "8"},
{DPIN_5, "9"},
{DPIN_6, "0"},
{DPIN_7, "a"},
{DPIN_8, "b"},
{DPIN_9, "c"},
{DPIN_10, "d"},
{DPIN_11, "e"},
{DPIN_12, "f"},
};
char serialBuf[30];
const char *prefix = "kb:";
const char *newLineReturn = "\n\r";
// NANO EXCEPTION 1
// A6 and A7 can only be read as analog inputs
// NANO EXCEPTION 2
// D13 needs external resistor, because it's connected to
// onboard LED/cannot use INPUT_PULLUP
void setup() {
Serial.begin(9600); // Setup Serial communication
for (int i = 0, j = amountOfPins; i < j; i++)
{
pinMode(pins[i].pinId, INPUT_PULLUP);
}
}
void loop() {
for (int i = 0, j = amountOfPins; i < j; i++)
{
if (digitalRead(pins[i].pinId) == LOW) {
strcpy(serialBuf, prefix);
strcat(serialBuf, pins[i].keypress);
strcat(serialBuf, newLineReturn);
Serial.write(serialBuf);
// clear char buffer
strcpy(serialBuf, "");
delay(writeDelay);
break;
}
}
}