-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsensor_logger.ino
212 lines (168 loc) · 5.49 KB
/
sensor_logger.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Basic demo for accelerometer readings from Adafruit LIS3DH
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include "RTClib.h"
// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10
#define VBATPIN A7
// Enable debug logger.
// Note: Comment out before running in real-world.
#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINTLN(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
RTC_PCF8523 rtc;
// change this to match your SD shield or module;
// Adafruit SD shields and modules: pin 10
const int chipSelect = 10;
// 19 digits plus the null char
char DateTimeString[20];
// Filename format: 20180710.csv
char filename[13];
char dataRow[10000];
String strRow;
int counter = 0;
int lastX;
File dataFile;
// software SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
// hardware SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
void setup() {
#ifdef DEBUG
Serial.begin(9600);
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
DEBUG_PRINT("VBat: " ); DEBUG_PRINTLN(measuredvbat);
DEBUG_PRINTLN("Initializing SD card...");
if (!SD.begin(chipSelect)) {
DEBUG_PRINTLN("Card failed, or not present");
// don't do anything more:
while (1);
}
DEBUG_PRINTLN("Card initialized.");
DEBUG_PRINTLN("Initializing RTC...");
if (! rtc.begin()) {
DEBUG_PRINTLN("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
DEBUG_PRINTLN("RTC is NOT running!");
}
DEBUG_PRINTLN("RTC initialized.");
DEBUG_PRINTLN("Initializing LIS3DH Sensor...");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
DEBUG_PRINTLN("Couldnt start");
while (1);
}
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
DEBUG_PRINTLN("LIS3DH initialized.");
DEBUG_PRINTLN("Ready!");
DateTime now = rtc.now();
sprintf_P(DateTimeString, PSTR("%4d-%02d-%02d %d:%02d:%02d"),
now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
sprintf_P(filename, PSTR("%4d%02d%02d.csv"), now.year(), now.month(), now.day());
DEBUG_PRINTLN(DateTimeString);
DEBUG_PRINTLN(filename);
dataFile = SD.open(filename, O_CREAT | O_WRITE);
dataFile.println("timestamp, accel x, accel y, accel z, accel unit, sensor range, millis, micros, voltage");
DEBUG_PRINTLN(dataFile);
if (! dataFile) {
DEBUG_PRINTLN("Could not open file...");
}
dataFile.close();
// Check to see if the file exists:
if (SD.exists(filename)) {
DEBUG_PRINTLN("Log file exists.");
} else {
DEBUG_PRINTLN("Log file doesn't exist.");
}
// Leave the file open for writing.
dataFile = SD.open(filename, O_CREAT | O_APPEND | O_WRITE);
}
// Main Loop
void loop() {
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
//DEBUG_PRINT("VBat: " ); DEBUG_PRINTLN(measuredvbat);
lis.read(); // get X Y and Z data at once
// Then print out the raw data
/*
DEBUG_PRINT(lis.x); DEBUG_PRINT(", ");
DEBUG_PRINT(lis.y); DEBUG_PRINT(", ");
DEBUG_PRINT(lis.z); DEBUG_PRINT(", ");
*/
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lis.getEvent(&event);
int rangeVal = 2 << lis.getRange();
DateTime now = rtc.now();
/*
sprintf_P(dataRow, PSTR("%d, %d, %d, %d, %s, %s, %d"),
now.unixtime(), event.acceleration.x, event.acceleration.y, event.acceleration.z, "m/s^2", rangeVal, millis());
DEBUG_PRINTLN(dataRow);
*/
/* Display the results (acceleration is measured in m/s^2) */
strRow = String(now.unixtime()) + ", ";
strRow += String(event.acceleration.x) + ", ";
strRow += String(event.acceleration.y) + ", ";
strRow += String(event.acceleration.z) + ", ";
strRow += "m/s^2, ";
strRow += String(rangeVal) + ", ";
strRow += String(millis()) + ", ";
strRow += String(micros()) + ", ";
strRow += String(measuredvbat);
DEBUG_PRINTLN(strRow);
// Open the file if it's closed.
if (!dataFile) {
dataFile = SD.open(filename, O_CREAT | O_APPEND | O_WRITE);
}
// if the file is available, write to it:
if (dataFile) {
dataFile.println(strRow);
} else {
// if the file isn't open, pop up an error:
DEBUG_PRINTLN("error opening file");
}
// Determine if the unit is actively recording important motion to prevent delays from writing ops.
// X-Axis is inline with bore. Least likely to detect acceleration of gravity.
bool inMotion = false;
if (max(event.acceleration.x, lastX) > 1.0 || min(event.acceleration.x, lastX) < -1.0) {
inMotion = true;
}
counter++;
// write the data to prevent loss.
if (counter >= 20 && !inMotion) {
DEBUG_PRINTLN("Data file write/flush.");
dataFile.flush();
counter = 0;
} else {
DEBUG_PRINTLN(inMotion);
}
if (counter >= 100) {
DEBUG_PRINTLN("Data file write/close.");
dataFile.close();
counter = 0;
}
lastX = event.acceleration.x;
//delay(1);
}