Go Down

Topic: PS/2 Control keyboard + Arduino (Read 558 times) previous topic - next topic

Greetings! I am using the PS/2 library found here:

http://playground.arduino.cc/Main/PS2Keyboard

Everything hardware wise works great. I cannot for the life of me figure out how to put the incoming key strokes, specifically for letters to play well with conditional statements.

here is my code attempt..its essentially the same  as the example, slightly modified.

Code: [Select]
/*  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
     Teensy 2.0:   All pins, except 13 (LED)
     Teensy 2.0:   5, 6, 7, 8
     Teensy 1.0:   0, 1, 2, 3, 4, 6, 7, 16
     Teensy++ 2.0: 0, 1, 2, 3, 18, 19, 36, 37
     Teensy++ 1.0: 0, 1, 2, 3, 18, 19, 36, 37
     Sanguino:     2, 10, 11
 
  for more information you can read the original wiki in arduino.cc
  at http://www.arduino.cc/playground/Main/PS2Keyboard
  or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
 
  Like the Original library and example this is under LGPL license.
 
  Modified by Cuninganreset@gmail.com on 2010-03-22
  Modified by Paul Stoffregen <paul@pjrc.com> June 2010
*/
   
#include <PS2Keyboard.h>

#include "binary.h"
typedef uint8_t boolean;
typedef uint8_t byte;


const int DataPin = 8;
const int IRQpin =  3; //MUST BE PINS 2 OR 3 IF USING ARDUINO UNO

PS2Keyboard keyboard;
byte a;
int values = 0;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(115200);
  Serial.println("Keyboard Test:");
}

void loop() {
 
  if (keyboard.available()) {
   
   
    // read the next key
    char c = keyboard.read();
    Serial.print(c);
    // check for some of the special keys
    if (c == PS2_ENTER) {
      Serial.println();
    } else if (c == PS2_TAB) {
      Serial.print("[Tab]");

    } else if (c == PS2_PAGEDOWN) {
      Serial.print("[PgDn]");
    } else if (c == PS2_PAGEUP) {
      Serial.print("[PgUp]");
    } else if (c == PS2_LEFTARROW) {
      Serial.print("[Left]");
    } else if (c == PS2_RIGHTARROW) {
      Serial.print("[Right]");
    } else if (c == PS2_UPARROW) {
      Serial.print("[Up]");
    } else if (c == PS2_DOWNARROW) {
      Serial.print("[Down]");
    } else if (c == PS2_DELETE) {
      Serial.print("[Del]");
//    } else {

}


   

  }     // otherwise, just print all normal characters
     
   
 

while(Serial.available()){
 
values = Serial.read();
 
 
  if (values=='a' ){ //
    Serial.println ("entered A");
   
}

}
}


Everything compiles, but Im not sure why this chunk below does not show the print message:

while(Serial.available()){
 
values = Serial.read();
 
 
  if (values=='a' ){ //
    Serial.println ("entered A");



tobyb121

Have you sent an 'a' from the serial monitor? What are you expecting this code to do, it is a bit unclear and makes it much easier to help if you can explain clearly what you want to happen.
Also it would be better if you used a switch instead of all of those if statements (even if just to make your code more readable):
Code: [Select]

switch(c){
   case PS2_ENTER:
      Serial.println();
      break;
   case PS2_TAB:
      Serial.print("[TAB]");
      break;
   .
   .
   .
   defult:
      Serial.print(c);
      break;
}

PaulS

Code: [Select]
values = Serial.read();
Why is values plural, when Serial.read() returns ONE byte?

Why did you post code without using Tools + Auto Format to fix the horrid indenting?

So, what i want the code to do is when an 'a' is pressed, do this, when 'b' is pressed, do that.

the letters are printing in the serial monitor when struck, but I am having a hard time nesting the read values into conditions.

PaulS

Quote
So, what i want the code to do is when an 'a' is pressed, do this, when 'b' is pressed, do that.

What are you expecting to read from the serial port? The purpose of the sketch, as indicated by the thread title, is to read from the PS/2 keyboard.

Yes, with what seems to be class specific functions. I am lacking in knowing the basic class command for numeric/letter

tobyb121

Serial.read() reads data sent over the serial port to the Arduino from the pc. If you haven't sent any characters from your pc then what do you expect it to do? Have you tried this:
Code: [Select]
char c = keyboard.read();
switch(c){
   case 'a':
      Serial.print("Pressed A");
   break;
   case 'b':
      Serial.print("Pressed B");
   break;
   .
   .
   .
   case PS2_ENTER:
      Serial.println();
      break;
   case PS2_TAB:
      Serial.print("[TAB]");
      break;
   .
   .
   .
   defult:
      Serial.print(c);
      break;
}

Go Up