I literally just changed the "250" to "SWING_THRESHOLD". It shouldn't affect swing detection. I think you can verify the issue by dropping the threshold in your own lightsaber.ino to 150 or less. It was never apparent before because 250 is a ridiculously high swing threshold that made it hard to trigger a single swing event, let alone a series of recurring swings.
The code basically checks "if moving faster than threshold, say we're swinging and disable new swings. If under threshold, say we're not swinging and re-enable swings" which sounds great. The problem is with a low threshold, anything that's going to rock the saber back and forth, like a really hard impact, or just swinging back and forth, is going to vary speed. Say I'm swinging left and right - eventually I'm going to have to stop moving left to move right, so the saber stops moving left - if only for a fraction of a second, so that it can start moving right, and at that moment, speed is low enough to re-enable swing sounds.
I think some sort of swing timer variable will fix the issue. Basically, somewhere declare a timer variable:
swing_timer = 0;
Then adjust the swing code to check that the timer is 0, and set the timer when a swing starts:
if (speed > SWING_THRESHOLD) {
if (!swinging_ && state_ != STATE_OFF && swing_timer == 0) {
swinging_ = true;
swing_timer = 20; //arbitrary value
Play(&swng);
}
} else {
swinging_ = false;
And then somewhere else, count down the timer.
if (swing_timer > 0) {
swing_timer--;
I'm just not sure if that's the best way to go about making a timer for this little microcontroller. I've only ever done game development for PC where there's plenty of CPU and memory to spare.