ADXL335 Twitchy Readings

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
niknak35
 
Posts: 53
Joined: Sat Apr 26, 2014 10:59 pm

ADXL335 Twitchy Readings

Post by niknak35 »

Hi
I've made the following sketch to measure the 'Rotational Speed' with the ADXL335. I'm getting odd results. Sometime when I run the sketch it works well and the rotational speed returns 0 when stationary and slowly increases as it rotates. Other times the values are jumpy when the sensor is still. Is it a calibration problem?
Cheers
Nick

Code: Select all

int zPin = A1; //accelerometer z-axis on pin A0
int yPin = A2; //accelerometer y-axis on pin A1
int xPin = A3; //accelerometer x-axis on pin A2

int z;  //z angle
int y;  //y angle
int x;  //x angle
int q; //delay time
float rotY; //y rotational speed



int minVal = 390;
int maxVal = 620;

#include <Adafruit_NeoPixel.h> //allows recognition of codes from the adafruit NeoPixel library
#define pix 20

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(pix, 6, NEO_GRB + NEO_KHZ800); //left
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(pix, 7, NEO_GRB + NEO_KHZ800); //right*
const int numReadings = 5;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int averageY = 0;                // the average


void setup()
{
  strip2.begin();
  strip2.show();// initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
}

void loop() {


  int yReadOld = analogRead(yPin);
//  Serial.print("old y: "); 
//  Serial.println(yReadOld);
  delay(1);
  int yReadNew = analogRead(yPin);
//  Serial.print("New y: "); 
//  Serial.println(yReadNew);
 int rotY = abs(yReadNew - yReadOld);
//    Serial.print("rotY=: "); 
//  Serial.println(rotY);
  
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = rotY; 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  averageY = total / numReadings;    //average rotational speed of y     
  // send it to the computer as ASCII digits
 Serial.println(averageY);   
//delay(1);        // delay in between reads for stability      
if (averageY >=5&&averageY<30)
{
theaterChase(strip2.Color(255, 255, 255), 1);
}


else
theaterChase(strip2.Color(0, 0, 10), 1);


}
 
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip2.numPixels(); i++) {
      strip2.setPixelColor(i, c);
      strip2.show();
      delay(wait);
  }
}
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<1; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip2.numPixels(); i=i+3) {
        strip2.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip2.show();
     
      delay(wait);
     
      for (int i=0; i < strip2.numPixels(); i=i+3) {
        strip2.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL335 Twitchy Readings

Post by adafruit_support_bill »

The ADXL335 is an accelerometer which measures linear acceleration. Your code appears to be assuming a Gyro sensor which measures rotation.

User avatar
newbie3
 
Posts: 4
Joined: Mon Apr 13, 2015 7:03 pm

Re: ADXL335 Twitchy Readings

Post by newbie3 »

Hi, I'm trying to use the ADXL335 to get rate/speed of walking. Having trouble isolating the reading to get accurate code to measure speed of walking.

Scope of project: physical object output vibration when a person walks 1/3 faster than their rate of normal walking speed. Trying to get a read of linear path walking. How do isolate the different axis?

**Also, looking for equations to deduce velocity from ac + time. Anyone?

Part of the difficulty is that the rock can rotate randomly and also move randomly in the hand...while walking, this (to my limited math knowledge now) adds serious difficulties to finally know if you are walking and what speed.

Much Appreciated if anyone has experience and can help to figure out the above questions.

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL335 Twitchy Readings

Post by adafruit_support_bill »

You can estimate velocity by integrating the acceleration over time. Since acceleration is measured as the change in meters per second per second, your instantaneous velocity is the sum of the acceleration rate divided by the sample rate. For example: If you are sampling at 100 times per second, you divide each reading by 100 and add it to the running total.

This is good for short term measurements, but as you might imagine, the errors will accumulate over time and your measurement will drift - especially if you have a noisy signal due to the other motions involved in walking.. For this reason, accelerometer data is typically combined with GPS data to measure velocity over the long term.

User avatar
niknak35
 
Posts: 53
Joined: Sat Apr 26, 2014 10:59 pm

Re: ADXL335 Twitchy Readings

Post by niknak35 »

Thank Bill
Looks Like a Gryo/Accel combo is going to give more accurate data.
cheers
Nick

User avatar
newbie3
 
Posts: 4
Joined: Mon Apr 13, 2015 7:03 pm

Re: ADXL335 Twitchy Readings

Post by newbie3 »

Thank you Bill.

How do you incorporate GPS with the accelerometer? Right now GPS does not work indoor and it takes approx. 7 to 10 minutes to initialize signal. Therefore, was hoping not to use GPS but from your email you are recommending it will help. Can you elaborate on this?

Will be using gyro to activate spin/rotate object for different interaction to toggle through different modes on the physical object.

Thank you!
Newbie

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: ADXL335 Twitchy Readings

Post by adafruit_support_bill »

The accelerometers are good at measuring instantaneous changes in speed, but integrating the complex signals from walking motion, I would expect the errors to accumulate rather quickly. GPS suffers from the opposite problem. It doesn't have any cumulative error, but the precision is rather low. So the usual approach is to combine the two through a filter such as a Kalman Filter or Complementary Filter. There are plenty of papers on the subject.

http://www.eeng.nuim.ie/~jringwood/Respubs/C198TOKS.pdf
http://www.moog-crossbow.com/Literature ... PS-IMU.pdf

If you search the web, you might find some code for it. I'd check over DIY Drones, since they do a lot of IMU/GPS navigation work.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”