Need help for algorithm that act like wrist (Adxl335 + servo motor)

Hi all before that i am using ADXL345 eval board. Because of the communication problem between the eval board and arduino. I give up on the eval board and change to breakoutboard ADXL335. The before posting is: http://arduino.cc/forum/index.php/topic,95980.0.html

Now i can get the reading directly from adxl335 and process it at arduino. Now my project is related on the hand movement control servo motor. I am referring to http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/ so that i can figure out what algorithm can be used to act like the movement of the wrist.

Objective:

  1. the sensor is placed on wrist. The movement of the wrist is transfer to the Arduino Uno board and process and then send the output to servo motor.
  2. 3 servo motors are used to control each axis. (x,y,z)

Here is what i had in my code:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
Servo myservo1;
Servo myservo2;
  
const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 262;
int maxVal = 402;

//to hold the caculated values
int x = 0;
int y = 0;
int z = 0;

void setup(){
  Serial.begin(9600);
  myservo.attach(9);
  myservo1.attach(10);
  myservo2.attach(11);
}

void loop(){

  //read the analog values from the accelerometer
  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);

  //convert read values to degrees -90 to 90 - Needed for atan2
  int xAng = map(xRead, minVal, maxVal, -90, 90);
  int yAng = map(yRead, minVal, maxVal, -90, 90);
  int zAng = map(zRead, minVal, maxVal, -90, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -? to ? (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

  //Output the caculations
  Serial.print("x: ");
  Serial.print(x);
  myservo.write(x);
  delay(15);
  Serial.print(" | y: ");
  Serial.print(y);
  myservo1.write(y);
  delay(15);
  Serial.print(" | z: ");
  Serial.println(z);
  myservo2.write(z);
  delay(15);

  delay(100);//just here to slow down the serial output - Easier to read
}

Question:

  1. according to the code, converting method to degree can be modified or not?
  2. After tested the code with servo motor, the servo motor will move randomly (seem like messy output but it gives no problem in serial monitor)
  3. Any1 can advised me how to works on this moving algorithm? many thanks for those who replied. Many thaks :slight_smile:

i modified my code at the part of atan2 to:

//convert read values to degrees -90 to 90 - Needed for atan2
  int xAng = map(xRead, minVal, maxVal, 0, 180);
  int yAng = map(yRead, minVal, maxVal, 0, 90);
  int zAng = map(zRead, minVal, maxVal, 0, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -? to ? (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(yAng, zAng));
  y = RAD_TO_DEG * (atan2(xAng, zAng));
  z = RAD_TO_DEG * (atan2(yAng, xAng));

To convert to degree and match with servo motor range. I remap it to become 0-180 for x, 0-90 for y and 0-90 for z. But now the problem i met is when i use more than 1 servo motor, the reading will become unstable...may i know why?
when i am using 1 servo motor. It works well. I can control the servo motor with sensor. (although not exact accurate angle). But when i connect 2 servo motor. The data will become negative values...is it be power supplied problem?all the Vcc and Gnd is connected with same pin. That is 3V3.

any suggestion is really helpful. Thanks first =(

Does sound like a power issue - generally, you can't power servos from the Arduino, especially if you're trying it with more than one.

wildbill:
Does sound like a power issue - generally, you can't power servos from the Arduino, especially if you're trying it with more than one.

so what method can u suggest to me??because i saw from:

so i believe that can use the power. Is there any problem in my code?...anyway...really thanks so much for your replied..appreciate...

What you should do is power the servo with an external source, the arduino should just send the commands. In practice, plug the servo power pin and ground to a power source, and the servo command pin and ground to the arduino.

overdrivr:
What you should do is power the servo with an external source, the arduino should just send the commands. In practice, plug the servo power pin and ground to a power source, and the servo command pin and ground to the arduino.

this is also another way that i am thinking of. But i need another power source..the external power source u mean is battery?because i wish to make a easier carry and mobility sensor...hope my sensor will not be too much stuff on top. Is that mean i need to use another 1 arduino board?
Any more suggestion is appreciate...really need help here.... =(