• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

understanding code: accelerometer with arduino

meowth08

Member
Code:
#define ANALOG0 14

class Accelerometer
{
    int p[3]; // which analog pins
    int a[3]; // acceleration, zero-based
    int b[3]; // acceleration bias/calibration information
    int g, t, r; // cached copies of calculations
    int scale; // scaling factor between ADC and gravity

public:
    Accelerometer(int pinX, int pinY, int pinZ, int pinVCC, int pinGND)
    {
        pinMode(pinGND + ANALOG0, OUTPUT); digitalWrite(pinGND + ANALOG0, LOW);
        pinMode(pinVCC + ANALOG0, OUTPUT); digitalWrite(pinVCC + ANALOG0, HIGH);
        pinMode((p[0] = pinX) + ANALOG0, INPUT);
        pinMode((p[1] = pinY) + ANALOG0, INPUT);
        pinMode((p[2] = pinZ) + ANALOG0, INPUT);
        for (int i = 0; i < 3; i++)
            b[i] = 512;
        g = t = r = 0;
        scale = 100;
    }

    void update()
    {
        for (int i = 0; i < 3; i++)
            a[i] = analogRead(p[i]) - b[i];
        g = t = r = 0;
    }

    void dump()
    {
        Serial.print(  "x="); Serial.print(a[0]);
        Serial.print("\ty="); Serial.print(a[1]);
        Serial.print("\tz="); Serial.print(a[2]);
        Serial.print("\tmg="); Serial.print(milligee());
        Serial.print("\tpitch="); Serial.print(pitch());
        Serial.print("\troll="); Serial.print(roll());
        Serial.println();
    }

    void calibrate()
    {
        for (int i = 0; i < 3; i++)
            b[i] = analogRead(p[i]);
        b[2] -= scale;
        update();
    }

    int milligee()
    {
        if (g != 0) return g;
        long squared = 0.0;
        for (int i = 0; i < 3; i++)
            squared += (long)a[i] * (long)a[i];
        g = squared * 1000 / (scale*scale);
        return g;
    }

    int accel(int axis)
    {
        if (axis < 0 || axis > 3) return 0;
        return a[axis];
    }

    int roll()
    {
        if (r != 0) return r;
        r = (int)(atan2(a[0], a[2]) * 180. / M_PI);
        return r;
    }

    int pitch()
    {
        if (t != 0) return t;
        t = (int)(acos(a[1] / (float)scale) * 180. / M_PI);
        return t;
    }

    void loop()
    {
        update();
    }
};
Hi,
I copied this code from the net.
Kindly add some comments on what each part of the code is for.
I am having a hard time understanding C.

Thanks in advance.
m8
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I am having a hard time understanding C.
Unfortunately this is worse... Its C++... If you don't grasp C++ ( Object Orientated ) It won't be easy to explain

Some comments added
C:
#define ANALOG0 14
 
class Accelerometer				/// Name of the class
{
	// the following are class variables ( private and can't be seen outside the scope of the class)
    int p[3]; // which analog pins
    int a[3]; // acceleration, zero-based
    int b[3]; // acceleration bias/calibration information
    int g, t, r; // cached copies of calculations
    int scale; // scaling factor between ADC and gravity
 
public:		// All these functions below WILL be seen outside of the class scope.
    Accelerometer(int pinX, int pinY, int pinZ, int pinVCC, int pinGND)
    {
        pinMode(pinGND + ANALOG0, OUTPUT); digitalWrite(pinGND + ANALOG0, LOW);
        pinMode(pinVCC + ANALOG0, OUTPUT); digitalWrite(pinVCC + ANALOG0, HIGH);
        pinMode((p[0] = pinX) + ANALOG0, INPUT);
        pinMode((p[1] = pinY) + ANALOG0, INPUT);
        pinMode((p[2] = pinZ) + ANALOG0, INPUT);     // pinmode is a library function
        for (int i = 0; i < 3; i++)
            b[i] = 512;
        g = t = r = 0;
        scale = 100;
    }
 
    void update()	//  This can be called to refresh all the above variables.
    {
        for (int i = 0; i < 3; i++)
            a[i] = analogRead(p[i]) - b[i];		// analogRead is a library function
        g = t = r = 0;
    }
 
    void dump()		// this is called to print the variables
    {
        Serial.print(  "x="); Serial.print(a[0]);		// serial is the serial class. Print is one of the functions
        Serial.print("\ty="); Serial.print(a[1]);
        Serial.print("\tz="); Serial.print(a[2]);
        Serial.print("\tmg="); Serial.print(milligee());
        Serial.print("\tpitch="); Serial.print(pitch());
        Serial.print("\troll="); Serial.print(roll());
        Serial.println();
    }
 
    void calibrate()	// Again.. Called to calibrate the accelerometer
    {
        for (int i = 0; i < 3; i++)
            b[i] = analogRead(p[i]);	
        b[2] -= scale;
        update();
    }
 
    int milligee()		// returns the G force 
    {
        if (g != 0) return g;
        long squared = 0.0;
        for (int i = 0; i < 3; i++)
            squared += (long)a[i] * (long)a[i];
        g = squared * 1000 / (scale*scale);
        return g;
    }
 
    int accel(int axis)	// this returns the acceration as an Int
    {
        if (axis < 0 || axis > 3) return 0;
        return a[axis];
    }
 
    int roll() 	// this returns the roll as an Int
    {
        if (r != 0) return r;
        r = (int)(atan2(a[0], a[2]) * 180. / M_PI);
        return r;
    }
 
    int pitch()		// this returns the pitch as an Int
    {
        if (t != 0) return t;
        t = (int)(acos(a[1] / (float)scale) * 180. / M_PI);
        return t;
    }
 
    void loop()		
    {
        update();
    }
 

meowth08

Member
Hi,

Thanks for the reply.
I think it would be hard if I start with something I don't understand.
I'll try to post the actual problem I have later.
But I'll try my best to make my own code.
 

Latest threads

EE World Online Articles

Loading

 
Top