8 years ago.

MMA7455 can't do what i want

Did someone use this sensor before? This is my code

#include "mbed.h"
#include "MMA7455.h"
#define PI 3.14159265358979323846264

MMA7455 acc(p32,p31);
Serial pc(USBTX, USBRX);

int main() {
    int32_t x,y,z;
    double x1,y1,z1;
    double xg,yg,zg;
    double xd,yd,zd;
    
    acc.setMode(MMA7455::ModeMeasurement);
    acc.setRange(MMA7455::Range_2g);
    acc.calibrate();
    while(1)
    {
        acc.read(x,y,z);

        xg=x*0.016; 
        yg=y*0.016; 
        zg=z*0.016; 

        x1=xg/(sqrt(pow(yg,2.0)+pow(zg,2.0)));
        y1=yg/(sqrt(pow(xg,2.0)+pow(zg,2.0)));
        z1=(sqrt(pow(yg,2.0)+pow(xg,2.0)))/zg;

        xd=atan(x1);
        yd=atan(y1);
        zd=atan(z1); 

        pc.printf("xd: %.4f ",xd*180.0/PI);
        pc.printf("yd: %.4f ",yd*180.0/PI);
        pc.printf("zd: %.4f ",zd*180.0/PI);
        pc.printf("\r\n\n");
        wait(0.2);
    }
}

My problem is about when MMA7455 doesn't move. The degree can't keep 0 0 0 (x,y,z degree) It's the problem of my code or the sensor ? Thx very much!!

1 Answer

8 years ago.

if you print the xg,yg and zg, you may find they are all zero....

xg = double(x) * 0.016;

I have found this issue sometimes...

Thx your answer.So the problem is the sensor or my code?

posted by jajn HA 14 Nov 2016

Hi your code uses an int32 x 0.016 which I have found to be a little cumbersome results ie. int32 x int(0.016) = 0; I suggest that you make the variables, X,Y,Z Doubles

which may not be possible, so you must cast it to double. hence my answer....

xg = double(x) * 0.016; . .

posted by Nick Marsh 14 Nov 2016

Thanks! i will try later.

posted by jajn HA 15 Nov 2016