Well, simply the sensor should read 200 on an axis when it is pointing straight up. The other axis wshould read 0. That reading of 200 is the force of gravity.
It's possible to detrimine the angle that the sensor is relative to that force.
Without going into a lot of explanation the simple way to do it is:
float unitX = ((float)accellX)/200.0;
float angle = acos(unitX);
angle = radiansToDegrees(angle);
where accelX is the reading of the x axis of the sensor. 200 is the force of gravity.
accelX can be changed to an axis you want and you should verify the the reading at level to make sure that it reads 200;
If you just want it to level something here's a simple sample program
(x should read 0 when level)
accelerometer.h is
 |  |  |
 | Code: /* Hitechnic accelerometer sensor driver
sample:
#include "accelerometer.h"
task main() { accelData accellerometer; accelStart(S1);
while(1) { readAccel(accelerometer);
nxtDisplayTextLine(1,"x %d",accelerometer.x); nxtDisplayTextLine(2,"y %d",accelerometer.y); nxtDisplayTextLine(3,"z %d",accelerometer.z);
} }
*/
#define accelAddress 0x02
typedef struct{ byte MsgSize; byte DeviceAddress; byte LocationPtr; }accelBlock;
typedef struct{ int x; int y; int z; }accelData;
accelBlock accelI2C; tSensors Accelport;
void accelStart(tSensors port) { Accelport =port;
accelI2C.MsgSize =2; accelI2C.DeviceAddress = accelAddress; accelI2C.LocationPtr = 0x42;
SensorType[Accelport]= sensorI2CCustomFast; }
void readAccel(accelData &values) { byte rawData[6];
nI2CBytesReady[Accelport] = 0;
sendI2CMsg(Accelport, accelI2C.MsgSize, 6);
while (nI2CStatus[Accelport] == STAT_COMM_PENDING) wait1Msec(2);
if (nI2CStatus[Accelport] == NO_ERR) { readI2CReply(Accelport, rawData[0], 6);
values.x = ( rawData[0] << 2) + (0xFF & rawData[3]); values.y = ( rawData[1] << 2) + (0xFF & rawData[4]); values.z = ( rawData[2] << 2) + (0xFF & rawData[5]); } }
|  |
 |  |  |
this program will producce a little "head bobbing" but it illustrates the point.
balancing is much more difficult. I've read that it's not possible with a accelerometer alone, you also need a gyroscope. But that may not be true. I think I've seena balancing robot that only used an accelerometer but there wasn't much in the way of details, so it might of had a gyro as well.