Browse code

neues Arduino script

dewomser authored on26/03/2020 09:26:07
Showing1 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,237 @@
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
+int xyz[720];
44
+byte orient[240];
45
+int cycle = 0;
46
+int cycle1 = 0;
47
+int messungen = 700;
48
+
49
+
50
+// The setup function simply starts serial and initializes the
51
+//  accelerometer.
52
+void setup()
53
+{
54
+  Serial.begin(9600);
55
+  Serial.println("MMA8452Q Test Code!");
56
+  
57
+  // Choose your adventure! There are a few options when it comes
58
+  // to initializing the MMA8452Q:
59
+  //  1. Default init. This will set the accelerometer up
60
+  //     with a full-scale range of +/-2g, and an output data rate
61
+  //     of 800 Hz (fastest).
62
+  //accel.init();
63
+  //  2. Initialize with FULL-SCALE setting. You can set the scale
64
+  //     using either SCALE_2G, SCALE_4G, or SCALE_8G as the value.
65
+  //     That'll set the scale to +/-2g, 4g, or 8g respectively.
66
+  //accel.init(SCALE_4G); // Uncomment this out if you'd like
67
+  //  3. Initialize with FULL-SCALE and DATA RATE setting. If you
68
+  //     want control over how fast your accelerometer produces
69
+  //     data use one of the following options in the second param:
70
+  //     ODR_800, ODR_400, ODR_200, ODR_100, ODR_50, ODR_12,
71
+  //     ODR_6, or ODR_1. 
72
+  //     Sets to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz.
73
+  accel.init(SCALE_8G);
74
+}
75
+
76
+// The loop function will simply check for new data from the
77
+//  accelerometer and print it out if it's available.
78
+void loop()
79
+{
80
+
81
+
82
+buttonState = digitalRead(buttonPin);
83
+  if (buttonState == HIGH) {
84
+    digitalWrite(ledPin, HIGH);
85
+
86
+    for (int cycle = 0 ; cycle < messungen; cycle = cycle + 3) {
87
+      delay (100);
88
+      
89
+      //Serial.print (i);
90
+      //Serial.print("\t");
91
+      //Serial.print(cycle);
92
+      //Serial.print("\t");
93
+      Serial.print (xyz[cycle]);
94
+      Serial.print("\t");
95
+      Serial.print (xyz[cycle + 1]);
96
+      Serial.print("\t");
97
+      Serial.print (xyz[cycle + 2]);
98
+      Serial.print("\t");
99
+      Serial.print (orient[cycle/3]);
100
+      Serial.print("\t");
101
+      
102
+      Serial.println(); // Print new line every time.
103
+
104
+    }
105
+    Serial.print ("Daten zu Ende");
106
+    // delay(1000);
107
+    // cycle = 0 ;
108
+digitalWrite(ledPin, LOW);
109
+  } else {
110
+    
111
+
112
+
113
+  
114
+  // Use the accel.available() function to wait for new data
115
+  //  from the accelerometer.
116
+  if (accel.available())
117
+  {
118
+    // First, use accel.read() to read the new variables:
119
+    accel.read();
120
+    
121
+    // accel.read() will update two sets of variables. 
122
+    // * int's x, y, and z will store the signed 12-bit values 
123
+    //   read out of the accelerometer.
124
+    // * floats cx, cy, and cz will store the calculated 
125
+    //   acceleration from those 12-bit values. These variables 
126
+    //   are in units of g's.
127
+    // Check the two function declarations below for an example
128
+    // of how to use these variables.
129
+    if (cycle1 < messungen) {
130
+      cycle1 = cycle1 +3;
131
+    printCalculatedAccels();
132
+    }
133
+    //printAccels(); // Uncomment to print digital readings
134
+    
135
+    // The library also supports the portrait/landscape detection
136
+    //  of the MMA8452Q. Check out this function declaration for
137
+    //  an example of how to use that.
138
+    //printOrientation();
139
+    
140
+ //   Serial.println(); // Print new line every time.
141
+  }
142
+}
143
+}
144
+
145
+// The function demonstrates how to use the accel.x, accel.y and
146
+//  accel.z variables.
147
+// Before using these variables you must call the accel.read()
148
+//  function!
149
+void printAccels()
150
+{
151
+  Serial.print(accel.x, 3);
152
+  Serial.print("\t");
153
+  Serial.print(accel.y, 3);
154
+  Serial.print("\t");
155
+  Serial.print(accel.z, 3);
156
+  Serial.print("\t");
157
+}
158
+
159
+// This function demonstrates how to use the accel.cx, accel.cy,
160
+//  and accel.cz variables.
161
+// Before using these variables you must call the accel.read()
162
+//  function!
163
+/************************
164
+void printCalculatedAccels()
165
+{ 
166
+  Serial.print(accel.cx, 3);
167
+  Serial.print("\t");
168
+  Serial.print(accel.cy, 3);
169
+  Serial.print("\t");
170
+  Serial.print(accel.cz, 3);
171
+  Serial.print("\t");
172
+}
173
+**************************/
174
+
175
+void printCalculatedAccels()
176
+{
177
+  messungen = 700; //700
178
+  cycle=cycle + 3;
179
+  delay(100);
180
+    xyz[cycle] = (accel.x);
181
+    //  Serial.print("\t");
182
+    xyz[cycle + 1] = (accel.y);
183
+    //  Serial.print("\t");
184
+    xyz[cycle + 2] = (accel.z);
185
+    // Serial.print("\t");
186
+    orient[cycle/3]= accel.readPL();
187
+Serial.print (xyz[cycle]);
188
+Serial.print("\t"); 
189
+Serial.print (xyz[cycle + 1]);
190
+Serial.print("\t");
191
+Serial.print (xyz[cycle + 2]);
192
+Serial.print("\t");
193
+Serial.print (orient[cycle/3]);
194
+// Serial.print("\t");
195
+// Serial.print(cycle);
196
+Serial.print("\n");
197
+
198
+if (cycle > messungen){
199
+  return;
200
+  }
201
+    
202
+    }
203
+
204
+
205
+
206
+
207
+
208
+
209
+// This function demonstrates how to use the accel.readPL()
210
+// function, which reads the portrait/landscape status of the
211
+// sensor.
212
+void printOrientation()
213
+{
214
+  // accel.readPL() will return a byte containing information
215
+  // about the orientation of the sensor. It will be either
216
+  // PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
217
+  // LOCKOUT.
218
+  byte pl = accel.readPL();
219
+  switch (pl)
220
+  {
221
+  case PORTRAIT_U:
222
+    Serial.print("Portrait Up");
223
+    break;
224
+  case PORTRAIT_D:
225
+    Serial.print("Portrait Down");
226
+    break;
227
+  case LANDSCAPE_R:
228
+    Serial.print("Landscape Right");
229
+    break;
230
+  case LANDSCAPE_L:
231
+    Serial.print("Landscape Left");
232
+    break;
233
+  case LOCKOUT:
234
+    Serial.print("Flat");
235
+    break;
236
+  }
237
+}