LoginSignup
12
12

More than 5 years have passed since last update.

Arduino x Ruby でLEDを光らせる

Posted at

まずはこちらを参考にやってみることにした。
Introducing Arduino & Dino

まずはLEDを光らせる

まずは通電しているかチェック。

無事光りました。
では、次。

環境構築

Dinoのインストール

/Users/yoshidayuuya% gem install dino
Fetching: serialport-1.1.0.gem (100%)
Building native extensions.  This could take a while...
Fetching: dino-0.10.0.gem (100%)
Successfully installed serialport-1.1.0
Successfully installed dino-0.10.0
2 gems installed
Installing ri documentation for serialport-1.1.0...
Installing ri documentation for dino-0.10.0...
Installing RDoc documentation for serialport-1.1.0...
Installing RDoc documentation for dino-0.10.0...

Arduinoの環境構築

from Getting Started w/ Arduino on Mac OS X

  • MacOSX用のArduino Softwareをダウンロード
  • Zipを解凍し、出てきたアプリをアプリケーションへ
  • アプリを起動し、https://raw.github.com/austinbv/dino/master/src/du.ino をダウンロードして開く
  • マイコンボードに書き込むを実行
    • 基板の"L"の横にあるLEDが点滅していたが点灯に変わった

LEDを光らせる

ボード上のLEDは13番PINに繋がっているそうです。サンプルを実行して光らせてみました。

blink.rb
require 'dino'

board = Dino::Board.new(Dino::TxRx.new)
led = Dino::Components::Led.new(pin: 13, board: board)

[:on, :off].cycle do |switch|
  led.send(switch)
  puts switch
  sleep 1
end

光りました〜。
これだとRubyらしくないので、pryを使ってみましょう。

gem install pryでインストールし、pryで実行。
実行したら、上記のサンプルコードを順々に打ちこみます。
これでインタラクティブにソースを実行させることができます。

では、早速お遊び。2つのLEDを交互に光らせてみます。

赤いLEDと緑のLEDを用意し、ボードに差し込みます。
それぞれの+側はボードの11番と12番へ差し込みます。それぞれのー側はブレッドボードのマイナスへつなげてしまいます。最後にブレッドボードのマイナスから基板の"GNDへつなげて完成。(回路の基本は、マイナスをすべてGNDへ戻すこと。)
結線は以上。

プログラムは先ほどのものを改造しました。

02_blinks.rb
require 'dino'

board = Dino::Board.new(Dino::TxRx.new)
green_led = Dino::Components::Led.new(pin: 11, board: board)
red_led = Dino::Components::Led.new(pin: 12, board: board)

[[:on, :off],[:off,:on]].cycle do |switch|
  red_led.send(switch[0])
  green_led.send(switch[1])
  p switch
  sleep 1
end

はい、完成。実行してみましょう。

なんだか妙に楽しいですな。

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12