From 3928ef91d414147b7c2b5441a5ce4ff718c46edd Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 27 Nov 2013 12:13:44 -0800 Subject: [PATCH] Literate CoffeeScript version of led_brightness example --- .../led_brightness/led_brightness.litcoffee | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/led_brightness/led_brightness.litcoffee diff --git a/examples/led_brightness/led_brightness.litcoffee b/examples/led_brightness/led_brightness.litcoffee new file mode 100644 index 0000000..f825db2 --- /dev/null +++ b/examples/led_brightness/led_brightness.litcoffee @@ -0,0 +1,43 @@ +# LED Brightness + +For this example, we'll be using an LED on an Arduino board, and modifying it's +brightness to make it fade in and out. Before we start, make sure you've got the +`cylon-arduino` module installed. + +Let's start by importing Cylon: + + Cylon = require '../..' + +Once we've got that, we can start defining our robot: + + Cylon.robot + +We'll be using an Arduino, and communicating over the Firmata protocol. As well, +we'll let our robot know about the LED we'll be modifying, on pin #3 of the +Arduino. + + connection: { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' } + device: { name: 'led', driver: 'led', pin: 3 } + +Next up, we'll define our robot's work: + + work: (my) -> + +We'll set some variables here: brightness will represent the LED's brightness, +from 1-255, and 'fade' will be the brightness change on each tick. + + brightness = 0 + fade = 5 + +Every 50 milliseconds, we'll be incrementing the brightness by `fade`'s value, +setting the LED to that brightness, and reversing `fade`'s value if brightness +hits 0 or 255. + + every 0.05.seconds(), -> + brightness += fade + my.led.brightness(brightness) + fade = -fade if (brightness is 0) or (brightness is 255) + +And with that done, we can now start our robot. + + .start()