# Set the Nunchuck tap detector parameters and initialize tap event history list
TAP_THRESHOLD = 350  # Tap sensitivity threshold; depends on the physical sensor mount
TAP_DEBOUNCE = 0.2  # Time for accelerometer to settle after tap (seconds)
TAP_TIMEOUT = 1500  # Remove tap event from history timeout (milliseconds)
tap_events = []  # Initialize the tap event history list

# The project's main while looop
while True:
    # Detect a tap on the x-axis of the Nunchuck accelerometer
    accel_sample_1 = nunchuck.acceleration  # Read one sample
    accel_sample_2 = nunchuck.acceleration  # Read the next sample
    if abs(accel_sample_1 - accel_sample_2) >= TAP_THRESHOLD:
        # The difference between two consecutive samples exceeded the threshold
        # (equivalent to a high-pass filter)
        print(f"SINGLE tap detected {ticks_ms()}")
        tap_events.append(ticks_ms() + TAP_TIMEOUT)  # save tap expiration time in event stack
        time.sleep(TAP_DEBOUNCE)  # Debounce delay

    # Clean up tap event history after timeout period expires
    if len(tap_events) > 0:
        # Check for expired events
        if tap_events[0] <= ticks_ms():
            # The oldest event has expired
            tap_events = tap_events[1:]  # Remove the oldest event

    # Check see if two taps are in the event history list
    if len(tap_events) == 2:
        # Double-tap: execute the task and clear event history
        print(f"DOUBLE tap detected {ticks_ms()}")
        # 
        # Perform the double-tap task here
        # 
        tap_events = []  # Clear event history