I interfaced an old ps2 compatible mouse that I thought might work as a crude velocity sensor for a robot car.
What I now realize is only when the Arduino is connected to serial on the USB does this program work.
The actual setup is very similar to this ( credit jazzy camel )
It works fine connected to USB cable but now I cannot use this on a moving robot car "as-is" .
Perhaps this will never work?
PS: I have not figured out how to attach code yet. Sorry.
#include "PS2Mouse.h"
// use 6 and 5 on MEGA for clk and data
PS2Mouse mymouse(6, 5);
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.print("Setup...");
mymouse.begin();
Serial.println("complete!");
}
int mycounter;
//-------------------------------
void loop() {
uint8_t stat;
int x, y;
mymouse.getPosition(stat, x, y);
mycounter ++;
if ( mycounter >= 100 )
{
mymouse.begin();
mycounter = 0;
}
if ( x != 0 || y != 0)
{
Serial.print(x);
Serial.print("\t");
Serial.println(y);
}
The best thing to get help is post the Schematic along with the picture, not a frizzy thing. Be sure the schematic shows all power and ground connections. How do you know it is not working?
The wiring is very simple..
All connections tested and verified with voltmeter.
I hope this info will suffice.
PS2 mouse side..
wire designed function
Red: Vcc (USB pin 1)
White: Data (USB pin 2)
Green: Clock (USB pin 3)
Black: Gnd (USB pin 4)
Black: Shield (not used)
Arduino side
Mouse Vcc to Arduino Vcc
Mouse Gnd to Arduino Gnd
Mouse Clock to to Arduino Pin 6
Mouse Data to Arduino Pin 5
I'll have to get back later with more info. I am not sure about the electronics now.
It is acting erratic tonight. not turning on at all, then out of nowhere it does turn on.
Not sure what is happening but connections are good. THE clk and data line is high (3.6V)
there is bias voltage on the ps2 sensor leds. You can see the little red (lit) dot on each led.
I'm going to buy a new ps2 mouse and try again, just to be sure it isn't electronics.
#include "PS2Mouse.h"
// use 6 and 5 on MEGA for clk and data
PS2Mouse mymouse(6, 5);
void setup() {
 Serial.begin(9600);
// delay (100);
while (!Serial);
 Serial.print("Setup...");
 mymouse.begin();
Â
 Serial.println("complete!");
}
int mycounter;
//-------------------------------
void loop() {
 uint8_t stat;
 int x, y;
 mymouse.getPosition(stat, x, y);
 mycounter ++;
 if ( mycounter >= 100 )
 {
  mymouse.begin();
  mycounter = 0;
 }
 if ( x != 0 || y != 0)
 {
  Serial.print(x);
  Serial.print("\t");
  Serial.println(y);
 }
 delay(200);
}
Which Arduino (code mentions Mega)? How is it powered when not connected to USB?
If you're using something with native USB (Due, Leonardo, Micro, ProMicro, teensy, ...), this line will block your code till a terminal program (e.g. serial monitor) is opened.
while (!Serial);
There are a few more pitfalls with boards that support native USB, but let's wait for the answer on above questions.
Mega 2560 sorry, although the original asks for Arduino Leonardo Nano clone.
It was working just fine earlier, so I am believing it is electronic problem for now.
This is the original code before I hacked into it. Will have good electronics soon.
/*
USB Pinout (Left to Right, USB symbol up)
4: GND
3: Clk
2: Data
1: Vcc
*/
#include "PS2Mouse.h"
PS2Mouse mouse(6,5);
void setup(){
 Serial.begin(9600);
 while(!Serial);
 Serial.print("Setup...");
 mouse.begin();
 Serial.println("complete!");
}
void loop(){
 uint8_t stat;
 int x,y;
 mouse.getPosition(stat,x,y);
Â
 Serial.print(stat, BIN);
 Serial.print("\tx=");
 Serial.print(x, DEC);
 Serial.print("\ty=");
 Serial.println(y, DEC);
Â
 delay(1000);Â
}
I seem to have stumbled upon a satisfactory work around for my problem!
I have certified this code and wiring works with a PS2 mouse of the old type.
I Bought a PS2 mouse and (taking care to be sure that wiring is correct ) I now have a working crude floor velocity sensor that can be eventually integrated into a robot car.
I will use it as a sensor so that car can be aware it has stalled into an obstacle.
The sensor is working very nicely now, giving me hope this can indeed be used further
/*
PS2 mouse
USB Pinout (Left to Right, USB symbol up)
__________PS2 connector________________jumpered________MEGA pins_______________________
4: GND PS2 low wht my blk GND
3: Clk PS2 grn my yel pin 6
2: Data PS2 blue my wht pin 5
1: Vcc PS2 wht my red pin 2 << let digital output give power to ps2 mouse
*/
int led = 13; // the PWM pin the LED is attached to
int pwr = 2; // PS2 VCC pin
#include "PS2Mouse.h"
// use for clk and data
PS2Mouse mymouse( 6, 5);
void setup(){
pinMode(led, OUTPUT);
pinMode(pwr, OUTPUT); // for powering up PS2 mouse
digitalWrite(pwr, LOW);
delay (500);
digitalWrite(pwr, HIGH);
delay (1200);
mymouse.begin();
}
void loop(){
uint8_t stat;
int x,y;
mymouse.getPosition(stat,x,y);
// using y value to fade led as mouse moves
// It actually works now!
analogWrite(led, abs(y*1.5));
delay(100);
}