Skip to content

Instantly share code, notes, and snippets.

@crosson
Last active December 10, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crosson/4446111 to your computer and use it in GitHub Desktop.
Save crosson/4446111 to your computer and use it in GitHub Desktop.
Learning how to use the Arduino with the Dino gem. This is the start of a binary counter. Maybe will eventually become a binary clock. I only wired up 3 LEDS so I could only count to 7 in this example.
require 'dino'
def new_led(pin, board = BOARD)
Dino::Components::Led.new(:pin => pin, :board => board)
end
BOARD = Dino::Board.new(Dino::TxRx.new)
LEDS = [13, 12, 11, 10, 9].map do |pin|
new_led pin
end
def blink(leds, bit_array, status = :on)
LEDS.length.times do |bit|
leds[bit].send(status) if is_one? bit_array, bit
end
end
def is_one?(bit_array, bit)
bit_array[bit] == "1"
end
def num_bits_to_a(number)
binary_number = "%08b" % number
binary_number.reverse!
binary_number.split(//)
end
def bits_to_light(number)
bit_array = num_bits_to_a(number)
blink(LEDS, bit_array)
sleep 1
blink(LEDS, bit_array, :off)
end
(1..31).cycle do |n|
bits_to_light(n)
end
@crosson
Copy link
Author

crosson commented Jan 7, 2013

I am able to count to 31 since this starter kit came with 5 LEDS. I wanted to use pins 1 and 2 so that pin 1 would be LED 1 in my program but I couldn't get it to late. It is labeled TX and RX on the board I suspect I have some reading to do from here.

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