Created
May 15, 2013 20:29
-
-
Save peterhellberg/5587104 to your computer and use it in GitHub Desktop.
Controlling an Arduino using the Dino gem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'dino' | |
| # Slightly nicer interface to the Arduino :) | |
| module Dino | |
| class << self | |
| def board | |
| @board ||= Board.new TxRx.new | |
| end | |
| def method_missing(m, pin) | |
| klass = const_get("Components::#{m.capitalize}") | |
| klass.new pin: pin, board: board | |
| end | |
| end | |
| end | |
| # Get an array of leds | |
| @leds = Dino.led(10), Dino.led(11) | |
| # Control multiple leds | |
| def turn(*states) | |
| [email protected]_with_index do |led,i| | |
| led.send(states[i]) | |
| end | |
| end | |
| # Turn off the leds before exiting | |
| trap("SIGINT") { exit turn :off, :off } | |
| # Handle the light sensor data | |
| Dino.sensor('A0').when_data_received -> data do | |
| case data.to_i | |
| when 900..1024 then turn :off, :off | |
| when 810..899 then turn :on, :off | |
| else turn :on, :on | |
| end | |
| end | |
| # Sleep forever | |
| sleep |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that
when_data_recievedwill take a block instead of a lambda when 0.12 of the gem is released.