Beruflich Dokumente
Kultur Dokumente
//Simple code for the ADXL335, prints calculated orientation via serial ////////////////////////////////////////////////////////////////// //Analog read pins 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 = 265; int maxVal = 402; //to hold the caculated values double x; double y; double z; void setup(){ Serial.begin(9600); } void loop(){ //read the analog values from the accelerometer int xRead = analogRead(xPin); int yRead = analogRead(yPin); int zRead = analogRead(zPin); //convert read values int xAng = map(xRead, int yAng = map(yRead, int zAng = map(zRead, to degrees -90 to 90 minVal, maxVal, -90, minVal, maxVal, -90, minVal, maxVal, -90, - Needed for atan2 90); 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); Serial.print(" | y: "); Serial.print(y); Serial.print(" | z: "); Serial.println(z);
El ADXL335 proporcionar a Arduino, tres voltajes proporcionales a la aceleracin de cada eje X, Y y Z. Algo muy importante es que se trata de un dispositivo que se alimenta a 3.3 V, por lo que hay que tener cuidado al conectarlo a nuestro Arduino de no hacerlo a la salida de 5v, ya que daaramos el sensor. Conexion:
Codigo: const int xPin = 0; const int yPin = 1; const int zPin = 2; //Pines analogicos de lectura
int minVal = 265; // Valores mnimos y mximos del acelerometro en reposo int maxVal = 402; double x; double y; double z; // para guardar los valores calculados
void setup ( ) { Serial.begin(9600); } void loop ( ) { int xRead = analogRead(xPin); //Lee los valores analogicos del acelerometro int yRead = analogRead(yPin); int zRead = analogRead(zPin); // mapea los valores leidos a un rango -90 a 90 grados (- a ) int xAng = map(xRead, minVal, maxVal, -90, 90); int yAng = map(yRead, minVal, maxVal, -90, 90); int zAng = map(zRead, minVal, maxVal, -90, 90); //Convertimos los radianes a grados x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); //Imprimimos en el monitor serial los caluculos Serial.print(x: );
This example is for Wiring version 1.0 build 0100+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know. Acceleromenter ADXL335 Sparkfun breakout by BARRAGAN http://barraganstudio.com Reads values from an accelerometer with X, Y and Z connected to analog inputs 0, 1 and 2 respectively The values are printed to the serial monitor
z = analogRead(2); // Serial.print("accelerations Serial.print(x, DEC); // Serial.print(" "); // Serial.print(y, DEC); // Serial.print(" "); // Serial.println(z, DEC); // delay(100); // }
read analog input pin 1 are x, y, z: "); print the acceleration in the X axis prints a space between the numbers print the acceleration in the Y axis prints a space between the numbers print the acceleration in the Z axis wait 100ms for next reading