I'm not an Arduino whiz by any measure. Just coded this today for my project and wondered if it might be helpful for anyone else. I owe a lot to this forum so i'd like to give something back if possible.
The trackball: http://www.parallax.com/product/27908
Feels just like a Blackberrry trackball. Seems to work pretty well so far.
Really simple to connect to the Arduino board, connect the OPA,OPB,OPC and OPD pins to any of the digital input pins on the Arduino. I randomly chose pins 3,5, 8 and 12. Also connected it to draw power from the Arduino board, and the same input for the LED. Works just fine without the LED but i like to leave it on when it's active.
Like i said, the code's really simple and im sure there are better ways to code this. But hopefully this will be helpful to fellow newbies like me.
#define pinA 12
#define pinB 3
#define pinC 5
#define pinD 8
int initA;
int initB;
int initC;
int initD;
int newvalA;
int newvalB;
int newvalC;
int newvalD;
int recordA;
int recordB;
int recordC;
int recordD;
int change;
long int x=0;
long int y=0;
int invert(int init) {
int notvalue ;
if (init == 0)
{ notvalue =1; }
else
{notvalue =0;
}
return notvalue;
}
int compare (int init, int newval)
{
int record=0;
if (newval == init) {
change=0;
}
else
{ change =1;
record = record +1;
}
return record;
}
void setup () {
Serial.begin(38400);
pinMode(pinA,INPUT);
pinMode(pinB,INPUT);
pinMode(pinC,INPUT);
pinMode(pinD,INPUT);
initA = digitalRead(pinA);
initB = digitalRead(pinB);
initC = digitalRead(pinC);
initD = digitalRead(pinD);
}
void loop () {
newvalA = digitalRead(pinA);
newvalB = digitalRead(pinB);
newvalC = digitalRead(pinC);
newvalD = digitalRead(pinD);
recordA = compare(initA,newvalA);
if (change==1)
{
initA = invert(initA);
}
recordB = compare(initB,newvalB);
if (change==1)
{
initB = invert(initB);
}
recordC = compare(initC,newvalC);
if (change==1)
{
initC = invert(initC);
}
recordD = compare(initD,newvalD);
if (change==1)
{
initD = invert(initD);
}
x = x + recordA - recordC;
y = y + recordB - recordD;
Serial.print("Position \t X=");
Serial.print(x,DEC);
Serial.print("\t");
Serial.print("Y=");
Serial.print(y,DEC);
Serial.print("\n");
}
Here's what the output looks like for now
Cheers !