VIBRATION DETECTOR ADXL355

Vib Det

const int groundpin = 18; // analog input pin 4 — ground

const int powerpin = 19; // analog input pin 5 — voltage

const int xpin = A3; // x-axis of the accelerometer

const int ypin = A2; // y-axis

const int zpin = A1;// z-axis (only on 3-axis models)

float cx=0;

float cy=0;

float cz=0;

float a;

float b;

float c;

float d;

float x;

float y;

float z;

void setup()

{

pinMode(13, OUTPUT);

// initialize the serial communications:

Serial.begin(9600);

pinMode(groundpin, OUTPUT);

pinMode(powerpin, OUTPUT);

digitalWrite(groundpin, LOW);

digitalWrite(powerpin, HIGH);

delay(6000);

x=analogRead(xpin);

y=analogRead(ypin);

z=analogRead(zpin);

if ((x >= cx)||(x < cx)) {

cx = x;

}

if ((y >= cy)||(y < cy)) {

cy = y;

}

if ((z >= cz)||(z < cz)) {

cz = z;

}

delay(2000);

}

void loop()

{

a=analogRead(xpin)-cx;

b=analogRead(ypin)-cy;

c=analogRead(zpin)-cz;

d=sqrt(sq(a)+sq(b)+sq(c));

// print the sensor values:

if (d > 2.5){

digitalWrite(13, HIGH);

delay(2000);

}

else {

Serial.print(a);

// print a tab between values:

Serial.print(“\t”);

Serial.print(b);

// print a tab between values:

Serial.print(“\t”);

Serial.print(c);

Serial.print(“\t”);

Serial.print(d);

Serial.println();

digitalWrite(13, LOW);

}

}