Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active December 3, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/ba20c373142a1e0451a245d5b2ed5381 to your computer and use it in GitHub Desktop.
Save carlosdelfino/ba20c373142a1e0451a245d5b2ed5381 to your computer and use it in GitHub Desktop.
// Vibration meter
// v1.0, June 2014
// GrMis - RCGROUPS
// Mais Informações visite: http://forum.arduino.cc/index.php?topic=225776.0 ou https://www.rcgroups.com/forums/showthread.php?2197784-DIY-vibration-meter-for-propeller-or-motor-balancing-%28etc-%29-Arduino-MMA7361
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR 0x20 // I2C for PCF8574A inside the MJKDZ
#define LED_OFF 0
#define LED_ON 1
//mjkdz i2c LCD board
LiquidCrystal_I2C lcd(I2C_ADDR, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE);
const int Pin_ACC_X=0;
const int Pin_ACC_Y=1;
const int Pin_ACC_Z=2;
int ax,ay,az;
double a2;
double ma2;
long int last_LCD=0;
long int last_acc=0;
bool led_on=false;
double MX=0,MY=0,MZ=0;
const double c1=0.002;// 1.0/c1 is the time scale for computing the average acceleration, in milli seconds.
const double c2=0.015;// 1.0/c2 is the time scale for computing the average vibration, in milli seconds.
const double scale=1.5/512;
double AX,AY,AZ;
int count=0;
void setup() {
delay(100);
lcd.begin (16,2); // Initialize the LCD (16 characters & 2 lines )
lcd.setBacklight(LED_OFF);
lcd.setCursor(0,0);
lcd.print("Vibration meter");
delay(1500);
lcd.clear();
analogReference(EXTERNAL);// 3.3 volts connected to aref.
// Initialize the average accceleration vector by some initial measurement
MX= 1.0*analogRead(Pin_ACC_X);
MY= 1.0*analogRead(Pin_ACC_Y);
MZ= 1.0*analogRead(Pin_ACC_Z);
}
void loop() {
if (micros()-last_acc>=900) {
last_acc=micros();
ax= analogRead(Pin_ACC_X);//-512+20;
ay= analogRead(Pin_ACC_Y);//-512-30;
az= analogRead(Pin_ACC_Z);//-512+70;
// MX,MY,XZ are the components of the average acceleration
MX=c1*ax+(1.-c1)*MX;
MY=c1*ay+(1.-c1)*MY;
MZ=c1*az+(1.-c1)*MZ;
// AX,AY,AZ are the components of the deviation from the average acceleration
AX=1.0*ax-MX;
AY=1.0*ay-MY;
AZ=1.0*az-MZ;
//Square of the deviation
a2= AX*AX+AY*AY+AZ*AZ;
//ma2 is the average of the square of the deviation
ma2=c2*a2+(1.-c2)*ma2;
//count++;
}
if (millis()-last_LCD>=300) {
last_LCD=millis();
lcd.clear();
const double g=sqrt(ma2)*scale;
lcd.setCursor(0,0);lcd.print("|dA|=");lcd.print(g,3);
//lcd.setCursor(0,1);lcd.print("Sampl.:");lcd.print(1.0*count/0.3,0);lcd.print("Hz");
//count=0;
if (g>2) {//Shake !
led_on=!led_on;
if (led_on) {lcd.setBacklight(LED_ON);} else {lcd.setBacklight(LED_OFF);}
}
}
}
// Vibration meter
// v1.0, June 2014
// GrMis - RCGROUPS
#define LED_OFF 0
#define LED_ON 1
const int Pin_ACC_X=0;
const int Pin_ACC_Y=1;
const int Pin_ACC_Z=2;
int ax,ay,az;
double a2;
double ma2;
long int last_acc=0;
bool led_on=false;
double MX=0,MY=0,MZ=0;
const double c1=0.002;// 1.0/c1 is the time scale for computing the average acceleration, in milli seconds.
const double c2=0.015;// 1.0/c2 is the time scale for computing the average vibration, in milli seconds.
const double scale=1.5/512;
double AX,AY,AZ;
int count=0;
void setup() {
delay(100);
analogReference(EXTERNAL);// 3.3 volts connected to aref.
// Initialize the average accceleration vector by some initial measurement
MX= 1.0*analogRead(Pin_ACC_X);
MY= 1.0*analogRead(Pin_ACC_Y);
MZ= 1.0*analogRead(Pin_ACC_Z);
}
void loop() {
if (micros()-last_acc>=900) {
last_acc=micros();
ax= analogRead(Pin_ACC_X);//-512+20;
ay= analogRead(Pin_ACC_Y);//-512-30;
az= analogRead(Pin_ACC_Z);//-512+70;
// MX,MY,XZ are the components of the average acceleration
MX=c1*ax+(1.-c1)*MX;
MY=c1*ay+(1.-c1)*MY;
MZ=c1*az+(1.-c1)*MZ;
// AX,AY,AZ are the components of the deviation from the average acceleration
AX=1.0*ax-MX;
AY=1.0*ay-MY;
AZ=1.0*az-MZ;
//Square of the deviation
a2= AX*AX+AY*AY+AZ*AZ;
//ma2 is the average of the square of the deviation
ma2=c2*a2+(1.-c2)*ma2;
//count++;
}
if ((sqrt(ma2)*scale)>2) //Shake !
digitalWrite(LED13,!led_on);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment