Detecting orientation changes

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, ...

Get Hands-On Internet of Things with MQTT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.