trying to use an accelerometer to control two servos, here is the code I have...
but this code for some reason just causes both servos to sweep constantly back and forth, with what seems like no regard to the position of the accelerometer?
thanks fellas
/*
The circuit:
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc
digital 9: servo_s
digital 10: servo_p
*/
// these constants describe the pins.
#include <Servo.h>
Servo servo_S; // create servo object to control a servo (starboard)
Servo servo_P; // create servo object to control a servo (port)
int starboard_val; // variable for starboard servo
int port_val; // variable for port servo
int groundpin = 18; // analog input pin 4 -- ground
int powerpin = 19; // analog input pin 5 -- voltage
int xpin = 1; // x-axis of the accelerometer
int ypin = 2; // y-axis
void setup()
{
servo_S.attach(9); // attaches the servo on pin 9 to the servo object
servo_P.attach(10); // attaches the servo on pin 10 to the servo object
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop()
{
analogRead(xpin); // reads accelerometer value in x axis
analogRead(ypin); // reads accelerometer value in y axis
delay(100); // delay before next reading
starboard_val = ((xpin + ypin)/2); // defines starboard servo position
starboard_val = map(starboard_val, 280, 372, 26, 154); // maps values to acceptable servo values
servo_S.write(starboard_val); // sets the servo position according to the value
if (ypin==326)
{
ypin=326;
}
else if (ypin<326)
{
ypin=((326-ypin)+326);
}
else
{
ypin=(326-(ypin-326));
}
port_val = ((xpin + ypin)/2); // defines port servo position
port_val = map(port_val, 280, 372, 26, 154); // maps values to acceptable servo values
servo_P.write(port_val); // sets the servo position according to the scaled value
delay(100); // waits for the servo to get there
}