<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Takepepe.com &#187; Arduino</title>
	<atom:link href="https://takepepe.com/category/arduino/feed/" rel="self" type="application/rss+xml" />
	<link>https://takepepe.com</link>
	<description>Designer::develop</description>
	<lastBuildDate>Sun, 16 Jun 2013 15:40:24 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>openFrameworks x Arduino #001</title>
		<link>https://takepepe.com/openframeworks-x-arduino-001-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=openframeworks-x-arduino-001-2</link>
		<comments>https://takepepe.com/openframeworks-x-arduino-001-2/#comments</comments>
		<pubDate>Fri, 15 Mar 2013 15:28:06 +0000</pubDate>
		<dc:creator>Takepepe</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[openFrameworks]]></category>
		<category><![CDATA[ofArduino]]></category>
		<category><![CDATA[ofNoise]]></category>
		<category><![CDATA[ofxSimpleGuiToo]]></category>
		<category><![CDATA[PerlinNoise]]></category>

		<guid isPermaLink="false">http://takepepe.com/?p=332</guid>
		<description><![CDATA[openFrameworks x Arduino #001 from Takepepe on Vimeo. そ [...]]]></description>
				<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/61887524" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/61887524">openFrameworks x Arduino #001</a> from <a href="http://vimeo.com/user16458913">Takepepe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>そろそろこのアバターの格好で記事投稿するのが辛くなり始める今日この頃。<br />
皆様いかがお過ごしでしょうか。</p>
<p>今回はopenFrameworksをArduinoでコントロールするネタです。（Arduinoはほぼオマケですが…）<br />
動画ではひらひら揺らめく面と、再生している音楽が同期しています。<br />
赤外線センサーに手を近づけて、面の揺らめきと音楽のスピード・音量をコントロールします。</p>
<p>ArduinoはINとOUTを備えたマイコンボードの統合開発環境です。<br />
一般的なPCやガジェットには無いセンサーをコンテンツに取り込むだけでなく、<br />
LED、モーターや家電のコントロールも出来る素敵インターフェースです。</p>
<p><img src="/wp-content/uploads/2013/03/arduino.png" alt="Arduino" /></p>
<h6>Arduino　<a href="http://www.arduino.cc/" target="_blank">http://www.arduino.cc/</a></h6>
<p>Arduinoはボードにプログラムを書き込み、電源を供給すればスタンドアロンでも動かす事ができます。<br />
Processing・openFrameworksでも簡単に相互通信できるAPIが用意されています。<br />
とても面白いプラットフォームなのでいろんな記事を調べてみてください。</p>
<p>いつもの様に制作過程の流れで説明していきます。</p>
<ol>
<li>Arduinoボードの設定</li>
<li>openFrameworksとArduinoをつなぐ</li>
<li>ofxSimpleGuiTooを追加</li>
<li>3D空間にofCircleを描く</li>
<li>PerlinNoiseでゆらゆら</li>
<li>音楽を再生</li>
<li>Arduinoの値を摘要</li>
</ol>
<h2>1.Arduinoボードの設定</h2>
<p>今回のインスタレーションでは赤外線測距モジュール(GP2Y0A21)を使っています。<br />
参考というか、出版されている書籍に載っているものをそのままですw<br />
「5章 レシピ2：距離を測りたい」</p>
<h6>Prototyping Lab ―「作りながら考える」ためのArduino実践レシピ<br />
<a href="http://www.oreilly.co.jp/books/9784873114538/" target="_blank">http://www.oreilly.co.jp/books/9784873114538/</a></h6>
<p>まずは下記回路図の様にArduinoを組み立てます。</p>
<p><img src="/wp-content/uploads/2013/03/OA001_fzz.png" alt="Arduino" /></p>
<p>赤外線センサーから値が取れていることを確認します。<br />
以下のスケッチでは距離がcm単位でSerialに出力されます。</p>
<h3>OA001.ino</h3>
<pre class="brush: java; title: ; notranslate">


// アナログピン0番
const int sensorPin = 0;

// 閾値
const int threshold = 80;

void setup(){

	// 接続速度の設定
	Serial.begin(9600);

}

void loop(){

	int value = analogRead(sensorPin);
	if(value &gt; threshold){
		// cm単位に変換
		float range = (6787 / (value -3)) -4;
		Serial.println(range+&quot;cm&quot;);
	}

}
</pre>
<h2>2.openFrameworksとArduinoをつなぐ</h2>
<p>openFrameworksで値を取得する行程に入りますが、Arduinoでは先程のスケッチは使用しません。<br />
ArduinoボードにはStandardFirmataを書き込んでおきます。<br />
StandardFirmataは、色んなプラットフォームとArduinoを繋ぐものです。<br />
StandardFirmataのインストール方法とArduinoへの書き込み方法については割愛します。</p>
<p>openFrameworksのコードに移ります。
</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		ofArduino ard;
		float ardValue;
		float distance;
		void setupArduino(const int &amp; version);
		void analogPinChanged(const int &amp; pinNum);

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
// Arduinoとの接続速度
#define SPEED 57600

//--------------------------------------------------------------
void OA001::setup(){

	// Arduinoに接続
	ard.connect(&quot;/dev/cu.usbmodemfd321&quot;, SPEED);
	ofAddListener(ard.EInitialized, this, &amp;OA001::setupArduino);

}

//--------------------------------------------------------------
void OA001::setupArduino(const int &amp; version) {
    
	// イベントリスナ削除
	ofRemoveListener(ard.EInitialized, this, &amp;OA001::setupArduino);
	
	// アナログピン0番からのレポートを取得
	ard.sendAnalogPinReporting(0, ARD_ANALOG);
	ofAddListener(ard.EAnalogPinChanged, this, &amp;OA001::analogPinChanged);
	
	//cout &lt;&lt; ard.getFirmwareName() &lt;&lt; endl;
	//cout &lt;&lt; &quot;firmata v&quot; &lt;&lt; ard.getMajorFirmwareVersion() &lt;&lt; &quot;.&quot; &lt;&lt; ard.getMinorFirmwareVersion() &lt;&lt; endl;

}

//--------------------------------------------------------------
void OA001::analogPinChanged(const int &amp; pinNum) {

	int value = ard.getAnalog(pinNum);
	cout &lt;&lt; value &lt;&lt; endl;

}

//--------------------------------------------------------------
void OA001::update(){

	// Arduinoを更新
	ard.update();

}
</pre>
<p>コンソールにセンサー値が出力されたら接続成功です。<br />
このセンサー値は近くに何も無い場合、150以下で推移、最も手を近づけた時600強の値が出ます。<br />
純粋なセンサー値とは別に、この回で使用しているローパスフィルターをかけながら<br />
distanceに変換後の値を入れていきます。</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::analogPinChanged(const int &amp; pinNum) {

	// そのままのセンサー値を格納
	ardValue = ard.getAnalog(pinNum);

	// センサー値を調整して格納
	if(ardValue &lt; 100){
		distance = 100;
	}else if(ardValue &gt; 700){
		distance = 700;
	}else{
		// ローパスフィルターを摘要
		distance = distance*0.9+ardValue*0.1;
	}

}
</pre>
<p>ここまででArduinoについて分からない場合は田所先生の講義資料を見て勉強してみてください。<br />
本当に素晴らしい講義資料ばかりを公開していただいています。感謝！</p>
<h6>yoppa.org 「第９回：openFrameworksとArduinoを連携する」<br />
<a href="http://yoppa.org/ma2_11/3383.html">http://yoppa.org/ma2_11/3383.html</a></h6>
<h2>3.ofxSimpleGuiTooを追加</h2>
<p>開発を進めるにあたり、アドオンのofxSimpleGuiTooを使います。<br />
とても便利で、作品を作るにあたり開発効率が良くなるので早期に取り入れます。<br />
先程取得したセンサー値も、簡単に視覚化することが出来ます。<br />
筆者はmemoさんのバージョンを使っています。</p>
<h6>ofxSimpleGuiToo<br />
<a href="https://github.com/memo/ofxSimpleGuiToo">https://github.com/memo/ofxSimpleGuiToo</a></h6>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		ofxSimpleGuiToo gui;
		void setupGUI();

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">

void OA001::setup(){

	// ofxSimpleGuiTooをセットアップ
	setupGUI();

}

//--------------------------------------------------------------
void OA001::setupGUI() {

	gui.setup();
    
	// GUIの見た目を設定
	gui.config-&gt;textColor = 0xFFFFFF;
	gui.config-&gt;buttonHeight = 20;
	gui.config-&gt;sliderHeight = 10;
	gui.config-&gt;titleHeight = 20;
	gui.config-&gt;fullActiveColor = 0x00aec3;
    
	// Arduinoのセンサー値
	gui.addTitle(&quot;ARDUINO&quot;);
	gui.addSlider(&quot;ardValue&quot;, ardValue, 0.0f, 700.0f);
	gui.addSlider(&quot;distance&quot;, distance, 0.0f, 700.0f);
	
	// 前回終了した時の設定で起動
	gui.loadFromXML();
    
	// 最初から表示させる
	gui.show();
    
}

//--------------------------------------------------------------
void OA001::draw(){

	// ofxSimpleGuiTooを描画
	gui.draw();

}

</pre>
<h2>4.3D空間にofCircleを描く</h2>
<p>まずは縦横100個のofCircleを画面のセンターに表示させます。<br />
ofRotateYとかに適当に値を入れると、3D空間にofCircleが描かれているのが確認出来ます。</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();
		void draw();

	private :
		float rotateX;
		float rotateY;
		float rotateZ;

		int count;
		float margin;
		float radius;
		int color;
		int alpha;

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::setup(){

	// 画面全体の回転値を初期化
	rotateX = 0.0f;
	rotateY = 0.0f;
	rotateZ = 0.0f;

	// ofCircleで使用する値を初期化
	count = 100;
	margin = 25.0f;
	radius = 5.0f;
	color = 255;
	alpha = 255;

}

//--------------------------------------------------------------
void OA001::draw(){

	// この段階のMatrixを保持
	ofPushMatrix();

	// 画面センターに移動
	ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
	// 画面全体の回転値を摘要
	ofRotateX(rotateX);
	ofRotateY(rotateY);
	ofRotateZ(rotateZ);
    
	// ofCircleの描画
	int totalLen = (count-1)*margin;
	ofTranslate(totalLen/-2, totalLen/-2);
	ofSetColor(color, color, color, alpha);
	for(int i=0; i&lt;count; i++){
		for(int j=0; j&lt;count; j++){
			ofCircle(i*margin, j*margin, 0, radius);
		}
	}
    
	// 保持したMatrixに戻す
	ofPopMatrix();

}
</pre>
<h2>5.PerlinNoiseでゆらゆら</h2>
<p>ofNoiseはパターン化されたランダム値を取得する事が出来ます。<br />
ofNoiseからPerlinNoiseを取得するために以下記事を参考にしました。</p>
<h6>Noise &#8211; 【oF】openFrameworks<br />
<a href="https://sites.google.com/site/ofauckland/examples/noise">https://sites.google.com/site/ofauckland/examples/noise</a></h6>
<p>このPerlinNoiseで得たグレースケール値を、ofCircleの奥行きに摘要していきます。<br />
先程と同じ箇所を改変します。</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::draw(){

	// この段階のMatrixを保持
	ofPushMatrix();
    
	// 画面センターに移動
	ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
	// 画面全体の回転値を摘要
	ofRotateX(rotateX);
	ofRotateY(rotateY);
	ofRotateZ(rotateZ);
    
	// ofCircleの描画
	int totalLen = (count-1)*margin;
	ofTranslate(totalLen/-2, totalLen/-2);
	ofSetColor(color.r*255, color.g*255, color.b*255, alpha);
	for(int i=0; i&lt;count; i++){
		for(int j=0; j&lt;count; j++){
			float a = i * xForce;
			float b = j * yForce;
			float c = ofGetFrameNum() / 60.0;
			float noise = ofNoise(a,b,c) * zForce;
			ofCircle(i*margin, j*margin, noise, radius);
		}
	}
    
	// 保持したMatrixに戻す
	ofPopMatrix();

}
</pre>
<p>ここまでの様子は以下の様になります。<br />
ofxSimpleGuiTooで各パラメーターを操作して遊んでみます。<br />
画面収録している分フレームレートが落ちていますが、実際は60fpsで動きます。</p>
<p><iframe src="http://player.vimeo.com/video/61872549" width="720" height="405" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/61872549">openFrameworks x Arduino #001( Process )</a> from <a href="http://vimeo.com/user16458913">Takepepe</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2>6.音楽を再生</h2>
<p>ofSoundPlayerで音楽を取り込みます。<br />
openFrameworksで音楽をコントロールする、恐らく最も簡単な方法です。<br />
ofSoundPlayerのボリュームをPerlinNoiseに係数として与えることで、<br />
音に併せて揺らめきが変わることが確認出来ます。</p>
<h3>OA001.h</h3>
<pre class="brush: cpp; title: ; notranslate">
class OA001 : public ofBaseApp{

	public:
		void setup();

	private :
		ofSoundPlayer mySound;
		float speed;
		float volume;
		void setupMusic();

}
</pre>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::setupMusic() {
    
	// 音楽を読み込んでループ再生
	mySound.loadSound(&quot;sound.wav&quot;);
	mySound.setLoop(true);
	mySound.play();
    
}

//--------------------------------------------------------------
void OA001::update(){

	// ofSoundPlayerを設定
	mySound.setSpeed(speed);
	mySound.setVolume(volume);

}

</pre>
<h2>7.Arduinoの値を摘要</h2>
<p>冒頭で取得したArduinoの値をofMapで音楽の音量・スピードに摘要すれば完成です！</p>
<h3>OA001.cpp</h3>
<pre class="brush: cpp; title: ; notranslate">
void OA001::update(){
    
	// Arduinoのセンサー値をofSoundPlayerの設定値に変換
	speed = ofMap(distance, 150, 600, 0.1f, 3.0f);
	volume = ofMap(distance, 150, 600, 0.1f, 1.0f);
    
	// ofSoundPlayerを設定
	mySound.setSpeed(speed);
	mySound.setVolume(volume);
    

}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://takepepe.com/openframeworks-x-arduino-001-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: takepepe.com @ 2026-04-08 22:43:19 by W3 Total Cache -->