Hello all.
An Arduino newbie here.
Below is a simple sketch that reads the x,y,z values from an Analog Accelerometer. It is an ADXL335.
1. #include "ADXL335.h"
2. ADXL335 accelerometer;
3. void setup() {
4. Serial.begin(9600);
5. accelerometer.begin();
6. }
7. void loop() {
8. int x, y, z;
9. accelerometer.getXYZ(&x, &y, &z);
10. Serial.print("X: ");
11. Serial.print(x);
12. Serial.print("\tY: ");
13. Serial.print(y);
14. Serial.print("\tZ: ");
15. Serial.println(z);
16. delay(500); // Adjust delay as needed
17. }
Serial Monitor output
X: 285 Y: 358 Z: 348
X: 285 Y: 358 Z: 348
X: 285 Y: 358 Z: 348
I need the x values to be atleast 3 places after the decimal.
So for example I need the X value to say 285.154 instead of just plain old 285.
Here is my .h file for the accelerometer.
1. #ifndef __ADXL335_H__
2. #define __ADXL335_H__
3. #include <Arduino.h>
4. /*macro definitions of Analog read pins*/
5. #define X_AXIS_PIN A0
6. #define Y_AXIS_PIN A1
7. #define Z_AXIS_PIN A2
8. #define ADC_AMPLITUDE 1024//amplitude of the 10bit-ADC of Arduino is 1024LSB
9. #define ADC_REF 5 //ADC reference is 5v
10. #define ZERO_X 1.22 //accleration of X-AXIS is 0g, the voltage of X-AXIS is 1.22v
11. #define ZERO_Y 1.22 //
12. #define ZERO_Z 1.25 //
13. #define SENSITIVITY 0.25//sensitivity of X/Y/Z axis is 0.25v/g
14. class ADXL335
15. {
16. private:
17. void pinsInit();
18. float scale;
19. public:
20. void begin();
21. void getXYZ(int16_t *x,int16_t *y,int16_t *z);
22. void getAcceleration(float *ax,float *ay,float *az);
23. };
24. #endif
Below is the .cpp file of the accelerometer.
1. #include <Arduino.h>
2. #include "ADXL335.h"
3. void ADXL335::pinsInit()
4. {
5. pinMode(X_AXIS_PIN, INPUT);
6. pinMode(Y_AXIS_PIN, INPUT);
7. pinMode(Z_AXIS_PIN, INPUT);
8. }
9. void ADXL335::begin()
10. {
11. pinsInit();
12. scale = (float)SENSITIVITY*ADC_AMPLITUDE/ADC_REF;
13. }
14. void ADXL335::getXYZ(int16_t *x,int16_t *y,int16_t *z)
15. {
16. *x = analogRead(X_AXIS_PIN);
17. *y= analogRead(Y_AXIS_PIN);
18. *z = analogRead(Z_AXIS_PIN);
19. }
20. void ADXL335::getAcceleration(float *ax,float *ay,float *az)
21. {
22. int x,y,z;
23. float xvoltage,yvoltage,zvoltage;
24. getXYZ(&x,&y,&z);
25. xvoltage = (float)x*ADC_REF/ADC_AMPLITUDE;
26. yvoltage = (float)y*ADC_REF/ADC_AMPLITUDE;
27. zvoltage = (float)z*ADC_REF/ADC_AMPLITUDE;
28. Serial.println("voltage:");
29. Serial.println(xvoltage);
30. Serial.println(yvoltage);
31. Serial.println(zvoltage);
32. *ax = (xvoltage - ZERO_X)/SENSITIVITY;
33. *ay = (yvoltage - ZERO_Y)/SENSITIVITY;
34. *az = (zvoltage - ZERO_Z)/SENSITIVITY;
35.
36. }
Below is my version of the .cpp file. Yes, I want to go down into the underworld and change stuff such that my x value in the Serial monitor will be displayed to be atleast 3 places after the decimal.
I updated the lines 28 thru 32 from the original .cpp file.
The updates occur from line 12 thru 18 in my .cpp file below.
So for example instead of just plain old 285 being read from the Accelerometer, I THINK I will see something like 285.154 with 3 places after the decimal. ( Right now, the values change by 1 whole number increments or decrements.
Can someone please tell me if the following will give me x values in the Serial monitor that will be atleast 3 places after the decimal?
1. void ADXL335::getAcceleration(float *ax, float *ay, float *az)
2. {
3. int x, y, z;
4. float xvoltage, yvoltage, zvoltage;
5. getXYZ(&x, &y, &z);
6.
7. // Calculate voltage for each axis
8. xvoltage = (float)x * ADC_REF / ADC_AMPLITUDE;
9. yvoltage = (float)y * ADC_REF / ADC_AMPLITUDE;
10. zvoltage = (float)z * ADC_REF / ADC_AMPLITUDE;
11.
12. Serial.println("Voltage:");
13. Serial.print("X: ");
14. Serial.println(xvoltage, 4); // Print with 4 digits after the decimal point
15. Serial.print("Y: ");
16. Serial.println(yvoltage, 4); // Print with 4 digits after the decimal point
17. Serial.print("Z: ");
18. Serial.println(zvoltage, 4); // Print with 4 digits after the decimal point
19.
20. // Calculate acceleration for each axis
21. *ax = (xvoltage - ZERO_X) / SENSITIVITY;
22. *ay = (yvoltage - ZERO_Y) / SENSITIVITY;
23. *az = (zvoltage - ZERO_Z) / SENSITIVITY;
24. }
Thank you all for your replies!!