-
Notifications
You must be signed in to change notification settings - Fork 1
/
STEAMRnE_32.ino
149 lines (109 loc) · 2.7 KB
/
STEAMRnE_32.ino
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
144
145
146
147
#define DEBUG true
#define POT 1
// for Common:
#include <Wire.h>
#include <I2Cdev.h>
// for Acc:
#include <MPU6050.h>
// for Time:
#include <TimeLib.h>
#include <DS1307RTC.h>
// for Display:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "define.h"
Adafruit_SSD1306 display(OLED_RESET);
MPU6050 mpu;
tmElements_t tm;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy, vz, fx, fy, fz;
String Date = "N/A";
String Time = "N/A";
String acc = "";
String output = "";
int _wait = 0;
int potdata;
void setup() {
Serial.begin(115200);
while (!Serial);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
wait(1);
display.clearDisplay();
mpu.initialize();
while (!mpu.testConnection());
}
void loop() {
Time = "N/A";
Date = "N/A";
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
fx = vx = (gx + 300) / 200 + 2;
fy = vy = (gz - 100) / 200 + 2;
fz = vz = (gz - 600) / 200;
acc = String(fx) + '|' +
String(fy) + '|' +
String(fz);
if (RTC.read(tm)) {
Date = tmYearToCalendar(tm.Year);
Date += '/';
save2digits(&Date, tm.Month);
Date += '/';
save2digits(&Date, tm.Day);
Time = "";
save2digits(&Time, tm.Hour);
Time += ':';
save2digits(&Time, tm.Minute);
Time += ':';
save2digits(&Time, tm.Second);
}
output = "data:(" + Date + ' ' + Time + '|' + acc + ")\n";
char* data = new char[output.length() + 1];
output.toCharArray(data, output.length() + 1);
Serial.write(data);
delete[] data;
data = 0;
potdata = analogRead(POT);
if(potdata <= 512) {
displayTime(Date, Time);
} else {
displayData(fx, fy, fz);
}
// time correcting
String secNow = String(tm.Second);
char* timeNow = new char[secNow.length() + 1];
char* timeBefore = new char[secNow.length() + 1];
secNow.toCharArray(timeBefore, secNow.length() + 1);
for(;;) {
secNow = String(tm.Second);
secNow.toCharArray(timeNow, secNow.length() + 1);
if(!strcmp(timeBefore, timeNow))
break;
}
delete[] timeNow;
delete[] timeBefore;
timeNow = timeBefore = 0;
}
void displayTime(String Date, String Time) {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(5, 0);
display.println("TIME");
display.setCursor(0, 20);
display.print(Date);
display.print(" ");
display.println(Time);
display.display();
}
void displayData(int x, int y, int z) {
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,0);
display.print("x:"); display.println(x);
display.setCursor(10,20);
display.print("y:"); display.println(y);
display.setCursor(10,40);
display.print("z:"); display.println(z);
display.display();
}