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
| #ifdef ARDUINO_AVR_UNO
const int pX = 0;
const int pY = 1;
const int pZ = 2;
#endif
#ifdef ARDUINO_AVR_NANO
const int pX = 1;
const int pY = 2;
const int pZ = 3;
#endif
static char gBuffer[1024];
#define dAveQueueSize 30
static int gAveQueueX[dAveQueueSize];
static int gAveQueueY[dAveQueueSize];
static int gAveQueueZ[dAveQueueSize];
static int gAveQueueIndex = 0;
static int gAveX = 0;
static int gAveY = 0;
static int gAveZ = 0;
////////////////////////////////////////////////////////////////
void fInit() {
for(int i = 0; i < dAveQueueSize; i++) {
gAveQueueX[i] = 0;
gAveQueueY[i] = 0;
gAveQueueZ[i] = 0;
}
}
#define dAnalogOffset 512
void fSet(int x, int y, int z) {
gAveX -= gAveQueueX[gAveQueueIndex];
gAveY -= gAveQueueY[gAveQueueIndex];
gAveZ -= gAveQueueZ[gAveQueueIndex];
gAveQueueX[gAveQueueIndex] = (x - dAnalogOffset) / dAveQueueSize;
gAveQueueY[gAveQueueIndex] = (y - dAnalogOffset) / dAveQueueSize;
gAveQueueZ[gAveQueueIndex] = (z - dAnalogOffset) / dAveQueueSize;
gAveX += gAveQueueX[gAveQueueIndex];
gAveY += gAveQueueY[gAveQueueIndex];
gAveZ += gAveQueueZ[gAveQueueIndex];
gAveQueueIndex++;
if (gAveQueueIndex >= dAveQueueSize) gAveQueueIndex = 0;
}
////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
Serial.println("#AccSensor.Start");
Serial.println("#Format: Ax,Ay,Az");
#ifdef ARDUINO_AVR_UNO
Serial.println("#Arduino: ARDUINO_AVR_UNO");
#endif
#ifdef ARDUINO_AVR_NANO
Serial.println("#Arduino: ARDUINO_AVR_NANO");
#endif
fInit();
}
////////////////////////////////////////////////////////////////
void loop() {
int x = analogRead(pX);
int y = analogRead(pY);
int z = analogRead(pZ);
fSet(x,y,z);
snprintf(gBuffer, sizeof(gBuffer),
"%d, %d, %d",
gAveX, gAveY, gAveZ
);
Serial.println(gBuffer);
}
////////////////////////////////////////////////////////////////
|