quantum
quantum

Reputation: 1420

Accelerometer shake make sound only once?

I'm trying to implement a function that detects a user shaking his/her iPhone, and then the app will make a barking sound. So far I've got the code attached below, and it works. But, if I shake the phone harder, it makes 2 or more barks one immediately after the other. How do I make sure this only happens once per shake, no matter how hard?

- (void)accelerometer:(UIAccelerometer *)accelerometer 
    didAccelerate:(UIAcceleration *)acceleration {
        if (acceleration.x > kAccelerationThreshold ||
            acceleration.y > kAccelerationThreshold ||
            acceleration.z > kAccelerationThreshold) {
            // BARKING SOUND
        }
    }

Upvotes: 1

Views: 933

Answers (3)

Epsilon Prime
Epsilon Prime

Reputation: 4586

Why not keep a value around with the last time you played the sound?

time_t last_time_played_bark = 0;


#define MINIMUM_TIME_BETWEEN_BARKS 5   // in seconds
time_t now = time(NULL);

if (last_time_played_bark + MINIMUM_TIME_BETWEEN_BARKS < now) {
    // BARKING_SOUND
    last_time_played_bark = now;
}

Presumably you'll want MINIMUM_TIME_BETWEEN_BARKS to be at least as long as it takes to play the sound.

Upvotes: 3

Sixten Otto
Sixten Otto

Reputation: 14816

As a side note, if you just want to detect shake gestures, you might find it easier to use the motion events added in OS 3.0 than trying to roll your own from raw accelerometer data.

I agree with Epsilon Prime's suggestion to store the timestamp of the last time you played the sound as a means to throttle how often you play it.

Upvotes: 1

iYassin
iYassin

Reputation: 572

You could use an NSTimer - look here: section "Timer"

After playing the sound, set a boolean variable called "hasBarked" to YES and call the timer then. After two seconds, the timer sets "hasBarked" to NO and disables itself. Finally, put the whole sound-playing method into an if block that only plays the sound if "hasBarked" is NO.

Upvotes: 5

Related Questions