MotionSensor/Buffering/Buffering.ino
const unsigned int
Line 1
const unsigned int
-
const unsigned int
-
const unsigned int
-
const unsigned int
5
X_AXIS_PIN, Y_AXIS_PIN, Z_AXIS_PIN
-
};
-
const unsigned int
-
const unsigned int
-
int
buffer[NUM_AXES][BUFFER_SIZE];
10
int
buffer_pos[NUM_AXES] = { 0 };
-
-
void
setup() {
-
Serial.begin(BAUD_RATE);
-
}
15
-
int get_axis(const int
-
delay(1);
-
buffer[axis][buffer_pos[axis]] = analogRead(PINS[axis]);
-
buffer_pos[axis] = (buffer_pos[axis] + 1) % BUFFER_SIZE;
20
long
sum = 0;
-
for (unsigned int
-
sum += buffer[axis][i];
-
return
round(sum / BUFFER_SIZE);
-
}
25
-
int
get_x() {
return
-
int
get_y() {
return
-
int
get_z() {
return
-
void
loop() {
30
Serial.print(get_x());
-
Serial.print("
-
Serial.print(get_y());
-
Serial.print("
-
Serial.println(get_z());
35
}
-
As usual, we define some constants for the pins we use first. This time, we
also define a constant named
are measuring. We also have an array named
pins we use. This will help us keep our code more generic later.
In line 10, we declare buffers for all axes. They will be filled with the sensor
data we measure, so we can calculate average values when we need them.
We have to store our current position in each buffer, so in line 11, we define
an array of buffer positions.
setup
only initializes the serial port; the real action takes place in
X_AXIS_PIN = 2;
Y_AXIS_PIN = 1;
Z_AXIS_PIN = 0;
NUM_AXES = 3;
PINS[NUM_AXES] = {
BUFFER_SIZE = 16;
BAUD_RATE = 9600;
axis) {
i = 0; i < BUFFER_SIZE; i++)
get_axis(0); }
get_axis(1); }
get_axis(2); }
");
");
NUM_AXES
www.it-ebooks.info
Finding and Polishing Edge Values
that contains the number of axes we
PINS
that contains a list of the
105
get_axis
.
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?