The code presented on this page can be downloaded or found in the Arduino examples menu under Examples → SpinWearables → Tilt_Sensor → Fancy
.
#include "SpinWearables.h"
using namespace SpinWearables;
void setup() {
Ensure all of the SpinWheel hardware is on.
.begin();
SpinWheel}
void loop() {
Read all sensor data.
.readIMU(); SpinWheel
Scale the x and y measurement to a -255..255 range.
int x = SpinWheel.ax*255;
int y = SpinWheel.ay*255;
Turn all large LEDs white.
.setLargeLEDsUniform(0xffffff); SpinWheel
If the tilt is in a given direction, turn on the corresponding LED purple.
if (x>10) {
.setLargeLED(7, x, 0, x);
SpinWheel.setLargeLED(3, x, 0, x);
SpinWheel}
else if (x<-10) {
.setLargeLED(5, -x, 0, -x);
SpinWheel.setLargeLED(1, -x, 0, -x);
SpinWheel}
Do the same for the Y orientation
if (y>10) {
.setLargeLED(6, x, 0, x);
SpinWheel.setLargeLED(2, x, 0, x);
SpinWheel}
else if (y<-10) {
.setLargeLED(4, -x, 0, -x);
SpinWheel.setLargeLED(0, -x, 0, -x);
SpinWheel}
Use the setSmallLEDsPointer
to turn on only the small LEDs which are at the top. This requires some tricky inverse trigonometry, where we use the arctan function to turn the ay
and ax
measurements into an angle. Then we rescale that angle to fit the 0..255 range used by setSmallLEDsPointer
.
uint8_t angle = (-atan2(SpinWheel.ay, SpinWheel.ax)+3.1415/2)/2/3.1415*255;
.setSmallLEDsPointer(angle, 0xffffff); SpinWheel
Turn on the LEDs as commanded above.
.drawFrame();
SpinWheel}