Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created May 15, 2013 20:29
Show Gist options
  • Select an option

  • Save peterhellberg/5587104 to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/5587104 to your computer and use it in GitHub Desktop.
Controlling an Arduino using the Dino gem.
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
@peterhellberg
Copy link
Author

Note that when_data_recieved will take a block instead of a lambda when 0.12 of the gem is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment