1 | /****************************************************************************
|
---|
2 | * Copyright (C) 2011 - 2014 Bosch Sensortec GmbH
|
---|
3 | *
|
---|
4 | * Motion.ino
|
---|
5 | * Date: 2014/09/09
|
---|
6 | * Revision: 2.0 $
|
---|
7 | *
|
---|
8 | * Usage: Example code of a game to demonstrate the Any motion
|
---|
9 | * and No motion Interrupt features
|
---|
10 | *
|
---|
11 | ****************************************************************************
|
---|
12 | /***************************************************************************
|
---|
13 | * License:
|
---|
14 | *
|
---|
15 | * Redistribution and use in source and binary forms, with or without
|
---|
16 | * modification, are permitted provided that the following conditions are met:
|
---|
17 | *
|
---|
18 | * Redistributions of source code must retain the above copyright
|
---|
19 | * notice, this list of conditions and the following disclaimer.
|
---|
20 | *
|
---|
21 | * Redistributions in binary form must reproduce the above copyright
|
---|
22 | * notice, this list of conditions and the following disclaimer in the
|
---|
23 | * documentation and/or other materials provided with the distribution.
|
---|
24 | *
|
---|
25 | * Neither the name of the copyright holder nor the names of the
|
---|
26 | * contributors may be used to endorse or promote products derived from
|
---|
27 | * this software without specific prior written permission.
|
---|
28 | *
|
---|
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
30 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
31 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
32 | * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
---|
33 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
34 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
|
---|
39 | *
|
---|
40 | * The information provided is believed to be accurate and reliable.
|
---|
41 | * The copyright holder assumes no responsibility for the consequences of use
|
---|
42 | * of such information nor for any infringement of patents or
|
---|
43 | * other rights of third parties which may result from its use.
|
---|
44 | * No license is granted by implication or otherwise under any patent or
|
---|
45 | * patent rights of the copyright holder.
|
---|
46 | */
|
---|
47 |
|
---|
48 | #include "NAxisMotion.h" //Contains the bridge code between the API and the Arduino Environment
|
---|
49 | #include <Wire.h>
|
---|
50 |
|
---|
51 | NAxisMotion mySensor; //Object that for the sensor
|
---|
52 | bool intDetected = false; //Flag to indicate if an interrupt was detected
|
---|
53 | int threshold = 5; //At a Range of 4g, the threshold is set at 39.05mg or 0.3830m/s2. This Range is the default for NDOF Mode
|
---|
54 | int duration = 1; //At a filter Bandwidth of 62.5Hz, the duration is 8ms. This Bandwidth is the default for NDOF Mode
|
---|
55 | bool anyMotion = true; //To know which interrupt was triggered
|
---|
56 |
|
---|
57 | void setup() //This code is executed once
|
---|
58 | {
|
---|
59 | //Peripheral Initialization
|
---|
60 | Serial.begin(115200); //Initialize the Serial Port to view information on the Serial Monitor
|
---|
61 | I2C.begin(); //Initialize I2C communication to the let the library communicate with the sensor.
|
---|
62 | //Sensor Initialization
|
---|
63 | Serial.println("Please wait. Initialization in process.");
|
---|
64 | mySensor.initSensor(); //The I2C Address can be changed here inside this function in the library
|
---|
65 | mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
|
---|
66 | mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
|
---|
67 | //Setting to MANUAL requires lesser reads to the sensor
|
---|
68 |
|
---|
69 | attachInterrupt(INT_PIN, motionISR, RISING); //Attach the interrupt to the Interrupt Service Routine for a Rising Edge. Change the interrupt pin depending on the board
|
---|
70 |
|
---|
71 | //Setup the initial interrupt to trigger at No Motion
|
---|
72 | mySensor.resetInterrupt();
|
---|
73 | mySensor.enableSlowNoMotion(threshold, duration, NO_MOTION);
|
---|
74 | anyMotion = false;
|
---|
75 | mySensor.accelInterrupts(ENABLE, ENABLE, ENABLE); //Accelerometer interrupts can be triggered from all 3 axes
|
---|
76 | Serial.println("This is a game to test how steady you can move an object with one hand. \nKeep the device on a table and mark 2 points.");
|
---|
77 | Serial.println("Move the Device from one place to another without triggering the Any Motion Interrupt.\n\n");
|
---|
78 | delay(1000); //Delay for the player(s) to read
|
---|
79 | Serial.println("Move the device around and then place it at one position.\nChange the threshold and duration to increase the difficulty level.");
|
---|
80 | Serial.println("Have fun!\n\n");
|
---|
81 | }
|
---|
82 |
|
---|
83 | void loop() //This code is looped forever
|
---|
84 | {
|
---|
85 | if (intDetected)
|
---|
86 | {
|
---|
87 | if (anyMotion)
|
---|
88 | {
|
---|
89 | Serial.println("You moved!! Try again. Keep the Device at one place.\n");
|
---|
90 | intDetected = false;
|
---|
91 | mySensor.resetInterrupt(); //Reset the interrupt line
|
---|
92 | mySensor.disableAnyMotion(); //Disable the Any motion interrupt
|
---|
93 | mySensor.enableSlowNoMotion(threshold, duration, NO_MOTION); //Enable the No motion interrupt (can also use the Slow motion instead)
|
---|
94 | anyMotion = false;
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | Serial.println("Device is not moving. You may start again.\n\n\n");
|
---|
99 | intDetected = false;
|
---|
100 | mySensor.resetInterrupt(); //Reset the interrupt line
|
---|
101 | mySensor.disableSlowNoMotion(); //Disable the Slow or No motion interrupt
|
---|
102 | mySensor.enableAnyMotion(threshold, duration); //Enable the Any motion interrupt
|
---|
103 | anyMotion = true;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | //Interrupt Service Routine when the sensor triggers an Interrupt
|
---|
109 | void motionISR()
|
---|
110 | {
|
---|
111 | intDetected = true;
|
---|
112 | }
|
---|