I'm recently attempting to get direction data off of an optical mouse.
As far as I've been able to tell, the sensor itself has an i2c connection for the data. This connects to a chip that deals with the USB/PS/2 connection. While I'd like to try to use one with the PS/2 at some point, I already had this one part way ready, so I'm trying the i2c. This is probably something I'll want to use in the future anyway, so the practice is good. Even though the optical mouse library seems to be working with it, I'd like to figure out what's wrong with this setup.
I took the mouse apart, and soldered wires to connect the sensor IO pins directly to the data pins on the small connector that takes the cable that goes to the computer. I also cut the traces connecting the connector pins to the connector, so it wouldn't interfere. I'm now contemplating doing the same between the sensor and the interpretor chip too--the board gives me a connector, the LED, crystal for the sensor, and other stuff I figured would be good.
Here's the code I'm using:
#include <Servo.h>
#include <Wire.h>
#include <WString.h>
#include <SoftwareSerial.h>
String serialString = String(5);//string for taking data from computer
String mouseString = String(10);//i2c data
int deltaX = '0';
int deltaY = '0';
Servo servo;
boolean doloop = true;
boolean maybe = true;
void setup(){
Serial.begin(9600);
Serial.println("delta x and delta y");
Wire.begin();
servo.attach(2);
servo.write(90);
}
void loop(){
Wire.requestFrom(1, 2);
while (doloop == true)
{
char c = Wire.receive();
mouseString.append(c);
deltaX = atoi(mouseString);
mouseString ="";
Serial.println("test");
Serial.println(deltaX);
doloop = false;
}
delay(2000);
doloop = true;
}
The servo library is in there so I can play with the servo with the mouse later--the intention is to be able to use the sensor to find speed and direction change on a robot, so moving the servo is a good first step.
The problem I'm having is that the code freezes at the requestFrom function call. If I pull out the power lines for the mouse to reset it, it'll loop around, but stop there again(put a println("test1", "test2", etc at places to find the stopping point)the next time around.
I'm not sure it's getting stuff from the mouse. I'm inclined to say I need to send a signal to the mouse beyond the request and receive functions to get an answer, actually tell it to send me stuff or something.