In the previous project, we started creating an accelerometer application that recorded each time the CREATOR Kit is tapped. One issue, though, was that the program was too sensitive — it recorded too many taps.

In this project, we'll filter out all of those extra taps. To do this, we'll look at any group of taps that were registered close together in a short time period, and assume that they were all part of a single tap.

For this project, we'll only need the accelerometer and speaker. Here is what your breadboard should look like:

Here is what the code for this project should look like at this point (with new changes highlighted with arrows):

from rstem.accel import Accel from rstem.sound import Note import time accel = Accel() # Calibrate z_rest = 0 SAMPLES = 100 for i in range(SAMPLES): x, y, z = accel.forces() z_rest += z time.sleep(0.01) z_rest /= SAMPLES # Beep to tell user we're starting recording beep = Note('A6') beep.play(0.2).wait() # Record taps TOTAL_TICKS = 2000 raw_tap_ticks = [] for i in range(TOTAL_TICKS): x, y, z = accel.forces() if abs(z_rest-z) > 0.1: raw_tap_ticks.append(i) time.sleep(0.001) # Beep to tell user we've stopped recording beep.play(0.2).wait() *# Convert raw ticks to filtered ticks *filtered_tap_ticks = [] *previous_tick = None *for tick in raw_tap_ticks: * if previous_tick == None or tick - previous_tick > 20: * filtered_tap_ticks.append(tick) * previous_tick = tick * *# Convert filtered ticks to relative ticks *relative_tap_ticks = [] *previous_tick = filtered_tap_ticks[0] *for tick in filtered_tap_ticks: * relative_tap_ticks.append(tick - previous_tick) * previous_tick = tick * *print(raw_tap_ticks) *print(filtered_tap_ticks) *print(relative_tap_ticks)

Here are the new additions that we've made to the code and highlighted above:

- On Lines 33 through 38, we filter out all the extra taps that occur from the vibration of the kit. In a `for` loop, we make a new list `filtered_tap_ticks` that does not include any taps that were 20 ticks or less from the previous tick.

- On Lines 41 through 45, we make the ticks relative to the previous tick. That is, before, the tick was the time from when recording started until the tap occurred. After this code, we'll have a new list `relative_tap_ticks` that is the time in ticks from the previous tap.

- On Lines 47 through 49, we print out the taps in each of the list that we've made.

When you try this, you'll see three sets of numbers printed in the Output Window. The first is the list of raw taps, the same as what we saw in the previous project. The second set of numbers is the filtered taps, which removes all the taps close in time, and leaves us with the start time of each new tap. Finally, we have the relative tap timing, which tells us the amount of time between each successive tap.

1. We used the arbitrary number 20 to filter out extra taps. If the number is too small, then it we won't filter out all of the extra taps. How small can this number be so that when you tap the kit once, you still see only one `filtered_tap_ticks`?

2. You can also increase the tap filter number (20). However, if the number is too large, then two fast taps in a row will only be recognized as one tap. How high can you increase the number so that the program still recognizes individual taps when you record two fast taps in a row.