-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorRotater.ino
More file actions
143 lines (115 loc) · 3.59 KB
/
MonitorRotater.ino
File metadata and controls
143 lines (115 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Author : Shenggao Li
Date : 2022 July
Target Board : Arduino Pro Micro
Peripherals : ADXL345 x 1
Sensing the rotation
Touch Sensor (or any button) x 1
As the major input device
Toggle Switch x 1
Use as a enable/disable switch
Compatible with : Arduino Micro, etc.
*/
#include <Wire.h> // Wire library - used for I2C communication
#include <EEPROM.h>
#include "Keyboard.h"
#include "helper.hpp"
//#define DEBUG
#define KEYBOARD
#define CAL_HEADER 0xCA // Calibration data header
// Use USB serial port for debugging
#ifdef DEBUG
#define Serial_port Serial
#else
//#define Serial_port Serial1
// Seems USB serial can work with Keyboard port at the same time.
// Use Serial1 if you have any stability issue.
#define Serial_port Serial
#endif //DEBUG
// Define pin connection
#define toggle 5
#define button 4
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
// Sample number for calibration
#define NUM_REST 1000
// Sample delay time (in ms)
#define DELAY_REST 5
// Some process control vars
static bool last_button_state = false;
static uint32_t reset_millis = 0;
bool resetted = false;
static uint32_t check_millis = 0;
// Init vector and matrix variables
Vec3D g_vec(0, -1, 0);
Vec3D g_ref(0, -1, 0);
const Mat Rot_90 (0, 1, 0, -1, 0, 0, 0, 0, 1); // ANGLEFT
const Mat Rot_180(-1, 0, 0, 0, -1, 0, 0, 0, 1); // ANGUP
const Mat Rot_270(0, -1, 0, 1, 0, 0, 0, 0, 1); // ANGRIGHT
// enum for 4 rotation configs
enum DirecAngle { ANGDOWN, ANGLEFT, ANGUP, ANGRIGHT };
// Init rotation angle
DirecAngle rotAngle_curt(ANGDOWN), rotAngle_last(ANGDOWN);
void setup() {
// put your setup code here, to run once:
Serial_port.begin(9600);
#ifdef KEYBOARD
Keyboard.begin();
#endif //KEYBOARD
delay(2000);
Serial_port.println("Starting Device...");
Serial_port.println("Waiting for USB connection ...");
delay(3000);
Wire.begin(); // Initiate the Wire library
IMU_offset();
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
pinMode(toggle, INPUT_PULLUP);
pinMode(button, INPUT);
resetted = true;
load_g_ref(); // Load calibrated data from EEPROM
Serial_port.println("[INITIALIZATION DONE]");
}
void loop() {
const uint32_t t = millis();
const bool button_state = digitalRead(button);
if ( button_state ) { // button pressed
if (!last_button_state) {
reset_millis = t + 3000; // set 3 seconds for restart;
} else {
if (t > reset_millis && resetted) {
resetted = false;
reset_g();
}
}
} else {
if (last_button_state) {
if (!resetted) {
resetted = true;
} else {
g_vec = read_gravity();
rotAngle_curt = get_angle();
Serial_port.print("Rotation angle is ");
Serial_port.println(rotAngle_curt);
rotate();
rotAngle_last = rotAngle_curt;
}
}
}
last_button_state = button_state;
if (t > check_millis) {
check_millis = t + 3000; // check orientation per 3 secs
g_vec = read_gravity();
rotAngle_curt = get_angle();
Serial_port.print("Rotation angle is ");
Serial_port.println(rotAngle_curt);
if (rotAngle_curt != rotAngle_last) {
rotate();
rotAngle_last = rotAngle_curt;
}
}
}