ARGGGGGG.... Servos...

So guys, ive been enjoying my newly received accelerometer recently, made some pretty cool stuff on my own, including a tilt detector wit some led’s

but here is my problem… lets say i wanted to control a servo similar to the tutorial “potentiometer knob servo control” except by tilting the accelerometer…

so here is the code i have now:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int groundpin = 18; // analog input pin 4
int powerpin = 19; // analog input pin 5
int xpin = 3; // x-axis of the accelerometer

int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(xpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}

clearly something isnt right, cause the servo just randomly twitches and then does nothing… im guessing its the “map” part of the code, but the thing is i dont knw the min/max values of the accelerometer… so if anyone can help me out, pls do so

P.S.: i have the ADXL330, if that makes a diff with the min/max values

You don’t show anything about “setting up” the accelerometer at all. Are you feeding power by GND and VCC, or by other analog pins? If you mean the analog pins you declare, then you have to set them as outputs and write HIGH to the power, LOW to the ground.

Give more time for the servo to react? I see at least 15 milliseconds in most servo apps.

(And the ... tags are preferable to red. They’ll retain indentation better and can be longer.)

Are you sure you have the accelerometer connected to analog pin 3 and not digital pin 3?

If so, put some debugging in to check the values coming out of your accelerometer.

ure absolutely right about me not setting the pins up right… my bad… il see if it fixes it… and yes etracer its all connected properly

sorta works now… any way to make the servo control more sensitive?

Check to see what range the accelerometer is providing – you can do this by sending the values to the serial port. You may also want to add some smoothing: http://www.arduino.cc/en/Tutorial/Smoothing

If the range is less than 0 to 1023 then you can change the arguments in the map function to send 0 degrees to the servo when your minimum value is detected and 180 with your maximum value

thx, i fixed it right after i posted, i still need to fine tune the values but its fairly nice running at 400 min 600 max… considering that 512 is the middle (horizontal position) value