Arduino Uno+adxl335 help

Discussion in 'Programmer's Corner' started by neo1111, Jan 11, 2015.

  1. neo1111

    Thread Starter New Member

    Jan 11, 2014
    Hi there!
    I want to make a program that reads the movement on X and Y axis and turns a LED on.
    For mevement on X axis-turn on Red led and for movement on Y axis-turn on Yellow led.
    For the X axis it's ok but i can't find what's wrong for the Y axis.
    For the x axis , the program works practically but not for the Y axis.

    #define PRAG_X_1 0.5
    #define PRAG_X_2 1
    #define PRAG_X_3 2
    #define PRAG_Y_1 0.5
    #define PRAG_Y_2 1
    #define PRAG_Y_3 2
    //X and Y are the axis of the accelerometer; +/- 3 g it can detect; 0,5/1/2 are the steps
    #define SMOOTH_X 0.4
    #define SMOOTH_Y 0.4 //this is a filtering that the eye can detect the difference
    void setup(){
    pinMode(2, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(13, OUTPUT); //prin 2,8,13 as outputs
    digitalWrite(2, HIGH); // this pin will stay on -reprezenting the z axis
    } ]

    float oldAccX;
    float oldAccY;
    void loop() {
    float accX = smooth(readAcc(0), SMOOTH_X, oldAccX); //reads 2 measurements :x , y axis
    float accY = smooth(readAcc(1), SMOOTH_Y, oldAccY);
    if (accX < -PRAG_X_2){
    digitalWrite(13, HIGH);
    else {
    digitalWrite(13, LOW);
    if (accY < -PRAG_Y_2){
    digitalWrite(8, HIGH);
    else {
    digitalWrite(8, LOW);
    float readAcc(int port) {
    int value=analogRead(port); //reads the valure returned by the AD converter
    int miliVolts=map(value,0,,1023,0,3300)-3300/2; // transforms readed value in mV
    float acc=(float)miliVolts/360; //adxl generates a 360mV/g
    return acc; //returns that value of the acceleration
    float smooth(float data, float filterVal, float smoothedVal) {
    if (filterVal > 1) {
    filterVal = .99;
    else if (filterVal <= 0){
    filterVal = 0; //the bandwich is 100 MHz
    smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal);
    return (float)smoothedVal;
    ll DSC_0157.JPG DSC_0158.JPG
    The pictures are meant to illustrate that it's functionating for the x axis but not for the y axis.

    I wanna know what i must change in the if line for the 8 pin in order to turn a led on when it detects acceleration . I made myself some modifications but i wasn't succesful in my matter .
  2. neo1111

    Thread Starter New Member

    Jan 11, 2014
    Problem solved.
  3. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    And could you share how?