 # Make an acceleromer control a servo.

Can any one write the code that this guy used to make his servo react the way it does. Thank you! ``````#include <Servo.h>
const int servoPin = 9;

Servo myServo;

void setup() {myServo.attach(servoPin);}

void loop()
{
int angle = map(accel, 0, 1023, 0, 180);
myServo.write(angle);
}
``````

THANK YOU SO MUCH!!!!

Does anything have to go in the parenthesis with the word angle in it? I have an odd feeling like there should be an angle measure there.`myServo.write(angle);`

roboticfan101: Does anything have to go in the parenthesis with the word angle in it? I have an odd feeling like there should be an angle measure there.`myServo.write(angle);`

'angle' is the name if an integer variable, calculated from the acceleration:

``````    int accel = analogRead(ADXLpin);
int angle = map(accel, 0, 1023, 0, 180);
myServo.write(angle);
``````

Hi again! I have a new question. Would this code be compatible with a Mesmic 2125 accelerometer? Can you explain if any the difference? Thanks a lot! I've learned a lot from asking questions of this forum. You guys are great! :D

The Mesmic 2125 accelerometer (http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/memsickit.pdf) outputs a variable-width pulse rather than an analog voltage. At 0G the output pulse is 5000 microseconds high and each 1G would be a change of 1250 microseconds. A pulse of 3750 microseconds would be -1G and 6250 microseconds would be +1G. The code for that would be something like this:

``````    int accel = pulseIn(MemsicPin);
int angle = map(accel, 3750, 6250, 0, 180);  // map -1G through +1G to 0 through 180
myServo.write(angle);
``````

Oh, I see! That's so cool! Thanks again, you rock!