Accelerometer and servo code trouble

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       

}

y are ur vcc & gnd connected

y are ur vcc & gnd connected to analog ports? those are the acc’s pins rite? they sld be connected to the 3.3/5 V & gnd pins on the arduino

oh, yes they are connected

oh, yes they are connected to the 3.3 and grd, that must be a chunk of left over code from an arduino example

also, this error is

also, this error is streaming across the bottom

 

Error inside Serial.serialEvent()

java.io.IOException: Bad file descriptor in nativeavailable

at gnu.io.RXTXPort.nativeavailable(Native Method)

at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)

at processing.app.Serial.serialEvent(Serial.java:215)

at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)

at gnu.io.RXTXPort.eventLoop(Native Method)

at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

also i added in a serial

also i added in a serial output to read the values from the accelerometer, they are correct. So im guessing it has something to do with what is written for the servos 

Not sure about the map

Not sure about the map values, since an analogue reading would give you a 10bit value. 0-1024. I think you could try using contstrain on the function as well to keep the values between the values that you are using. I’m also not sure if when using the map function  with values like 280-326 don’t wrap around, which would cause some strangeness.(say you had 328, that would wrap around to 282)

Have you tried to read the value that you are sending to the servo, not just what you’ve read from the adc. It sounds like your values are different from what you’d expect.

Out of curiousity, what exactly are you trying to do?

 

 

**My first post on lmr **

and english isnt my main language but Anyway

your error is with your use of analogRead(xpin) and analogRead(ypin), analogRead doesn"t allocate the read value between to (xpin).

the first time your loop is executed, xpin and ypin values are 1, and starboard_val become -162, your servo wont like this value. Below when you test ypin, it allocate -162 to ypin because of it being 1, this is why you have an erratic behavior with your servo, the error is amplified every loop.

you should use analogRead more like this:

myReadValue = analogRead(myReadPin);

that should do it.

 

 

hmm interesting, thank youso

it worked!, thanks for your help everyone, it is greatly appreciated! 

hmm, i took at the values

hmm, i took at the values the servo was receiving and like I expected it was just bouncing between two seeming random numbers (88 and -326 i think), 

 

concerning the values you suggest I should be using, when I look at the output from my accelerometer, I see values in the range of 280 to 372, is that not what it is actually sending out (is it really between 0 and 1024)?

I think the if statemenst

I think the if  statemenst shoul go before

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