Problems with PS2Keyboard

I'm not sure if this kind of question is to post here or if is to post on the Electronic section, anyway i'm trying a thing that should be simple, but i can't solve it.
My code is simply an example on how to use PS2Keyboard libraries:

#include <PS2Keyboard.h>

const int DataPin = 1;
const int IRQpin =  3;

PS2Keyboard keyboard;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  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_LINEFEED) {
      Serial.print("[^Enter]");
    } 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("[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]");
    
    // Ctrl + <alphabet key> produces ASCII codes 1-26
    } else if (c < 26) {
      Serial.print("^");
      Serial.print((char)('A' + c - 1));

    } else {
      
      // otherwise, just print all normal characters
      Serial.print(c);
    }
  }else{
    Serial.println("Not available");
  }
}

I opened the wire of the Keyboard, and connected the red one to the 5V pin, the white to the GND pin, the yellow on the digital pin #1 and the green on the digital pin #3

then i runned this example but...
It keep writing is not available, I can't understand what's wrong, i checked the wires with a tester, they are all connected, so maybe is something in the code?
But if it was something in the code it wouldn't even compiled.
Ah, last informations: i'm using an arduino uno, and the keyboard lights turn on when i start the script but a second after they turn off and keep off

const int DataPin = 1;

That pin is one of the hardware serial pins. That pin is NOT available to you since you use Serial.begin().

You can either give up on debugging your code, or you can use another pin.

Thanks for the information, but... It doesen't work anyway :confused:
I changed the data cable to the pin 4, and retryed with something more simple:

#include <PS2Keyboard.h>

const int DataPin = 4;
const int IRQpin =  3;
bool first = true;
PS2Keyboard keyboard;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  if (keyboard.available()) {
    Serial.print("Work");
  }else{
    Serial.print("Not work");
  }
}

void loop() {

}

But it still doesen't work, any other ideas on what could it be?

When you connect power to the circuit and the keyboard, do the LEDs on the keyboard light briefly? If not, the keyboard may be dead. There's a complete PS2 keyboard program in Arduino Project for Amateur Radio, on page 176.

econjack:
do the LEDs on the keyboard light briefly?

Yes, they turn on, and a second after they turn off and stay off

econjack:
There's a complete PS2 keyboard program in Arduino Project for Amateur Radio, on page 176.

I don't have that book, but my example is from here Arduino Playground - HomePage, i'm using the version 2.3

up

It keep writing is not available,

That's what you told it to do.
Your loop is essentially:

void loop() {
  if (keyboard.available()) {
    // read the next key
  } else {
    Serial.println("Not available");
  }

keyboard.available() does NOT "wait" for a keypress; it just tells you whether there is a keypress that you haven't read yet (exactly like serial.available()) So the loop continuously prints "not available" unless you press a key (and after you read the key as well, perhaps obscuring the correct processing of the key.)

Similarly, your shorter test program would only print "working" if you managed to type a key in the short time that is between the keyboard.begin() and keyboard.available() calls.

westfw:
keyboard.available() does NOT "wait" for a keypress; it just tells you whether there is a keypress that you haven't read yet (exactly like serial.available()) So the loop continuously prints "not available" unless you press a key (and after you read the key as well, perhaps obscuring the correct processing of the key.)

Similarly, your shorter test program would only print "working" if you managed to type a key in the short time that is between the keyboard.begin() and keyboard.available() calls.

Fixed it, it should be ok now

void loop() {
  if (keyboard.available()) {
    Serial.println("You have pressed a key");
  } else {
    
  }

But it doesen't write nothing now