Hi,
I have four ADXL335 Module 3-Axis Analog Output Accelerometer.
I also have an Arduino UNO and the sparkcore.
The x,y and z outputs on the ADXL335 are connected to A0-A2 on UNO and sparkcore. The Vin On ADXL335 is connected to the 3.3v on both boards, the GND is also connected.
The following code runs fine and gives accurate output on the UNO, but when I run the same code on the sparkcore, the output is wacky.
Here is the code (source: http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/):
//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////
//Analog read pins
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;
//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 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(xRead);
Serial.print("): ");
Serial.print(x);
Serial.print(" y(");
Serial.print(yRead);
Serial.print("): ");
Serial.print(y);
Serial.print(" z(");
Serial.print(zRead);
Serial.print("): ");
Serial.println(z);
delay(3000);//just here to slow down the serial output - Easier to read
}
Note that both ADXL335 are taped to my desk so they don’t move.
here is the output from arduino:
x(326): 189.87 y(325): 188.25 z(281): 230.19
x(326): 189.59 y(325): 188.02 z(280): 230.19
x(326): 189.59 y(325): 188.02 z(280): 230.19
x(326): 189.87 y(325): 188.25 z(281): 230.19
x(326): 189.87 y(325): 188.25 z(281): 230.19
x(326): 189.87 y(325): 188.25 z(281): 230.19
x(326): 189.87 y(325): 188.25 z(281): 230.19
And here is the output from sparkcore ((Notice how the values are fluctuating):
x(440.00): 45.44 y(435.00): 47.53 z(433.00): 42.91
x(419.00): 49.90 y(425.00): 47.29 z(415.00): 47.63
x(420.00): 51.71 y(425.00): 49.67 z(412.00): 47.08
x(441.00): 35.87 y(414.00): 47.92 z(433.00): 33.14
x(440.00): 31.22 y(406.00): 47.09 z(434.00): 29.40
x(439.00): 45.00 y(439.00): 45.00 z(439.00): 45.00
x(439.00): 34.09 y(411.00): 47.12 z(433.00): 32.15
x(439.00): 33.69 y(411.00): 46.68 z(434.00): 32.15
x(439.00): 31.36 y(405.00): 47.56 z(432.00): 29.12
x(441.00): 43.18 y(429.00): 47.92 z(433.00): 40.27
x(441.00): 47.12 y(439.00): 47.92 z(433.00): 44.19
FYI, I switch the ADXL335 and still same results.
I added 0.1 MF cap for each A0-A2 to ground and that did not help eaither.
Does anyone know why the readings are so different and why the values are fluctuating?
Could someone point me to the correct direction how to fix this please?
thanks,
/N