I'm trying to write a program to work with Parallax's Mouse Sensor kit. I've seen a lot of people trying to do something similar with old mice-I think it would be fantastic to get this to work because then we would have standardized the hardware somewhat.
I've succeeded after a fashion. So long as I make slow, smooth movements it appears to function as desired. However, when I make sudden movements, the sensor is often "tricked" - it will say it isn't moving or is moving in the opposite direction at times. It looks almost like an overflow problem, but when I check the status bits for dy & dx, they're 0s.
When I run the basic stamp program with the sensor hooked up to my Boe-Bot, the performance is much more consistent, so it's not a limitation of the hardware. I also noticed that the basic stamp program seems to be more granular-it looks like it returns something besides a 0, 128, 0r 255; where these are the only values I seem to get when using the arduino.
Datasheets and sample programs can be found at: https://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/677/Default.aspx?txtSearch=mouse+sensor
Here's what I have so far:
#define sclk 5 //Define pin 5 as clock
#define sdio 6 //Define pin 6 as data
//Mouse sensor register addresses
#define CHNG 0x80
byte stat;
byte readx; //Vallue from sensor: 255 = -1, 128 = 1
byte ready; //Vallue from sensor: 255 = -1, 128 = 1
byte quality;
int dx;
int dy;
int x;
int y;
byte WriteNeg(byte data)
{
pinMode(sdio,OUTPUT);
for(int i = 0; i < 8; i++)
{
digitalWrite(sdio,bitRead(data,7));
digitalWrite(sclk,LOW);
delayMicroseconds(50);
digitalWrite(sclk,HIGH);
delayMicroseconds(50);
data = data << 1;
}
pinMode(sdio,INPUT);
}
byte ReadNeg()
{
byte data = 0;
for(int i = 0; i < 8; i++)
{
digitalWrite(sclk,LOW);
delayMicroseconds(50);
digitalWrite(sclk,HIGH);
delayMicroseconds(50);
bitWrite(data,i,digitalRead(sdio));
}
return data;
}
void WriteAddr(byte address, byte data)
{
WriteNeg(address|0x80);
WriteNeg(data);
}
int ReadAddr(byte address)
{
WriteNeg(address);
delayMicroseconds(100);
return ReadNeg();
}
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.print("Program Initializing");
pinMode(sclk,OUTPUT);
digitalWrite(sclk,HIGH);
Serial.print("\nClock pin set high");
delay(100);
WriteAddr(0xB1,0x80); //Sets resolution to 500 dpi
Serial.print("\nResolution set to 500 dpi");
Serial.print("\nMain program starting\n");
}
void loop()
{
stat = ReadAddr(0x16);
if (stat & CHNG != 0)
{
readx = ReadAddr(0x03);
switch (readx)
{
case 255:
dx = -1;
break;
case 128:
dx = 1;
break;
case 0:
dx = 0;
break;
}
x = x + dx;
Serial.print(dx,DEC);
Serial.print("\t");
Serial.print(x,DEC);
Serial.print("\t");
ready = ReadAddr(0x02);
switch (ready)
{
case 255:
dy = -1;
break;
case 128:
dy = 1;
break;
case 0:
dy = 0;
break;
}
y = y + dy;
Serial.print(dy,DEC);
Serial.print("\t");
Serial.print(y,DEC);
Serial.print("\t");
quality = ReadAddr(0x04);
Serial.print(quality,DEC);
Serial.print("\t");
Serial.print(stat,BIN);
Serial.print("\n");
}
}