Show Posts
Pages: [1]
1  Using Arduino / Sensors / Re: Library for accelerometer functions on: April 20, 2011, 07:08:41 pm
Hello every1!
its been long.Below is the code.It gives an error of about 10 degrees with real life investigation.NOW I AM TRYING TO STORE AND READ THIS FROM THE BUILT-IN EEPROMOF THE ARDUINO. I AM HAVING TROUBLE WITH 10Bit to 8 bit conversion. I understand that I will have to divide it by '4' according to the example given with arduino0022. But I cant read the values in 10 bit format smiley-sad.Pleae shout out if any1 can help.

 The circuit:
 analog 0: accelerometer self test
 analog 1: z-axis
 analog 2: y-axis
 analog 3: x-axis
 analog 4: ground
 analog 5: vcc


// these constants describe the pins. They won't change:
const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
double xpin = A1;                  // x-axis of the accelerometer
double ypin = A2;                  // y-axis
double zpin = A3;                  // z-axis (only on 3-axis models)
const float arduino_power_supply = 5;
const float sensor_power_supply = 5;
const float zero_g_bias = sensor_power_supply / 2;
void setup()
  // initialize the serial communications:
  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);

void loop()
  // print the sensor values:
    float voltage_x = (analogRead(xpin)) * arduino_power_supply / 1024;
  float x = (voltage_x - zero_g_bias) * 1000 / 330;
float Aa = (analogRead(xpin));
float Bb =(analogRead(xpin)-503);
float Cc =(Bb/150);
float Dd =(tan(Cc)*x);
float xangle=((Dd)*(180/PI));
  // print a tab between values:
  float voltage_y = (analogRead(ypin)) * arduino_power_supply / 1024;
  float y = (voltage_y - zero_g_bias) * 1000 / 330;
double A=(analogRead(ypin));
double B=(A-509);
double C=((B/-154));
double D=(tan(C));
double E=(((D)*(180/PI))*y);
  // print a tab between values:
  float voltage_z = (analogRead(zpin)) * arduino_power_supply / 1024;
  float z = (voltage_z - zero_g_bias) * 1000 / 330;
 float Aaa = (analogRead(zpin));
float Bbb =(analogRead(zpin)-580);
float Ccc =(Bbb/190);
float Ddd =(tan(Ccc)*2);
float zangle=((Ddd)*(180/PI)*z);


  Serial.print ("\t");

  Serial.print ("\t");

  // delay before next reading:
2  Using Arduino / Sensors / Re: Library for accelerometer functions on: March 24, 2011, 08:18:29 pm
Hi there!
Is there a way you could use the serial monitor to display string of angles in degrees with your code. I am a total newbee,so your help will be greatly appreciated.

Cheers smiley-confuse
3  Using Arduino / Programming Questions / Re: lilypad arduino and lilypad adxl335 accelerometer measurement of a true angle. on: March 24, 2011, 08:06:10 pm
Hi there!
Thnx again.As I might have mentioned earlier,I can get the raw data(acceleration) easy...but getting angles of x,y, and z is where I am stuck at... smiley-confuse
4  Using Arduino / Programming Questions / Re: lilypad arduino and lilypad adxl335 accelerometer measurement of a true angle. on: March 24, 2011, 07:28:37 pm
int pwrPin=18;                 // not initialized in case not used
int gndPin=19;                 // "
int xPin=A1;                   // variable to set x pin
int yPin=A2;                   // y pin
int zPin=A3;                   // z pin
                 // self test pin, build does not use

long x, y, z;               // hold raw x,y,z reading from sensor
int xAngle, yAngle, zAngle; // hold angle conversions
const int samples = 10;     // number of samples for error
int readingX[samples];      // array of x samples
int readingY[samples];      // y
int readingZ[samples];      // z
int neutral = 3;            // variable that will hold the deadspace for quadrants
int sensorMax = 470;          // holds specification for max sensor range
int sensorMin = 550;          // holds specification for min sensor range

// not sure if this will work because the pwrPin and gndPin were not initialized and since this
// only runs once........
void setup()
    pinMode(pwrPin, OUTPUT);
    digitalWrite(pwrPin, HIGH);
    pinMode(gndPin, OUTPUT);
    digitalWrite(gndPin, LOW);

void loop(){
  // read in (x) samples into the array
  for (int i=0; i<samples; i++){
    readingX = analogRead(xPin);
    readingY = analogRead(yPin);
    readingZ = analogRead(zPin);
  // sum up all the array for averaging
  for (int i=0; i<samples; i++){
    x = x + readingX;
    y = y + readingY;
    z = z + readingZ;
  // spit out the averages
  x = x/samples;
  y = y/samples;
  z = z/samples;
// returns the quadrant that the accelerometer is indicating, quadrants start in top right and go counter clockwise
int getTiltQuadrant(){
  if(x>=neutral && y>=neutral)return 1;
  if(x>-neutral && x<neutral && y>neutral)return 2;
  if(x<=neutral && y>=neutral)return 3;
  if(x<=-neutral && y>-neutral && y<neutral) return 4;
  if(x<-neutral && y<=-neutral) return 5;
  if(x>-neutral && x<neutral && y<-neutral) return 6;
  if(x>=neutral && y<=neutral) return 7; 
  if(x>=neutral && y>-neutral && y<neutral) return 8;

int getQuadrantTiltAngle(){
  if (x>0 && y>0) return atan(x/y);
  if (x>0 && y<0) return atan((x/y)+180);
  if (x<0 && y<0) return atan((x/y)+180);
  if (x<0 && y>0) return atan((x/y)+360);
// get the angle of the x in relation to plane
int getXAngle(){
  xAngle = map(x, sensorMin, sensorMax, -90, 90);
  return xAngle; 

// y axis
int getYAngle(){
  yAngle = map(y, sensorMin, sensorMax, -90, 90);
  return yAngle;
// z axis
int getZAngle(){
  zAngle = map(z, 662, 355, -90, 90);
  return zAngle;
// setters and getters section
int getX(){
  return x;

int getY(){
  return y;

int getZ(){
  return z;

void setXPin(int pin){
  xPin = 3;

void setYPin(int pin){
  yPin = 2;

void setZPin(int pin){
  zPin = 1;

void setPwrPin(int pin){
  pwrPin = 18;

void setGndPin(int pin){
  gndPin =19;

void setNeutral(int neutral){
  neutral = neutral;

void setSensorMax(int maxS){
  sensorMax = maxS;

void setSenorMin(int minS){
  sensorMin = minS;

  // print the sensor values:
5  Using Arduino / Programming Questions / Re: lilypad arduino and lilypad adxl335 accelerometer measurement of a true angle. on: March 24, 2011, 06:38:35 pm
Below is the code I am using at the moment,But I can't see anything in the serial monitor window  smiley-sad...Please help!!!
6  Using Arduino / Programming Questions / Re: lilypad arduino and lilypad adxl335 accelerometer measurement of a true angle. on: March 24, 2011, 06:03:50 pm
Thanks 4 ur reply.I know what a vector is.I know we have to get the resultant of all three axes. I do not have much problem understanding how it is calculated. I am kinda weak in programming.I do not know how to write the code so serial monitor will display it as a string.Any ideas?
7  Using Arduino / Programming Questions / lilypad arduino and lilypad adxl335 accelerometer measurement of a true angle. on: March 24, 2011, 05:24:07 pm
Hello every1!! smiley
This is my first ever post and I am an absolute newbee to arduino...I am trying to measure true angles of the 'x' 'y' and 'z' axis at the moment what I get is only 'g' values I am a bit confused about the coding.I dont know how to convert this values to get the angle values display by the serial monitor.Would anyone please care to assist me with this. I will be ever so helpful for your help..

Cheers smiley-confuse
Pages: [1]