-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino_Datalogger.ino
192 lines (134 loc) · 4.46 KB
/
Arduino_Datalogger.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
// Dataacquisition and analysis
// by Raphael Nagel
// 10032284 - University of the West of England, Bristol
// Created 24 Januar 2013
// This software contacts the MPL3115A2 barometer via I2C and reads a single barometer value with 20bits
// accuracy. It calculates the averages over a 30 sample long period
#include <I2C.h>
#define CP_OK 1
#define CP_ERROR 0
/*Barometer registers*/
#define baro 0x60
#define OUT_P_MSB 0x01
#define OUT_P_CSB 0x02
#define OUT_P_LSB 0x03
#define CTRL_REG1 0x26
#define set_OST 0x02
#define set_OST_OS 0x3A
#define Period500Hz 2000
#if 0
/*Temperature Registers - currently not needed*/
char OUT_T_MSB = 0x04;
char OUT_T_LSB = 0x05;
#endif
unsigned long raw = 0; //the raw input value in binary
unsigned long input[30]; //the 32 word FIFO buffer
unsigned long averaged[2]; //the averaged value
int derivative;
int devav[30];
int output = 0; //the output value after it has been worked - in binary
unsigned int counter = 1;
unsigned int i;
unsigned int delay_val = 1800;
unsigned int time = 0;
/********************FUNCTION PROTOTYPES************************************/
void PrintSignedNumber(unsigned long );
unsigned int SingleBarometerRead(unsigned long *);
/******************** VOID SETUP ******************************************/
void setup()
{
I2c.begin(); // join i2c bus (address optional for master)
TWBR = 12; //set the I2C frequency to 400kHz
I2c.pullup(1);
Serial.begin(9600); // start serial for output
}
/******************** VOID Loop ******************************************/
void loop()
{
time = micros(); //store the start time of our reads
SingleBarometerRead(&raw);
//30 element FIFO buffer
for(i = 29;i>=1;i--){
input[i]=input[i-1];
}
input[0] = raw;
/*************average the output and ************************/
if(counter <=30){
output = input[0];
PrintSignedNumber(output);
counter++;
}
else{
averaged[1] = averaged[0];
averaged[0] = 0;
for(i = 0;i<30;i++){
averaged[0] = averaged[0]+input[i];
}
averaged[0] =(unsigned long) (averaged[0]/30);
/* find the derivative.....*/
derivative = averaged[1]-averaged[0];
//30 element FIFO buffer to collect the derivatives
for(i = 29;i>=1;i--){
devav[i]=devav[i-1];
}
devav[0] = derivative;
output = 0;
for(i = 0;i<30;i++){
output = output + devav[i];
}
output =(output/30);
/************************************************************ OUTPUT ********************************************/
Serial.println(raw,DEC);
/************************************************************ OUTPUT ********************************************/
// PrintSignedNumber(derivative);
}
#if 0
/*ensure a sample rate of about 500Hz*/
time = (micros()-time); //how much time has evolved since we started the read and
if((Period500Hz-time)> 0){
delayMicroseconds(Period500Hz-time);
}
else{
for(i = 0; i>10;i++){
Serial.print("cannot run at 500Hz sampling rate");
delay(1000);
}
}
#endif
}
void PrintSignedNumber(unsigned long rawin){
unsigned long bin_nofract;
unsigned long fract;
//otherwise its positive
Serial.print(PSTR(" "));
bin_nofract = (rawin>>2)&0x03FFFF;
fract = (rawin & 0x03)*0x19;
Serial.print(bin_nofract, DEC);
Serial.print(".");
Serial.println(fract,DEC);
}
unsigned int SingleBarometerRead(unsigned long *raw){
/*Temporary output data*/
unsigned long MSB_Data = 0;
unsigned long CSB_Data = 0;
unsigned long LSB_Data = 0;
I2c.write(baro,CTRL_REG1,set_OST_OS); //initiates a single barometer read.
delay(0); /*The typical data rate in OST - one shot mode is 100Hz*/
I2c.read(baro,OUT_P_MSB,8);
MSB_Data = I2c.receive();//these are the 8 MSB out of the total of 20-bit
I2c.read(baro,OUT_P_CSB,8);
CSB_Data = I2c.receive();//these are the 8 CSB out of a total of 20-bits
I2c.read(baro,OUT_P_LSB,8);
LSB_Data = (I2c.receive());//these are the 4 LSB of the 20-bit output.
//The bitmap of the value received from the barometer however places these at the 4 MSB positions of the 8-bit word received.
//The lower 4-bits are '0'. THus we rightshift to get rid of these.
//CHANGED: we ignore the fraction now...
//Serial.println(((MSB_Data<<10)|(CSB_Data<<2)|LSB_Data>>6),DEC);
*raw = (unsigned long)((MSB_Data<<10)|(CSB_Data<<2)|LSB_Data>>6); //all output data put together.
if(raw != 0){
return CP_OK;
}
else{
return CP_ERROR;
}
}