If you want to take things a step further than our getting started tutorial and add an element of control to the Arduino, this short guide will cover just that; we're going to make the LED blink faster the more you tilt a hand forwards or backwards within the view of an active Leap Motion Controller!
Provided you have a Leap on hand, we can add this functionality simply by adding a couple of lines to the
Hello Shard that you just finished making in the previous example.
You'll want to add a new variable to the top of your Shard's JS file called
leap, like so:
create callback, we'll want to get a reference to any valid Leapmotion Controller using the following line:
leap = self.require("LeapmotionController");
And within the
update callback, we'll want to change the line containing
sleep(500) to be like the following:
self.sleep(Bundle.denormalize(50, 300, leap.get("pitch").invert()));
That long line of functions inside the
sleep method call might be slightly confusing - let's break it down:
Bundle.denormalize(50, 300, ...)converts the third parameter (a normalized value from -1.0 to 1.0) to a scale of 50 to 300 so that we can use it as a delay to control the blink rate of the LED.
leap.get("pitch")retrieves the current state of the specified axis on the Leapmotion Controller (the pitch of the first hand in view, in this case) as a normalized value in the range of -1.0 to 1.0.
.invert()inverts the value of the axis so that our LED will blink faster the more we tilt our hand forward, instead of slower.
With that, we're now ready to test your code! Go ahead save the file and launch your Alicorn server - if everything went well, you'll now be able to control the blink rate of the LED on port #13 on your Arduino simply by tilting your hand forwards and backwards.