Jump to content
I2Cdevlib Forums

Motion Alarm with UNO R3 + GY-521 (MPU6050) - Projects Ideas


Recommended Posts

This thread is just to ideas and other relevant info on this project

 

My idea is to create an advanced version of a motion / shock sensor alarm use.
(Motion = Accel & Shock = Gyro)

A bit like NorthQ NQ-10920 - G-sensor or NQ-10030, but more advanced.

G-Sensor Only - NQ-10920: http://northq.linux01.dandomain.dk/products/alarm/nq10920.html

NQ-10030 is a set with G-sensor, remote control + Sound Unit !!
A set - NQ-10030: http://northq.linux01.dandomain.dk/products/alarm/nq10030.html

I did a demo code for this, but it is only the beginning.This I upload later, in a post on this thread.

 

 

 

Make your bid / ideas about this project.

 

All ideas and otherwise are welcome.

Regards.Nicolai

Link to comment
Share on other sites


// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class

// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>

// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib

//

// Changelog:

// 2011-10-07 - initial release

/* ============================================

I2Cdev device library code is placed under the MIT license

Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in

all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

THE SOFTWARE.

===============================================

*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation

// is used in I2Cdev.h

#include "Wire.h"

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files

// for both classes must be in the include path of your project

#include "I2Cdev.h"

#include "MPU6050.h"

// class default I2C address is 0x68

// specific I2C addresses may be passed as a parameter here

// AD0 low = 0x68 (default for InvenSense evaluation board)

// AD0 high = 0x69

MPU6050 accelgyro;

int16_t ax, ay, az;

int16_t gx, gy, gz;

const int buttonPin = 12;

int buttonState = 0;

int alarm_Accel = 7;

int alarm_Gyro = 8;

int updateT = 500*1;

int logNumA = 0;

char command = 0;

double dT;

//******************************************************************************

// Acceleration Alarm Set - X,Y,Z

int ax_High = 20000; // Acceleration X - HIGH +

int ax_Low = -20000;

int ay_High = 20000; // Acceleration Y - HIGH +

int ay_Low = -20000;

int az_High = 20000; // Acceleration Z - HIGH +

int az_Low = -20000;

// *********************************************

// Gyro Alarm Set - X,Y,Z

int gx_High = 20000; // Gyro X - HIGH +

int gx_Low = -20000;

int gy_High = 20000; // Gyro Y - HIGH +

int gy_Low = -20000;

int gz_High = 20000; // Gyro Z - HIGH +

int gz_Low = -20000;

//******************************************************************************

void setup() {

// join I2C bus (I2Cdev library doesn't do this automatically)

Wire.begin();

// initialize serial communication

// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but

// it's really up to you depending on your project)

Serial.begin(115200);

// initialize device

Serial.println("Initializing I2C devices...");

accelgyro.initialize();

// verify connection

Serial.println("Testing device connections...");

Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

// configure Arduino LED for

pinMode(alarm_Accel, OUTPUT);

pinMode(alarm_Gyro, OUTPUT);

pinMode(buttonPin, INPUT);

accelgyro.resetSensors();

accelgyro.setXAccelOffset(0);

accelgyro.setYAccelOffset(0);

accelgyro.setZAccelOffset(0);

accelgyro.setXGyroOffset(0);

accelgyro.setYGyroOffset(0);

accelgyro.setZGyroOffset(0);

delay(500);

}

void loop() {

read_switch();

read_serial();

read_Temp();

delay(updateT);

}

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void read_serial() {

if(Serial.available() > 0){

command = Serial.read();

switch(command){

case 'o':

Serial.println("***********************************");

Serial.println("Read Current Offset");

Serial.println(" ");

Serial.print("Accel X Offset: "); Serial.println(accelgyro.getXAccelOffset());

Serial.print("Accel Y Offset: "); Serial.println(accelgyro.getYAccelOffset());

Serial.print("Accel Z Offset: "); Serial.println(accelgyro.getZAccelOffset());

Serial.println(" ");

Serial.print("Gyro X Offset: "); Serial.println(accelgyro.getXGyroOffset());

Serial.print("Gyro Y Offset: "); Serial.println(accelgyro.getYGyroOffset());

Serial.print("Gyro Z Offset: "); Serial.println(accelgyro.getZGyroOffset());

Serial.println("***********************************");

Serial.println(" ");

break;

case 'r':

Serial.println("Reset to Default");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

resetFunc();

break;

case 'a':

Serial.println("Accel X");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Acceleration X:");

for(int i0=0; i0<15; i0++) {

accelgyro.getAcceleration(&ax, &ay, &az);

//accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("X: ");

Serial.println(ax);

Serial.println(" ");

delay(1000*1);

}

break;

case 'b':

Serial.println("Accel Y");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Acceleration Y:");

for(int i1=0; i1<15; i1++) {

accelgyro.getAcceleration(&ax, &ay, &az);

//accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("Y: ");

Serial.println(ay);

Serial.println(" ");

delay(1000*1);

}

break;

case 'c':

Serial.println("Accel Z");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Acceleration Z:");

for(int i2=0; i2<15; i2++) {

accelgyro.getAcceleration(&ax, &ay, &az);

//accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("Z: ");

Serial.println(az);

Serial.println(" ");

delay(1000*1);

}

break;

//****************

case 'd':

Serial.println("Gyro X");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Gyro Rotation X:");

for(int i3=0; i3<15; i3++) {

//accelgyro.getAcceleration(&ax, &ay, &az);

accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("X: ");

Serial.println(gx);

Serial.println(" ");

delay(1000*1);

}

break;

case 'e':

Serial.println("Gyro Y");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Gyro Rotation Y:");

for(int i4=0; i4<15; i4++) {

//accelgyro.getAcceleration(&ax, &ay, &az);

accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("Y: ");

Serial.println(gy);

Serial.println(" ");

delay(1000*1);

}

break;

case 'f':

Serial.println("Gyro Z");

logNumA = 0;

accelgyro.resetSensors();

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

Serial.println("Gyro Rotation Z:");

for(int i5=0; i5<15; i5++) {

//accelgyro.getAcceleration(&ax, &ay, &az);

accelgyro.getRotation(&gx, &gy, &gz);

Serial.print("Log No.: "); Serial.println(logNumA++);

//Serial.println(" ");

//Serial.print("Z: ");

Serial.println(gz);

Serial.println(" ");

delay(1000*1);

}

break;

}

}

}

void read_switch() {

buttonState = digitalRead(buttonPin);

//Serial.print("Switch: "); Serial.println(buttonState);

if (buttonState == HIGH) {

Serial.print("Log No.: "); Serial.println(logNumA++);

show_Temp();

read_Accel();

read_Gyro();

if_Accel();

if_Gyro();

}

else if (buttonState == LOW) {

digitalWrite(alarm_Accel, LOW);

digitalWrite(alarm_Gyro, LOW);

}

}

void read_Temp() {

dT = ( (double) accelgyro.getTemperature() + 11450.70) / 340.0;

}

void show_Temp() {

Serial.print("Temperature: "); Serial.print(dT, 2); Serial.println(" Celsius"); Serial.println(" ");

}

void read_Accel() {

accelgyro.getAcceleration(&ax, &ay, &az);

//ax = map(ax, -17000, 17000, -20, 20);

//ay = map(ay, -17000, 17000, -20, 20);

//az = map(az, -17000, 17000, -20, 20);

//Serial.println("*******************");

Serial.println("Acceleration:");

Serial.print("X: "); Serial.println(ax);

Serial.print("Y: "); Serial.println(ay);

Serial.print("Z: "); Serial.println(az);

Serial.println(" ");

}

void read_Gyro() {

accelgyro.getRotation(&gx, &gy, &gz);

//gx = map(gx, -17000, 17000, -20, 20);

//gy = map(gy, -17000, 17000, -20, 20);

//gz = map(gz, -17000, 17000, -20, 20);

Serial.println("Gyro Rotation:");

Serial.print("X: "); Serial.println(gx);

Serial.print("Y: "); Serial.println(gy);

Serial.print("Z: "); Serial.println(gz);

//Serial.println("*******************");

Serial.println(" ");

}

void if_Accel() {

if (ax >= ax_High) { // 100 - 6

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel X High");

Serial.println(" ");

}

else if (ax <= ax_Low) { // -1 _ -100

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel X Low");

Serial.println(" ");

}

else if (ay >= ay_High) { // -5 _ -100

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel Y High");

Serial.println(" ");

}

else if (ay <= ay_Low) { // 100 - 1

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel Y Low");

Serial.println(" ");

}

else if (az >= az_High) { // -5 _ -100

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel Z High");

Serial.println(" ");

}

else if (az <= az_Low) { // 100 - 1

digitalWrite(alarm_Accel, HIGH);

Serial.println("Accel Z Low");

Serial.println(" ");

}

else {

digitalWrite(alarm_Accel, LOW);

}

}

void if_Gyro() {

if (gx >= gx_High) { // +

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro X High");

Serial.println(" ");

}

else if (gx <= gx_Low) { // -

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro X Low");

Serial.println(" ");

}

else if (gy >= gy_High) { // -

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro Y High");

Serial.println(" ");

}

else if (gy <= gy_Low) { // +

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro Y Low");

Serial.println(" ");

}

else if (gz >= gz_High) { // -

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro Z High");

Serial.println(" ");

}

else if (gz <= gz_Low) { // +

digitalWrite(alarm_Gyro, HIGH);

Serial.println("Gyro Z Low");

Serial.println(" ");

}

else {

digitalWrite(alarm_Gyro, LOW);

}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...