PS/2 Mouse Problem

Hey guys,

i have a project which use 4 ps/2 mouse.

I use this library.

I tested 1 mouse with the example code and it works.

However when i tried the code again with the same mouse it doesnt transmit anything, so i figured it must be stuck on some part. Looks like the arduino stuck on this part:

void PS2Mouse::write(int data) {
  char i;
  char parity = 1;
  pull_high(_data_pin);
  pull_high(_clock_pin);
  delayMicroseconds(300);
  pull_low(_clock_pin);
  delayMicroseconds(300);
  pull_low(_data_pin);
  delayMicroseconds(10);
  pull_high(_clock_pin); // Start Bit
  while (digitalRead(_clock_pin)) {;} // stuck on this part.
  // clock is low, and we are clear to send data 
  for (i=0; i < 8; i++) {
    if (data & 0x01) {
      pull_high(_data_pin);
    } else {
      pull_low(_data_pin);
    }
    // wait for clock cycle 
    while (!digitalRead(_clock_pin)) {;}
    while (digitalRead(_clock_pin)) {;}
    parity = parity ^ (data & 0x01);
    data = data >> 1;
}

executed when running this:

void PS2Mouse::initialize() {
  pull_high(_clock_pin);
  pull_high(_data_pin);
  delay(20);
  [b]write(0xff); // Send Reset to the mouse[/b]
  read_byte();  // Read ack byte 
  delay(20); // Not sure why this needs the delay
read_byte(); // blank

turns out the mouse clock were never low.However when i change the pull_low code from:

void PS2Mouse::pull_low(int pin) {
 pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);  
}

to

void PS2Mouse::pull_low(int pin) {
 pinMode(pin, INPUT);
  digitalWrite(pin, LOW);  
}

It sorta works, but the output never changes, and the Serial.println() always take 5 seconds where theres no delay() in the loop
i tried with the other 3 mouse but the result always the same. can anyone help?

Heres the example code:

/**
 * Reads X/Y values from a PS/2 mouse connected to an Arduino
 * using the PS2Mouse library available from
 *   http://github.com/kristopher/PS2-Mouse-Arduino/
 * Original by Kristopher Chambers <kristopher.chambers@gmail.com>
 * Updated by Jonathan Oxer <jon@oxer.com.au>
 */

#include <PS2Mouse.h>
#define MOUSE_DATA 5
#define MOUSE_CLOCK 6

PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM);

/**
 * Setup
 */
void setup()
{
  Serial.begin(38400);
  mouse.initialize();
}

/**
 * Main program loop
 */
void loop()
{
  int data[2];
  mouse.report(data);
  Serial.print(data[0]); // Status Byte
  Serial.print(":");
  Serial.print(data[1]); // X Movement Data
  Serial.print(",");
  Serial.print(data[2]); // Y Movement Data
  Serial.println();
}

Can we assume the mouse is connected to the Arduino ground and is powered by 5 volts?

Paul

Yeah VCC connected to +5V, GND to GND, Data to D5, and Clock to D6. I use Arduino Nano with USB as power source.

When pinmode is output:

(i add Serial.println(digitalRead(_clock_pin)) to the part where it stuck for debugging)

When pinmode is input:

it takes 5 seconds each between the output (not the 1) even though theres no delay in loop

the output dont change even when i press the buttons on the mouse (normally the 255 change), and also when i close the serial monitor and open it again, it transmits dozen of 1 first then this: 255:-1,-1 like the arduino got resetted

Look at the way you are using "data" array.

int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory address)

For this reason you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.

Paul

I see, i changed the data array to [3] instead [2] but still the same. Gonna try with other arduino.

It works, the array must have messed up the arduino pretty bad. Thank you for your help.