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
@krainboltgreene
Copy link

  1. If you want to make an empty array, you can simply say Array.new(4).
  2. You shouldn't need to use Object#send() on line 7 and 11.
  3. You might want to move the bit_array check into it's own method.
  4. Same for the 8.times sending.

@crosson
Copy link
Author

crosson commented Jan 3, 2013

Thanks! I'll post an update.

@crosson
Copy link
Author

crosson commented Jan 3, 2013

I wasn't quite sure how to accomplish what you were recommending. I tried to flatten out the 8.times into a blink method.

@krainboltgreene
Copy link

Good job so far, but lets go further:

def blink(leds, bit_array, status = :on)
  8.times do |bit|
    leds[bit].send(status) if bit_array[bit] == "1"
  end
end

Could be

def blink(leds, bit_array, status = :on)
  8.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

Now we can test/refactor that check without having to search/replace, and use it in other places too!

Seems like this code is repetitive:

LEDS = [Dino::Components::Led.new(pin: 13, board: BOARD), 
        Dino::Components::Led.new(pin: 12, board: BOARD), 
        Dino::Components::Led.new(pin: 11, board: BOARD), 
        Dino::Components::Led.new(pin: 10, board: BOARD)]
# NOTE: Here we're creating a method that creates new Dino::Components::Led objects
# Notice how the board parameter has a weird syntax. We're saying the default value of
# board is the value of the constant BOARD
def new_led(pin, board = BOARD)
  Dino::Components::Led.new pin: pin, board: board
end


LEDS = [new_led(13), new_led(12), new_led(11), new_led(10)]

We can further see some more repetitive code in the new source:

# NOTE: map goes over each item in the array, evalutes the block with that item, and then returns 
# an array with the values of each block call
LEDS = [13,12,11,10].map do |pin|
  new_led pin
end

Ah-ha, but Ruby has a cool syntax that further cleans this up:

LEDS = [13, 12, 11, 10].map &method(:new_led)

Tada! We went from:

LEDS = [Dino::Components::Led.new(pin: 13, board: BOARD), 
        Dino::Components::Led.new(pin: 12, board: BOARD), 
        Dino::Components::Led.new(pin: 11, board: BOARD), 
        Dino::Components::Led.new(pin: 10, board: BOARD)]

def blink(leds, bit_array, status = :on)
  8.times do |bit|
    leds[bit].send(status) if bit_array[bit] == "1"
  end
end

To:

def new_led(pin, board = BOARD)
  Dino::Components::Led.new pin: pin, board: board
end

def blink(leds, bit_array, status = :on)
  8.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

LEDS = [13, 12, 11, 10].map &method(:new_led)

You'll start to notice some patterns though, like that we're passing a fair amount of arguments tot hese methods. This is indicative of the need for a class with attributes.

@krainboltgreene
Copy link

With a class, by the way, we could probably clean up other bits of the code too.

@bhelx
Copy link

bhelx commented Jan 4, 2013

Can't you just write the byte directly to the register? That would be one line of code.

@crosson
Copy link
Author

crosson commented Jan 4, 2013

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?

@krainboltgreene
Copy link

@crosson: Go right ahead

@bhelx: I'm assuming some sort of restriction requiring this.

@bhelx
Copy link

bhelx commented Jan 4, 2013

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

@crosson
Copy link
Author

crosson commented Jan 6, 2013

@bhelx. I'd have to look into it. I only just started playing with this. I built this out of reading BLINK tutorials.

@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