So, I've been trying to write a program for the accelerometer sensor to get the vector magnitude based on the x,y and z values. However, the output of the multiplication part doesn't seem right at all. It always shows the wrong value for the vlength, which is way smaller than the actual value. Is there anything wrong with my code?
int arr[3] = {0,0,0};
void setup() {
Serial.begin(9600);
}
void loop() {
readXYZ();
magnitude();
}
int readXYZ(){
arr[0] = analogRead(A0);
arr[1] = analogRead(A1);
arr[2] = analogRead(A2);
delay(100);
Serial.print("x: ");
Serial.print(arr[0]);
Serial.print(" y: ");
Serial.print(arr[1]);
Serial.print(" z:");
Serial.print(arr[2]);
}
int magnitude(){
long vlength = sqrt(sq(arr[0])+sq(arr[1])+sq(arr[2]));
Serial.print(" m:");
Serial.println(vlength);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.