Now a computer is no good, unless you can type something into it. Although it is possible to read a USB keyboard you need additional hardware if you are just using an Arduino UNO. However there is an Arduino library for PS/2 Keyboards. I found it at http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html. You'll need to add it to the Arduino IDE Libraries. Click on Sketch->Import Library->Add Library...
All I needed now was a PS/2 Keyboard as all my keyboards are USB these days. Fortunately they can still be found in plentiful supply at your favourite online stores.
Of course, with s PS/2 keyboard you need a PS/2 socket to plug it in. I had an old motherboard and a hot air gun soon melted the solder to free it from the main board. Now to make it breadboard friendly.
You only need 4 pins. +5V, GND, Clock and Data. See http://en.wikipedia.org/wiki/PS/2_port for details of the pin outs. I used a continuity tester to check which pins underneath matched which ps/2 port inputs and soldered on four jump wires.
Using just the LiquidCrystal library and the PS2Keyboard library, I was able to write all over my 20x4 LCD display.
The PS2Keyboard library defaults to US layout doesnt have the UK layout defined so I might tweak that later.
Heres the code:
#include <LiquidCrystal.h>
#include <PS2Keyboard.h>
LiquidCrystal lcd(10,9,8,7,6,5,4);
PS2Keyboard keyboard;
const int DataPin = 3;
const int IRQpin = 2;
int row=1;
int col=0;
void setup() {
// put your setup code here, to run once:
delay(1000);
keyboard.begin(DataPin, IRQpin);
lcd.begin(20,4);
lcd.print("Keyboard Test");
//lcd.cursor();
lcd.blink();
lcd.setCursor(col,row);
}
void loop() {
// put your main code here, to run repeatedly:
if (keyboard.available()) {
// read the next key
char c = keyboard.read();
// check for some of the special keys
if (c == PS2_ENTER) {
col=0;
row=row+1;
} else if (c == PS2_ESC) {
lcd.clear(); // clear screen if ESC is pressed.
col=0;
row=0;
} else if (c == PS2_LEFTARROW) {
col=col-1;
} else if (c == PS2_RIGHTARROW) {
col=col+1;
} else if (c == PS2_UPARROW) {
row=row-1;
} else if (c == PS2_DOWNARROW) {
row=row+1;
} else if (c == PS2_DELETE) {
col=col-1;
lcd.setCursor(col,row);
c=32;
lcd.print(c);
lcd.setCursor(col,row);
} else {
// otherwise, just print all normal characters
lcd.print(c);
col=col+1;
}
if (col>19) {
col=0;
row=row+1;
}
if (col<0) {
col=19;
row=row-1;
}
if (row>3) row=0;
if (row<0) row=3;
}
lcd.setCursor(col,row);
}
No comments:
Post a Comment