Pages: [1]   Go Down
Author Topic: Using several ADXL335 accelerometers  (Read 718 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I try to read data from 4 ADXL335 accelerometers placed on various points of a robotics arm. The acquisition of a single accelerometer takes place very well, even by connecting the others. The problem is when I want to read data from a second accelerometer at the same time, the data become then totally inconsistent....
Has Anybody already realized this kind of project ?
Is there something particular to add/change in this code?

Thanks a lot.

Code:
/*
 ADXL335
*/
// these constants describe the pins.
const int xpin1 = A0;                  // x-axis of the accelerometer#1
const int ypin1 = A1;                  // y-axis
const int zpin1 = A2;                  // z-axis
const int xpin2 = A3;                  // x-axis of the accelerometer #2
const int ypin2 = A4;                  // y-axis
const int zpin2 = A5;                  // z-axis
//const int xpin3 = A6;                  // x-axis of the accelerometer #3
//const int ypin3 = A7;                  // y-axis
//const int zpin3 = A8;                  // z-axis

void setup()
{
  Serial.begin(115200);
}

void loop()
{
 
  unsigned long tmp1;
  unsigned char h1, l1, h2, l2, h3, l3;
  tmp1=analogRead(xpin1);
  h1=(unsigned char)(tmp1>>8);
  l1=(unsigned char)(tmp1 & ((1<<8)-1));
  Serial.print(l1,BYTE);
  Serial.print(h1,BYTE);
  unsigned long tmp2;
  tmp2=analogRead(ypin1);
  h2=(unsigned char)(tmp2>>8);
  l2=(unsigned char)(tmp2 & ((1<<8)-1));
  Serial.print(l2,BYTE);
  Serial.print(h2,BYTE);
  unsigned long tmp3;
  tmp3=analogRead(zpin1);
  h3=(unsigned char)(tmp3>>8);
  l3=(unsigned char)(tmp3 & ((1<<8)-1));
  Serial.print(l3,BYTE);
  Serial.print(h3,BYTE);
 
  unsigned long tmp4;
  unsigned char h4, l4, h5, l5, h6, l6;
  tmp4=analogRead(xpin2);
  h4=(unsigned char)(tmp4>>8);
  l4=(unsigned char)(tmp4 & ((1<<8)-1));
  Serial.print(l4,BYTE);
  Serial.print(h4,BYTE);
  delay(10);
  unsigned long tmp5;
  tmp5=analogRead(ypin2);
  h5=(unsigned char)(tmp5>>8);
  l5=(unsigned char)(tmp5 & ((1<<8)-1));
  Serial.print(l5,BYTE);
  Serial.print(h5,BYTE);
  delay(10);
  unsigned long tmp6;
  tmp6=analogRead(zpin2);
  h6=(unsigned char)(tmp6>>8);
  l6=(unsigned char)(tmp6 & ((1<<8)-1));
  Serial.print(l6,BYTE);
  Serial.print(h6,BYTE);
 
  /*
  unsigned long tmp3;
  unsigned char h3, l3;
  tmp3=analogRead(xpin3);
  h3=(unsigned char)(tmp3>>8);
  l3=(unsigned char)(tmp3 & ((1<<8)-1));
  Serial.print(l3,BYTE);
  Serial.print(h3,BYTE);
  tmp3=analogRead(ypin3);
  h3=(unsigned char)(tmp3>>8);
  l3=(unsigned char)(tmp3 & ((1<<8)-1));
  Serial.print(l3,BYTE);
  Serial.print(h3,BYTE);
  tmp3=analogRead(zpin3);
  h3=(unsigned char)(tmp3>>8);
  l3=(unsigned char)(tmp3 & ((1<<8)-1));
  Serial.print(l3,BYTE);
  Serial.print(h3,BYTE);
  */
  Serial.println();
 
//  delay(1);
}

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In fact here's my new code and data traces obtained with matlab :

Code:
/*
 ADXL335
 */
const int xpin1 = A0;                  // x-axis of the accelerometer#1
const int ypin1 = A1;                  // y-axis
const int zpin1 = A2;                  // z-axis
const int xpin2 = A3;                  // x-axis of the accelerometer #2
const int ypin2 = A4;                  // y-axis
const int zpin2 = A5;                  // z-axis

void setup()
{
  Serial.begin(115200);
}

void loop()
{
 
  unsigned long tmp1;
  unsigned char h1, l1, h2, l2, h3, l3;
  tmp1=analogRead(xpin1);
  h1=(unsigned char)(tmp1>>8);
  l1=(unsigned char)(tmp1 & ((1<<8)-1));
  Serial.print(l1,BYTE);
  Serial.print(h1,BYTE);
  unsigned long tmp2;
  tmp2=analogRead(ypin1);
  h2=(unsigned char)(tmp2>>8);
  l2=(unsigned char)(tmp2 & ((1<<8)-1));
  Serial.print(l2,BYTE);
  Serial.print(h2,BYTE);
  unsigned long tmp3;
  tmp3=analogRead(zpin1);
  h3=(unsigned char)(tmp3>>8);
  l3=(unsigned char)(tmp3 & ((1<<8)-1));
  Serial.print(l3,BYTE);
  Serial.print(h3,BYTE);
 
  unsigned long tmp4;
  unsigned char h4, l4, h5, l5, h6, l6;
  tmp4=analogRead(xpin2);
  h4=(unsigned char)(tmp4>>8);
  l4=(unsigned char)(tmp4 & ((1<<8)-1));
  Serial.print(l4,BYTE);
  Serial.print(h4,BYTE);
  unsigned long tmp5;
  tmp5=analogRead(ypin2);
  h5=(unsigned char)(tmp5>>8);
  l5=(unsigned char)(tmp5 & ((1<<8)-1));
  Serial.print(l5,BYTE);
  Serial.print(h5,BYTE);
  unsigned long tmp6;
  tmp6=analogRead(zpin2);
  h6=(unsigned char)(tmp6>>8);
  l6=(unsigned char)(tmp6 & ((1<<8)-1));
  Serial.print(l6,BYTE);
  Serial.print(h6,BYTE);
  Serial.println();
 
delay(1);
}



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I really don't understand this style of programming - read a sensor, print the value, read a sensor, print the value...

Why not read all three sensors as close as possible in time, i.e. consecutively, then print out the results?
(loops and arrays could help there)
Code:
/*
 ADXL335
 */
const int N_READINGS = 6;
const int aPin [N_READINGS] = {A0, A1, A2, A3, A4, A5};

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  int reading [N_READINGS];

  for (int i = 0; i < N_READINGS; ++i) {
    reading [i] = analogRead (aPin [i]);
  }

  for (int i = 0; i < N_READINGS; ++i) {
    Serial.print((unsigned char)(reading [i] & 255), BYTE);  // low byte
    Serial.print((unsigned char)(reading [i] >> 8), BYTE);   // high byte
  }

  delay (1);
}
« Last Edit: May 13, 2011, 04:48:46 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: