Browse code

Create acc-logger.ino

Stefan Höhn authored on14/12/2019 20:52:21 • GitHub committed on14/12/2019 20:52:21
Showing1 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,226 @@
1
+/******************************************************************************
2
+MMA8452Q_Basic.ino
3
+SFE_MMA8452Q Library Basic Example Sketch
4
+Jim Lindblom @ SparkFun Electronics
5
+Original Creation Date: June 3, 2014
6
+https://github.com/sparkfun/MMA8452_Accelerometer
7
+
8
+This sketch uses the SFE_MMA8452Q library to initialize the
9
+accelerometer, and stream values from it.
10
+
11
+Hardware hookup:
12
+  Arduino --------------- MMA8452Q Breakout
13
+    3.3V  ---------------     3.3V
14
+    GND   ---------------     GND
15
+  SDA (A4) --\/330 Ohm\/--    SDA
16
+  SCL (A5) --\/330 Ohm\/--    SCL
17
+
18
+The MMA8452Q is a 3.3V max sensor, so you'll need to do some 
19
+level-shifting between the Arduino and the breakout. Series
20
+resistors on the SDA and SCL lines should do the trick.
21
+
22
+Development environment specifics:
23
+  IDE: Arduino 1.0.5
24
+  Hardware Platform: Arduino Uno
25
+
26
+This code is beerware; if you see me (or any other SparkFun employee) at the
27
+local, and you've found our code helpful, please buy us a round!
28
+
29
+Distributed as-is; no warranty is given.
30
+******************************************************************************/
31
+#include <Wire.h> // Must include Wire library for I2C
32
+#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
33
+
34
+// Begin using the library by creating an instance of the MMA8452Q
35
+//  class. We'll call it "accel". That's what we'll reference from
36
+//  here on out.
37
+MMA8452Q accel;
38
+
39
+
40
+const int buttonPin = 12;
41
+const int ledPin =  13;
42
+int buttonState = 0;
43
+float xyz[263];
44
+int cycle = 0;
45
+int cycle1 = 0;
46
+int messungen = 264;
47
+
48
+
49
+// The setup function simply starts serial and initializes the
50
+//  accelerometer.
51
+void setup()
52
+{
53
+  Serial.begin(9600);
54
+  Serial.println("MMA8452Q Test Code!");
55
+  
56
+  // Choose your adventure! There are a few options when it comes
57
+  // to initializing the MMA8452Q:
58
+  //  1. Default init. This will set the accelerometer up
59
+  //     with a full-scale range of +/-2g, and an output data rate
60
+  //     of 800 Hz (fastest).
61
+  //accel.init();
62
+  //  2. Initialize with FULL-SCALE setting. You can set the scale
63
+  //     using either SCALE_2G, SCALE_4G, or SCALE_8G as the value.
64
+  //     That'll set the scale to +/-2g, 4g, or 8g respectively.
65
+  //accel.init(SCALE_4G); // Uncomment this out if you'd like
66
+  //  3. Initialize with FULL-SCALE and DATA RATE setting. If you
67
+  //     want control over how fast your accelerometer produces
68
+  //     data use one of the following options in the second param:
69
+  //     ODR_800, ODR_400, ODR_200, ODR_100, ODR_50, ODR_12,
70
+  //     ODR_6, or ODR_1. 
71
+  //     Sets to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz.
72
+  accel.init(SCALE_8G, ODR_12);
73
+}
74
+
75
+// The loop function will simply check for new data from the
76
+//  accelerometer and print it out if it's available.
77
+void loop()
78
+{
79
+
80
+
81
+buttonState = digitalRead(buttonPin);
82
+  if (buttonState == HIGH) {
83
+    digitalWrite(ledPin, HIGH);
84
+
85
+    for (int cycle = 0 ; cycle < messungen; cycle = cycle + 3) {
86
+      delay (100);
87
+      
88
+      //Serial.print (i);
89
+      //Serial.print("\t");
90
+      //Serial.print(cycle);
91
+      //Serial.print("\t");
92
+      Serial.print (xyz[cycle], 3);
93
+      Serial.print("\t");
94
+      Serial.print (xyz[cycle + 1], 3);
95
+      Serial.print("\t");
96
+      Serial.print (xyz[cycle + 2], 3);
97
+      Serial.print("\t");
98
+      Serial.println(); // Print new line every time.
99
+
100
+    }
101
+    Serial.print ("Daten zu Ende");
102
+    delay(1000);
103
+    // cycle = 0 ;
104
+digitalWrite(ledPin, LOW);
105
+  } else {
106
+    
107
+
108
+
109
+  
110
+  // Use the accel.available() function to wait for new data
111
+  //  from the accelerometer.
112
+  if (accel.available())
113
+  {
114
+    // First, use accel.read() to read the new variables:
115
+    accel.read();
116
+    
117
+    // accel.read() will update two sets of variables. 
118
+    // * int's x, y, and z will store the signed 12-bit values 
119
+    //   read out of the accelerometer.
120
+    // * floats cx, cy, and cz will store the calculated 
121
+    //   acceleration from those 12-bit values. These variables 
122
+    //   are in units of g's.
123
+    // Check the two function declarations below for an example
124
+    // of how to use these variables.
125
+    if (cycle1 < messungen) {
126
+      cycle1 = cycle1 +3;
127
+    printCalculatedAccels();
128
+    }
129
+    //printAccels(); // Uncomment to print digital readings
130
+    
131
+    // The library also supports the portrait/landscape detection
132
+    //  of the MMA8452Q. Check out this function declaration for
133
+    //  an example of how to use that.
134
+    //printOrientation();
135
+    
136
+ //   Serial.println(); // Print new line every time.
137
+  }
138
+}
139
+}
140
+
141
+// The function demonstrates how to use the accel.x, accel.y and
142
+//  accel.z variables.
143
+// Before using these variables you must call the accel.read()
144
+//  function!
145
+void printAccels()
146
+{
147
+  Serial.print(accel.x, 3);
148
+  Serial.print("\t");
149
+  Serial.print(accel.y, 3);
150
+  Serial.print("\t");
151
+  Serial.print(accel.z, 3);
152
+  Serial.print("\t");
153
+}
154
+
155
+// This function demonstrates how to use the accel.cx, accel.cy,
156
+//  and accel.cz variables.
157
+// Before using these variables you must call the accel.read()
158
+//  function!
159
+/************************
160
+void printCalculatedAccels()
161
+{ 
162
+  Serial.print(accel.cx, 3);
163
+  Serial.print("\t");
164
+  Serial.print(accel.cy, 3);
165
+  Serial.print("\t");
166
+  Serial.print(accel.cz, 3);
167
+  Serial.print("\t");
168
+}
169
+**************************/
170
+
171
+void printCalculatedAccels()
172
+{
173
+  messungen = 250;
174
+  cycle=cycle + 3;
175
+    xyz[cycle] = (accel.cx);
176
+    //  Serial.print("\t");
177
+    xyz[cycle + 1] = (accel.cy);
178
+    //  Serial.print("\t");
179
+    xyz[cycle + 2] = (accel.cz);
180
+    // Serial.print("\t");
181
+Serial.print (xyz[cycle], 3);
182
+Serial.print("\t"); 
183
+Serial.print (xyz[cycle + 1], 3);
184
+Serial.print("\t");
185
+Serial.print (xyz[cycle + 2], 3);
186
+Serial.print("\t");
187
+
188
+    
189
+    Serial.print(cycle);
190
+    Serial.print("\n");
191
+if (cycle > messungen){
192
+  return;
193
+  }
194
+    
195
+    }
196
+
197
+
198
+// This function demonstrates how to use the accel.readPL()
199
+// function, which reads the portrait/landscape status of the
200
+// sensor.
201
+void printOrientation()
202
+{
203
+  // accel.readPL() will return a byte containing information
204
+  // about the orientation of the sensor. It will be either
205
+  // PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
206
+  // LOCKOUT.
207
+  byte pl = accel.readPL();
208
+  switch (pl)
209
+  {
210
+  case PORTRAIT_U:
211
+    Serial.print("Portrait Up");
212
+    break;
213
+  case PORTRAIT_D:
214
+    Serial.print("Portrait Down");
215
+    break;
216
+  case LANDSCAPE_R:
217
+    Serial.print("Landscape Right");
218
+    break;
219
+  case LANDSCAPE_L:
220
+    Serial.print("Landscape Left");
221
+    break;
222
+  case LOCKOUT:
223
+    Serial.print("Flat");
224
+    break;
225
+  }
226
+}