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:
- 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.
- 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:
- according to the code, converting method to degree can be modified or not?
- 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)
- Any1 can advised me how to works on this moving algorithm? many thanks for those who replied. Many thaks