Here’s a Chuck that helps calibrating your laptop tilt sensor, when the readings are not 0,0 on your current surface. You say- you’re picking the laptop up to actuate change. This is important when you place it down again on an uneven surface, also if your null-state changes settings.

**Future update, will allow keyboard toggling of faster/slower refresh rates.

Wishlist: Maui interface to control everything

Code:

//Nick Hwang, July 20009
//Instantiate Human interface variables for Keyboard and Tilt Sensor ( X and Y values)

Hid hiKB, hiTilt;
HidMsg msgKB, msgTilt;
// open tilt sensor ============

if( !hiTilt.openTiltSensor() )
{
< << "tilt sensor unavailable", "" >>>;
me.exit();
}
//==============================
// which keyboard ==============
0 => int device;
// get from command line
if( me.args() ) me.arg(0) => Std.atoi => device;
// open keyboard (get device number from command line)
if( !hiKB.openKeyboard( device ) ) me.exit();
< << "keyboard '" + hiKB.name() + "' ready", "" >>>;
//================================
//Instantiate variables
2 => int Cal;
int xdiff, ydiff;
20::ms => dur RR; //Refresh rate of tilt sensor readings

int x, y;

// x, y values after calibration
/* =========================================
Takes inital sensor readings
Asks if you want to calibrate (Y, N, or R (retake settings)
===========================================*/
hiTilt.read(9, 0, msgTilt);
< << "Currently at",msgTilt.x, msgTilt.y >>>;
< <<"Calibrate? Y, N, R (retake current setting)">>>;
until( Cal!=2)
{
// wait on event
hiKB => now;
// get one or more messages
while( hiKB.recv( msgKB ) )
{
// check for action type
if( msgKB.isButtonUp())
{
if(msgKB.ascii == 89 )
{
< << "yes">>>;
1 => Cal;
Calibrate();
break;
}
if(msgKB.ascii == 78 )
{
0 => Cal;
break;
}
if(msgKB.ascii == 82 )
{
hiTilt.read(9, 0, msgTilt);
< << "Currently at",msgTilt.x, msgTilt.y >>>;
}
}
}
}
< << "Launching Program" >>>;
1::second => now;
Program();
/*============================================================================
Takes the current X Y positions of laptop and find the difference from (0,0).
The difference is later applied to the non-adjusted X Y values.
=============================================================================*/
fun void Calibrate()
{
< <<"Calibrating...">>>;
hiTilt.read(9, 0, msgTilt);
if (msgTilt.x > 0)
{
msgTilt.x => xdiff;
}
if (msgTilt.x < 0) { msgTilt.x * -1=> xdiff;
}
if (msgTilt.y > 0)
{
msgTilt.y => ydiff;
}
if (msgTilt.y < 0) { msgTilt.y * -1=> ydiff;
}
< << xdiff, ydiff>>>;
}

/*==================================================================
This returns the X and Y positions at Refresh Rate (currently 20ms)
If you chose to cailbrate, it will return the adjusted numbers, if not
X and Y will be ?undjusted?.
===================================================================*/

fun void Program()
{
if(Cal == 1)
{
while(true)
{
RR => now;
hiTilt.read(9, 0, msgTilt);
(msgTilt.x - xdiff) => x;
(msgTilt.y - ydiff) => y;
< << x, y >>>;
}
}
else
{
while(true)
{
RR => now;
hiTilt.read(9, 0, msgTilt);
msgTilt.x => x;
msgTilt.y => y;
< << x, y >>>;
}
}
}