LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

NodeBotsハンズオン 2018

Last updated at Posted at 2018-05-28

前置き。かなり緩いです。 1.5H~2Hくらい。
随時補足資料を使いながら話します。

はじめに - NodeBotsについて

まずはこちらの記事で概要を掴みましょう。

Webな人でもハードウェア制御が簡単に!Node.jsでJavaScript Roboticsを楽しもう #nodebots_jp

史上初!? NodeBotsの日本向けキットを作成しました! #nodebots

環境確認

ターミナルで

node -v

を実行してバージョンが表示されればOK

Johnny-Fiveのインストール

mkdir myj5
cd myj5
npm init -y
npm i johnny-five

Lチカ

  • app.jsファイルを作成
touch app.js

app.jsに以下を記述

app.js
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  var led = new five.Led(13);
  led.blink(500);
});

GNDと13ピンに配線します。

CdSセルを利用

  • CdSセル
    • 光の値によって抵抗が変化する素子

cds.jsを作成します。

cds.js
'use strict';
const five = require('johnny-five');

const board = new five.Board();

board.on('ready', () => {
    const s = new five.Sensor('A0');

    s.on('change', v => {
        console.log(v);
    });
})

チャレンジ

  • 暗くなったらLEDがひかる仕組みを作ってみよう

ブザーを利用

  • 圧電ブザー
    • 電圧を与えることで音がなる

beep.jsを作成します。

beep.js
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {

  // Create a standard `piezo` instance on pin 13
  var piezo = new five.Piezo(13);

  // Plays a song
  piezo.play({
    // song is composed by an array of pairs of notes and beats
    // The first argument is the note (null means "no note")
    // The second argument is the length of time (beat) of the note (or non-note)
    song: [
      ["C4", 1 / 4],
      ["D4", 1 / 4],
      ["F4", 1 / 4],
      ["D4", 1 / 4],
      ["A4", 1 / 4],
      [null, 1 / 4],
      ["A4", 1],
      ["G4", 1],
      [null, 1 / 2],
      ["C4", 1 / 4],
      ["D4", 1 / 4],
      ["F4", 1 / 4],
      ["D4", 1 / 4],
      ["G4", 1 / 4],
      [null, 1 / 4],
      ["G4", 1],
      ["F4", 1],
      [null, 1 / 2]
    ],
    tempo: 100
  });
});
onkai.js
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {

  // Create a standard `piezo` instance on pin 3
  var piezo = new five.Piezo(13);

  // Plays a song
  piezo.play({
    // song is composed by an array of pairs of notes and beats
    // The first argument is the note (null means "no note")
    // The second argument is the length of time (beat) of the note (or non-note)
    song: [
      ["C4", 1 / 4],
      ["D4", 1 / 4],
      ["E4", 1 / 4],
      ["F4", 1 / 4],
      ["G4", 1 / 4],
      ["A4", 1 / 4],
      ["B4", 1 / 4],
      ["C5", 1 / 4],
    ],
    tempo: 20
  });
});
  • Arduino版
#define BEAT 300   // 音の長さを指定
#define PINNO 13   // 圧電スピーカを接続したピン番号

void setup() {
}
void loop() {
     tone(PINNO,262,BEAT) ;  // ド
     delay(BEAT) ;
     tone(PINNO,294,BEAT) ;  // レ
     delay(BEAT) ;
     tone(PINNO,330,BEAT) ;  // ミ
     delay(BEAT) ;
     tone(PINNO,349,BEAT) ;  // ファ
     delay(BEAT) ;
     tone(PINNO,392,BEAT) ;  // ソ
     delay(BEAT) ;
     tone(PINNO,440,BEAT) ;  // ラ
     delay(BEAT) ;
     tone(PINNO,494,BEAT) ;  // シ
     delay(BEAT) ;
     tone(PINNO,523,BEAT) ;  // ド
     delay(3000) ;           // 3秒後に繰り返す
}

チャレンジ

  • 暗くなったら音がなる仕組みを作ってみよう

残りの時間はもくもく

他の関数も調べてみながらもくもくしてみよう。

まとめ

Johnny-Fiveを使うとJavaScriptだけで電子工作にチャレンジできます。

Webやソフトウェア出身の人からするとかなり嬉しいですね。

JSでネットワーク連携も出来るので、そのあたりまでいくとIoTな世界に入っていけます。

これをきっかけにチャレンジしてみてはいかがでしょうか。

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