The easiest approach to detect orientation changes of our cube would be to place the following code at the end of our loop function:
if (previousSide != currentSide) {
Serial.print("Side: "); Serial.println(currentSide);
previousSide = currentSide;
}
But the easiest approach is not always the best one. The problem with this approach is that when we switch sides, another side might be detected in between. For example, let's say the cube was lying on the bottom side and you wanted to place it on its top side. While rotating it, it would (for a very short time) detect that it is placed on the side in between, even though we are just rotating it by hand and it does not rest on this side.
To overcome this problem, we should only detect a side change when the last x values were all recorded on the same side. To get this right, we...