1 | #include <Wire.h>
|
2 | #include "MCP3X21.h"
|
3 | #include <ADXL345_WE.h>
|
4 | #define ADXL345_I2CADDR 0x53
|
5 |
|
6 |
|
7 | const uint8_t address = 0x4D;
|
8 | const uint16_t ref_voltage = 3300;
|
9 | const int int2Pin = 2;
|
10 | int resultnew = 0;
|
11 | float potiwert = 0;
|
12 | bool interruptON = false;
|
13 | byte intSource;
|
14 | volatile bool in_activity = false;
|
15 |
|
16 | ADXL345_WE myAcc = ADXL345_WE(ADXL345_I2CADDR);
|
17 | MCP3021 mcp3021(address);
|
18 |
|
19 |
|
20 | void setup() {
|
21 | Wire.begin();
|
22 | Serial.begin(115200);
|
23 | pinMode(int2Pin, INPUT);
|
24 | Serial.println("ADXL345_Sketch - Activity and Inactivity Interrupts");
|
25 | Serial.println();
|
26 | delay(200);
|
27 | if (!myAcc.init()) {
|
28 | Serial.println("ADXL345 not connected");
|
29 | Serial.print("ADXL345 connected!");
|
30 | }
|
31 |
|
32 | myAcc.setDataRate(ADXL345_DATA_RATE_50);
|
33 | Serial.print("Data rate: ");
|
34 | Serial.print(myAcc.getDataRateAsString());
|
35 | delay(100);
|
36 |
|
37 | myAcc.setRange(ADXL345_RANGE_8G);
|
38 | delay(25);
|
39 | Serial.print(" grange: ");
|
40 | Serial.print(myAcc.getRangeAsString());
|
41 | Serial.println();
|
42 |
|
43 |
|
44 | attachInterrupt(digitalPinToInterrupt(int2Pin), in_activityISR, RISING);
|
45 |
|
46 | myAcc.setInactivityParameters(ADXL345_DC_MODE, ADXL345_XY0, 1, 2.0);
|
47 |
|
48 | myAcc.setInterrupt(ADXL345_ACTIVITY, INT_PIN_2);
|
49 |
|
50 | uint16_t result1 = mcp3021.read();
|
51 | Serial.print("result: ");
|
52 | Serial.println(result1);
|
53 | result1 = map(result1, 0, 1023, 300, 1600);
|
54 | if (result1 != resultnew) {
|
55 | potiwert = result1 / 100.00;
|
56 | myAcc.setActivityParameters(ADXL345_DC_MODE, ADXL345_XYZ, potiwert);
|
57 | delay(50);
|
58 | myAcc.setInactivityParameters(ADXL345_DC_MODE, ADXL345_XY0, 1, 1);
|
59 | Serial.print("Mein Sensor ist jetzt auf: ");
|
60 | Serial.println(potiwert);
|
61 | Serial.print(" g eingestellt");
|
62 | Serial.print("Der Wert des ADC beträgt: ");
|
63 | Serial.println(result1);
|
64 | delay(25);
|
65 | resultnew = result1;
|
66 | }
|
67 | }
|
68 |
|
69 | void loop() {
|
70 | if (in_activity == true) {
|
71 | String axes = myAcc.getActTapStatusAsString();
|
72 | intSource = myAcc.readAndClearInterrupts();
|
73 | if (myAcc.checkInterrupt(intSource, ADXL345_ACTIVITY)) {
|
74 | delay(50);
|
75 | if (digitalRead(2) == true) {
|
76 | interruptON = true;
|
77 | }
|
78 | Serial.print("interrupt Merker: ");
|
79 | Serial.println(interruptON);
|
80 | Serial.print("Activity at: ");
|
81 | Serial.println(axes);
|
82 | }
|
83 | Serial.print("Status I2: ");
|
84 | Serial.println(digitalRead(2));
|
85 |
|
86 | myAcc.readAndClearInterrupts();
|
87 | in_activity = false;
|
88 |
|
89 | }
|
90 | if (myAcc.checkInterrupt(intSource, ADXL345_INACTIVITY)) {
|
91 | Serial.println("Inactivity!");
|
92 | }
|
93 | interruptON = false;
|
94 |
|
95 |
|
96 | }
|
97 |
|
98 | void in_activityISR() {
|
99 | in_activity = true;
|
100 | }
|