WiiFlash Tip #2 – Smoothing out the edges, part 2

.crux. added a comment about a different way to smooth out the data from the Wii controller – create an array of the last 10 values and average them. I’ve modified my code to try out this idea, and it’s not too bad. The data does wobble a little, but it’s more precise than my method. If you increase the size of the array, it gets smoother, but will also increase the cpu power required. Partial code below.

 private var WiiRollDegAry:Array = new Array();

public function get roll():int {
return averageIntArray(WiiRollDegAry);
}

private function updateData(pEvt:WiimoteEvent):void {
WiiRollDegAry = addValueToLimitedArry(WiiRollDegAry, int(TheWiimote.roll * TO_DEG));
}

private function addValueToLimitedArry(a:Array, v:int):Array{
a.push(v);
// increase to smooth out data
if (a.length > 10) a.shift();
return a;
}

private function averageIntArray(a:Array):int {
var len:int = a.length;
var c:int = 0;
for (var i:int = 0; i < len; i++) {
c += int(a[i]);
}
return int(c / len);
}

WiiFlash Tip #3 – Blindly pointing

Not so much a tip but a point in the right direction. These tips are really out of order!

Once you have WiiFlash working and you have your senor bar, you need be able to read X and Y values based on where you’re pointing the controller. I tried a few approaches for this, but none worked better than the code that Andrés Santos developed. Check out his wiimoteIR class and demo code. I’m using a very slighly modified version of it in my classes and it’s working great.