-
-
Save crosson/4446111 to your computer and use it in GitHub Desktop.
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 |
Can't you just write the byte directly to the register? That would be one line of code.
krainboltgreene, I've been taking the core ruby class at http://rubylearning.org/. Your notes above are amazing. I've been using ruby a little while but I seem to lag behind in figuring out all of these elegant solutions. Do you mind if I share your contributions with others in the class?
Well, I admit haven't read the Dino source so I couldn't say. In the arduino though, each pin is mapped to a bit on one of the registers available. Although you may have to shift and mask the number depending on which pins the leds are hooked up to and what is connected to the other pins, it should be as simple as doing this:
PORTB = number
Source -> Port Manipulation
@bhelx. I'd have to look into it. I only just started playing with this. I built this out of reading BLINK tutorials.
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.
With a class, by the way, we could probably clean up other bits of the code too.