Showing
1 changed file
with
79 additions
and
0 deletions
arduino/arduino.ino
0 → 100644
1 | +int actu = 6; | ||
2 | + | ||
3 | + | ||
4 | +const int xInput = A0; | ||
5 | +const int yInput = A1; | ||
6 | +const int zInput = A2; | ||
7 | +// initialize minimum and maximum Raw Ranges for each axis | ||
8 | +int RawMin = 0; | ||
9 | +int RawMax = 1023; | ||
10 | +int temp_x = 0; | ||
11 | +int temp_y = 0; | ||
12 | +int temp_z = 0; | ||
13 | + | ||
14 | +// Take multiple samples to reduce noise | ||
15 | +const int sampleSize = 10; | ||
16 | + | ||
17 | +void setup() | ||
18 | +{ | ||
19 | + pinMode(actu,OUTPUT); | ||
20 | + analogReference(EXTERNAL); | ||
21 | + Serial.begin(115200); | ||
22 | +} | ||
23 | + | ||
24 | +void loop() | ||
25 | +{ | ||
26 | + //Read raw values | ||
27 | + int xRaw = ReadAxis(xInput)-temp_x; | ||
28 | + int yRaw = ReadAxis(yInput)-temp_y; | ||
29 | + int zRaw = ReadAxis(zInput)-temp_z; | ||
30 | + int result = (abs(xRaw) +abs(yRaw) + abs(zRaw))/3; | ||
31 | + temp_x = ReadAxis(xInput); | ||
32 | + temp_y = ReadAxis(yInput); | ||
33 | + temp_z = ReadAxis(zInput); | ||
34 | + | ||
35 | + // Convert raw values to 'milli-Gs" | ||
36 | + long xScaled = map(xRaw, RawMin, RawMax, -3000, 3000); | ||
37 | + long yScaled = map(yRaw, RawMin, RawMax, -3000, 3000); | ||
38 | + long zScaled = map(zRaw, RawMin, RawMax, -3000, 3000); | ||
39 | + | ||
40 | + // re-scale to fractional Gs | ||
41 | + float xAccel = xScaled / 1000.0; | ||
42 | + float yAccel = yScaled / 1000.0; | ||
43 | + float zAccel = zScaled / 1000.0; | ||
44 | + | ||
45 | + Serial.print("X, Y, Z :: "); | ||
46 | + Serial.print(xRaw); | ||
47 | + Serial.print(", "); | ||
48 | + Serial.print(yRaw); | ||
49 | + Serial.print(", "); | ||
50 | + Serial.print(zRaw); | ||
51 | + Serial.print(" :: "); | ||
52 | + Serial.print(xAccel,0); | ||
53 | + Serial.print("G, "); | ||
54 | + Serial.print(yAccel,0); | ||
55 | + Serial.print("G, "); | ||
56 | + Serial.print(zAccel,0); | ||
57 | + Serial.print("G"); | ||
58 | + Serial.print(" :: "); | ||
59 | + Serial.println(result); | ||
60 | + if (result < 0){ | ||
61 | + result = 0 ; | ||
62 | + | ||
63 | + } | ||
64 | + analogWrite(actu,result-3); | ||
65 | + delay(50); | ||
66 | +} | ||
67 | + | ||
68 | +// Take samples and return the average | ||
69 | +int ReadAxis(int axisPin) | ||
70 | +{ | ||
71 | + long reading = 0; | ||
72 | + analogRead(axisPin); | ||
73 | + delay(1); | ||
74 | + for (int i = 0; i < sampleSize; i++) | ||
75 | + { | ||
76 | + reading += analogRead(axisPin); | ||
77 | + } | ||
78 | + return reading/sampleSize; | ||
79 | +} |
-
Please register or login to post a comment