DEV Community

Valentino Stoll
Valentino Stoll

Posted on • Originally published at blog.codenamev.com on

1

Ruby Driven LED Arrays

This is part 3 in my Sunfounder Superkit series.

We’ve blinked it, hooked it to a button, and now for some serious fun: lots of them! This exercise really tests your patience with the breadboard.

(1 LED + 1 resistor + 2 wires) * 8 = 24 things to plug in

Once this was all setup (~15min), it looked great! I was really pumped to start playing around with so many twinkling lights!

Ruby Driven LED Arrays
Fully wired up LED array

The Ruby Journey Begins

So I crack open the python script and start to pipe it into ruby, because Ruby. I get to the line where it sets up the LEDs with rpi_gpio and what do I find? No multi-pin setup support.

GASP

I know what you’re thinking, “Just move on and test it with Python or C already”. Helllllll no! I didn’t make it all this way to have to mangle together some half-baked snake-oil code (in all fairness, the C code is quite nice to work with, and I did try it out before converting this **blush**).

It’s time to pony it up and ride the open source winds into the sunset.

Ride into the sunset

Fortunately, the Ruby documentation on creating extension libraries is really spectacular. With a little fiddling, I was able to get a working multi-pin GPIO.setup method, and PR submitted. Yay open source!

Before the PR merged, I had to build the gem locally ( rake compile) and reference it using the gem “rpi_gpio", path: /path/to/local/rpi_gpio in my Gemfile. I'll float by all the gripes I have with developing Ruby extensions and dive right into the goal: a real-life self-made majestic racing marquee!

#!/usr/bin/env ruby
require "rpi_gpio"
LED_PINS = [17, 18, 27, 22, 23, 24, 25, 4]
puts "========================================"
puts "| 8 LEDs |"
puts "| ------------------------------ |"
puts "| LED0 connect to GPIO0 |"
puts "| LED1 connect to GPIO1 |"
puts "| LED2 connect to GPIO2 |"
puts "| LED3 connect to GPIO3 |"
puts "| LED4 connect to GPIO4 |"
puts "| LED5 connect to GPIO5 |"
puts "| LED6 connect to GPIO6 |"
puts "| LED7 connect to GPIO7 |"
puts "| |"
puts "| Flow LED effect |"
puts "| |"
puts "| codenamev |"
puts "========================================\n"
puts "Program is running..."
puts "Please press Ctrl+C to end the program..."
puts "Press Enter to begin"
raw_input = STDIN.gets.chomp
@led_status_matrix = {}
LED_PINS.each { |pin| @led_status_matrix[pin] = "-" }
# Set the GPIO modes to BCM Numbering
RPi::GPIO.set_numbering :bcm
# Set all LedPin's mode to output, and initial level to High(3.3v)
RPi::GPIO.setup LED_PINS, as: :output, initialize: :high
def inline_puts(output)
STDOUT.write "\r#{output}"
STDOUT.flush
end
at_exit do
# Turn off all LEDs
RPi::GPIO.set_high LED_PINS
# Release resource
RPi::GPIO.clean_up
end
def flash_led(pin_number)
RPi::GPIO.set_low pin_number
@led_status_matrix[pin_number] = "0" # Show the LED is on
inline_puts @led_status_matrix.values.join('')
sleep 0.1
RPi::GPIO.set_high pin_number
@led_status_matrix[pin_number] = "-" # Show the LED is off
end
begin
loop do
# Turn LED off from left to right
LED_PINS.each do |pin_number|
flash_led(pin_number)
end
# Turn LED off from right to left
LED_PINS.reverse.each do |pin_number|
flash_led(pin_number)
end
end
rescue Interrupt, SignalException
exit
end
view raw eight_leds.rb hosted with ❤ by GitHub

So, that was a fun ride. The creative corner of my brain came knocking through the wall like the Kool-aid man though. I just have to spin this drab loop into something a little more fun and, dare I say, practical?

A Simple Binary Counter

Ok, not the most amazingly-fantastic-best-idea-ever, but I'm looking for a small twist to my existing example so I can escape the monotony of this textbook tutorial. With our new lovely Ruby script, we can make some small tweaks and have our LEDs count all the way to two hundred and fifty five (code here to save space).

Ruby Driven LED Arrays

Impressive, I know, but practical? Well my creativity was oozing today (with this many twinkling lights how could it not!?) and discovered how I might be able to turn this into my new favorite desk toy.

To the Future and Beyond

I'm a pretty big fan of the Pomodoro Technique, and since I now made something that can count, I'll likely jack this bad girl up and spin this sweet little counter into a meaningful meter for time spent. A visual Pomodoro Meter if you will.

Or maybe, instead of counting time, it could count the number of PRs I have open on GitHub. So many possibilities!

Well that's all for now folks. I hope you enjoyed the sunset. If you decide to boldly clone this and make something of your own I'd love to hear about it :-)

Tune in next week, where I venture into mutli-colored LED land and a slowly pulsing light show.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more