Controlling servo motor using ADXL335 accelerometer

Hey guys ,
I’m making a project that is similar to a self balancing robot in which i’m using an ADXL335 accelerometer to control two servo motors.
I wrote and tried too many sketchs but it seems that none of them is working.
Here’s the code below :

#include <Servo.h>

// these constants won’t change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
int val;
int valAverage = 0;
Servo myservo;
Servo myservo1;// create servo object to control a servo
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
myservo.attach(9);
myservo1.attach(11); // attaches the servo on pin 9 to the servo object
}

void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;

// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);

// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g’s:
// earth’s gravity is 1000 milli-g’s, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;

// print the acceleration
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
//simple smoothening function
for(int i = 0; i < 5; i++){
val = map(accelerationY,-1200 , 1200, 0, 179); // scale it to use it with the servo (value between 0 and 180)
//can put a if stament here.
valAverage = valAverage + val;
}
// if value is too high change number you divide by
valAverage = valAverage / 5;
myservo.write(val);
int val1 = 180 - val;
myservo1.write(val1); // sets the servo position according to the scaled value
// valAverage = 0;
delay(100);
}

I would be really grateful if you guys help .

I's start with one motor only, -segway-like.. The accelerotater gives enough inot to balnce. ....so did 2 optical (very simple reflective sensors) on the lego nxt (even this is a stronger prosessor)

You can't expect advice when you haven't described what the problem is - none of them is working isn't very informative. Neither is politics in Iraq.

Why are you converting pulses into acceleration and then back into servo angles. That's just a waste of computer time and probably loses precision as well. Cut out the middle man.

Have you tested that the accelerometer gives the correct values without bothering with the servos? (I assume, though you haven't said) that the accelerometer produces the pulses that you are measuring with pulseIn(). How about posting a link to the dataseheet for the accelerometer.

How about telling us what the project is actually supposed to do?

...R

Yes i tested the accelerometer alone it works just fine .And the project is a two wheel self balancing robot that uses the orientation of just one axe of the accelerometer to adjust the rotation of the two servo motors Here's the datasheet: https://www.sparkfun.com/datasheets/Components/SMD/adxl335.pdf

I'm not much wiser. You still haven't explained what you mean by "none of them is working".

...R

I’m trying to wrap my head around what you wrote, but it doesn’t make any sense.

pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);

// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g’s:
// earth’s gravity is 1000 milli-g’s, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;

// print the acceleration
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();

Ok, do these output the correct data

//simple smoothening function
for(int i = 0; i < 5; i++){
val = map(accelerationY,-1200 , 1200, 0, 179); // scale it to use it with the servo (value between 0 and 180)
//can put a if stament here.
valAverage = valAverage + val;
}

accelerationY is at this point is never going to change while its in the FOR loop, so val is also never going to change therefore valAverage is always going to be the product of (val * 5).

// if value is too high change number you divide by
valAverage = valAverage / 5;
myservo.write(val);
int val1 = 180 - val;
myservo1.write(val1); // sets the servo position according to the scaled value
// valAverage = 0;
delay(100);
}

In here val is the last outcome from what was done in the FOR loop, and being that the stuff in the FOR loop is not changing, val = valAverage/5;

int val1 is not needed, because you can just have myservo1.write(180 - val);

For right now, comment out the FOR loop, all of it, and just have this "val = map(accelerationY,-1200 , 1200, 0, 179); " and see what you get.