self balance robot code for dead spot help needed

my first self balance robot... I have to robot put together and working.
I tried to put in a dead spot(stop the motors) at the balance point but it is not working
here is my code. please help of advise
//for this project, I have had partial succes with a self balance robot. I used the
//"cheap" tumbler rc robot gear boxes, arduino uno rev 3, 2 small proto boards,
//I am also using the parallax dual axis accelerometer from radio shack.
//this version uses cut pieces of code from the seeed grove sample code and also
//cut up sample code from parallax
//you can contact me if you need at stevecobb76@gmail.com
//this is the first version of the code for this bot and does not work as of yet.
//it is just my starting point.
//version 0.1 beta testing phase
//
//notes, robot seems jumpy, and dead spot or (balance point) is not working in this code.
//might need servo motors or faster gear motors instead of tumbler rc motor/gear boxs
// p.s. grove motor shield sucks hard... they block all pins...

const int xPin = 6; // X input from memsic dual axis accelerometermx2125
const int yPin = 5; // Y input accelerometer
int pinI1=8;//define I1 interface //code cut from grove motor shield sample
int pinI2=11;//define I2 interface //code cut from grove motor shield sample
int speedpinA=9;//enable motor A //code cut from grove motor shield sample
int pinI3=12;//define I3 interface //code cut from grove motor shield sample
int pinI4=13;//define I4 interface //code cut from grove motor shield sample
int speedpinB=10;//enable motor B //code cut from grove motor shield sample
int spead =255;//define the spead of motor //code cut from grove motor shield sample

int xPulse, yPulse; // read from accelerometer code cut from grove motor shield sample
int xVal, yVal; // converted value code cut from grove motor shield sample

void setup() {
Serial.begin(9600); //just so I can try to see what is going on...
pinMode(xPin, INPUT); //code cut from grove motor shield sample
pinMode(yPin, INPUT); //code cut from grove motor shield sample
pinMode(pinI1,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI2,OUTPUT); //code cut from grove motor shield sample
pinMode(speedpinA,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI3,OUTPUT); //code cut from grove motor shield sample
pinMode(pinI4,OUTPUT); //code cut from grove motor shield sample
pinMode(speedpinB,OUTPUT); //code cut from grove motor shield sample
}
void forward() { //code cut from grove motor shield sample
analogWrite(speedpinA,spead);// I dont get this, but ok...
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
delay(500); // trying to smooth out response/reaction time
}
void backward() //code cut from grove motor shield sample
{
analogWrite(speedpinA,spead);//input a simulation value to set the speed
analogWrite(speedpinB,spead);
digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
delay(500);
}
void loop()
{
// read x
xPulse = pulseIn(xPin,HIGH);
xVal = ((xPulse / 10) - 500) * 8;
Serial.print("x axes =");
Serial.print("\t");
Serial.print(xVal); // print to serial xval
Serial.println();
//______________________________________________________________
// this is where I need help!!!!
if (xVal > 50)//trying to create dead zone if xVal = 2 to 49 from acceleratometer.. did not work

{
backward();
}
if(xVal < 1)
{
forward();
}
}

to make it clear as to what I am trying to do,
I want the motors to stop in the dead spot aka the balance zone!!!

I don't know enough about how your motors are wired up and arranged to guess what that sketch will do, but it doesn't contain any of the sort of feedback logic I'd expect to see in a balance bot. Taken in conjunction with the half second delays and serial output in the 'real time' loop makes me skeptical that this sketch actually balances anything. Have I misunderstood what you're trying to do?

To address the specific question you're asking about, you don't say what the accelerometer output values are but the code implies that the accelerometer produces a signed value with zero meaning 'no acceleration' and positive/negative values corresponding to forward/backwards acceleration (I haven't bothered to work out which is which).

In that case, if you want the bot to settle around the 'zero' position, there are two approaches.

What you're doing now applies full power in whichever direction is needed and in that case you could simply switch the motor off if the 'bot is 'close enough' to balanced - and hope you can catch it when it starts to fall. The code for that would be something like:

if(xVal  < -50)
{
    // falling forwards, apply forward power
    forward();
}
else if (xVal < 50)
{
    // in the dead band - stop motors
    stop();
}
else
{
    // falling backwards, apply backward power
    backward
}

I'm skeptical that this will actually achieve a balance, but this achieves what you seem to want the code to do.

The other approach you could take is to vary the speed and direction of the motor according to how far the 'bot is away from the balance point. With that approach the speed naturally drops down to zero as the 'bot reaches the balance point and you don't need an explicit dead band. This approach is IMO likely to give better behaviour, assuming that your sensors and overall feedback algorithm is sufficient to achieve a balance at all - which I'm not convinced about.

The other approach you could take is to vary the speed and direction of the motor according to how far the 'bot is away from the balance point. With that approach the speed naturally drops down to zero as the 'bot reaches the balance point and you don't need an explicit dead band. This approach is IMO likely to give better behaviour, assuming that your sensors and overall feedback algorithm is sufficient to achieve a balance at all - which I'm not convinced about.
[/quote]
yes you were right about all..
thank you very much for the advice.
I like the idea of using feed back. so I thought I could use 2 rotary encoder from scroll wheels in mice. hmmmm there is a extended shaft comming off the back of the gearbox opposite of the output shaft that I could attach the encoders too.
and thank you very much for the code example. I didn't know if I was doing the neg number (i.e. -50) correctly.
ps. the servo output is 0 for level and appx +600 for appx 45* angle on the y axis in one direction and -600 for appx. 45* angle the other direction on the same axis. <---- I hope you can appreciate those specs? lol

Before I get too much into this code that may not work at all, what would be a better approach?
(assuming that I do use the rotary encoders or change out to servo's modded for continuous rotation)
would it be good to use the accelerometer output (xVal) as a variable to directly change motor speed and direction? i.e.
if xVal <= 0
backward
if xVal >= 0
foward
and use xval (with a little math) to change pwm?
wow, this is starting to get a little complicated...
"I'm not scared" and the yoda reply" you will be.... , you will be"
I am just trying to pick brains here... thanks all.

I have not done self balance robot ,and I want to do one too.
Here is a suggestion that may be helpful.

use a angle sensor to get the angle and then to conctrol the speed and direction of DC motor.
after a certain time and do the upper processing again.
If do with this method ,I think that maybe it can run forward at a certain angle and certain speed.

hope you can finish soon and share you control code.