Ligar um teclado ao arduino
Ligar um teclado ao arduino e acender leds
O esquema é o seguinte:
e aqui a programação:
#include <PS2Keyboard.h>
/* PS2Keyboard library example
PS2Keyboard now requries both pins specified for begin()
keyboard.begin(data_pin, irq_pin);
Valid irq pins:
Arduino Uno: 2, 3
Arduino Due: All pins, except 13 (LED)
Arduino Mega: 2, 3, 18, 19, 20, 21
for more information you can read the original wiki in arduino.cc
at http://arduinogasparpf.blogspot.pt/2017/06/ligar-um-teclado-ao-arduino.html
Like the Original library and example this is under LGPL license.
Modified by Gaspar Pereira on 2017-06-10
Modified by gaspar.10pereira@gmail.com June 2017
*/
#include <PS2Keyboard.h>
const int DataPin = 8;
const int IRQpin = 2;
int led1 = 5; // the pin that the LED is attached to
int b = 0; // how bright the LED is
int f = 5; // how many points to fade the LED by
PS2Keyboard keyboard;
void setup() {
pinMode(5, OUTPUT);
delay(1000);
keyboard.begin(DataPin, IRQpin);
//Selecione o seu teclado - não selecionar nenhum = Teclado US. Select your keyboard. Not selecting = US keyboard
//keyboard.begin(DataPin, IRQpin, PS2Keymap_French);
keyboard.begin(DataPin, IRQpin);
//keyboard.begin(DataPin, IRQpin, PS2Keymap_German );
Serial.begin(9600);
Serial.println("Keyboard Test Gaspar Pereira:");
}
void loop() {
if (keyboard.available()) {
// read the next key
byte c = keyboard.read();
if (c == PS2_ENTER) {
Serial.println();
} else if (c == PS2_TAB) {
Serial.print("[Tab]");
} else if (c == PS2_ESC) {
Serial.print("[ESC]");
} else if (c == PS2_PAGEDOWN) {
Serial.print("[PgDn]");
} else if (c == PS2_PAGEUP) {
Serial.print("[PgUp]");
} else if (c == PS2_LEFTARROW) {
Serial.print("[Esquerda]");
} else if (c == PS2_RIGHTARROW) {
Serial.print("[Direita]");
} else if (c == PS2_UPARROW) {
Serial.print("[Cima]");
} else if (c == PS2_DOWNARROW) {
Serial.print("[Baixo]");
} else if (c == PS2_DELETE) {
Serial.print("[Del]");
//Teste para ligar o LED no Pino 04 após tecla DEL
//Test to turn a LED on when keystroke 'a' or 'A' is pressed
} else if (c == 'a') { // LED will be on for 1 second - LED irá ligar por 1 segundo
Serial.write(c);
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
} else if (c == 'A') {// LED irá piscar 10 vezes - LED will blink 10 times
Serial.write(c);
for (int l=1; l<=10; l=l+1) {
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
}
} else if (c== 225) {// LED irá fazer um efeito Fade e depois apagar - LED will fade until max and turn off
Serial.write(c);
for (int b = 0; b <= 255; b++) {
analogWrite(led1, b);
delay(40);
}
analogWrite(led1, 0);
} else {
// otherwise, just print all normal characters
Serial.write(c);
}
}
}
Para a programação FUNCIONAR têm que baixar a livraria PS2keyboard
O esquema é o seguinte:
e aqui a programação:
#include <PS2Keyboard.h>
/* PS2Keyboard library example
PS2Keyboard now requries both pins specified for begin()
keyboard.begin(data_pin, irq_pin);
Valid irq pins:
Arduino Uno: 2, 3
Arduino Due: All pins, except 13 (LED)
Arduino Mega: 2, 3, 18, 19, 20, 21
for more information you can read the original wiki in arduino.cc
at http://arduinogasparpf.blogspot.pt/2017/06/ligar-um-teclado-ao-arduino.html
Like the Original library and example this is under LGPL license.
Modified by Gaspar Pereira on 2017-06-10
Modified by gaspar.10pereira@gmail.com June 2017
*/
#include <PS2Keyboard.h>
const int DataPin = 8;
const int IRQpin = 2;
int led1 = 5; // the pin that the LED is attached to
int b = 0; // how bright the LED is
int f = 5; // how many points to fade the LED by
PS2Keyboard keyboard;
void setup() {
pinMode(5, OUTPUT);
delay(1000);
keyboard.begin(DataPin, IRQpin);
//Selecione o seu teclado - não selecionar nenhum = Teclado US. Select your keyboard. Not selecting = US keyboard
//keyboard.begin(DataPin, IRQpin, PS2Keymap_French);
keyboard.begin(DataPin, IRQpin);
//keyboard.begin(DataPin, IRQpin, PS2Keymap_German );
Serial.begin(9600);
Serial.println("Keyboard Test Gaspar Pereira:");
}
void loop() {
if (keyboard.available()) {
// read the next key
byte c = keyboard.read();
if (c == PS2_ENTER) {
Serial.println();
} else if (c == PS2_TAB) {
Serial.print("[Tab]");
} else if (c == PS2_ESC) {
Serial.print("[ESC]");
} else if (c == PS2_PAGEDOWN) {
Serial.print("[PgDn]");
} else if (c == PS2_PAGEUP) {
Serial.print("[PgUp]");
} else if (c == PS2_LEFTARROW) {
Serial.print("[Esquerda]");
} else if (c == PS2_RIGHTARROW) {
Serial.print("[Direita]");
} else if (c == PS2_UPARROW) {
Serial.print("[Cima]");
} else if (c == PS2_DOWNARROW) {
Serial.print("[Baixo]");
} else if (c == PS2_DELETE) {
Serial.print("[Del]");
//Teste para ligar o LED no Pino 04 após tecla DEL
//Test to turn a LED on when keystroke 'a' or 'A' is pressed
} else if (c == 'a') { // LED will be on for 1 second - LED irá ligar por 1 segundo
Serial.write(c);
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
} else if (c == 'A') {// LED irá piscar 10 vezes - LED will blink 10 times
Serial.write(c);
for (int l=1; l<=10; l=l+1) {
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);
}
} else if (c== 225) {// LED irá fazer um efeito Fade e depois apagar - LED will fade until max and turn off
Serial.write(c);
for (int b = 0; b <= 255; b++) {
analogWrite(led1, b);
delay(40);
}
analogWrite(led1, 0);
} else {
// otherwise, just print all normal characters
Serial.write(c);
}
}
}
Comentários
Enviar um comentário