itsukichang

フロントエンドが得意なエンジニア.ダーツと旅行とギターが好き

Daily Arduino×Flash #3

今回は加速度センサで動くものを.

#3 加速度センサとバブル

加速度センサから読み取った値の変化量,一定時間内にどれだけ激しい動きをしたかを取得,解析し,
その激しさによって,Flashに表示されるアニメーションが変化するというもの.

画面下部から,あわあわーと泡的ななにか(ただの丸)がでます.

回路図

準備中なう.
(準備とか言ってるけど,ただ加速度センサを5VとGNDとanalogInputに刺すだけry)

プログラム
  • Main.as
package {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import funnel.*;
	import itsukichang.*;
	
	public class Main extends Sprite {
		
		private var _xPin:Pin;
		private var _yPin:Pin;
		private var _zPin:Pin;
		
		private var _arduino:Arduino;
		private var _p:Particle;
		
		private var _timer:Timer;
		
		private var _oldX:Number = 0;
		private var _nowX:Number = 0;
		public function Main() {
			init();
			
		}
		
		private function init():void {
			_arduino = new Arduino(Arduino.FIRMATA);
			_xPin = _arduino.analogPin(0);
			_yPin = _arduino.analogPin(1);
			_zPin = _arduino.analogPin(2);
			_timer = new Timer(500);
			_timer.addEventListener(TimerEvent.TIMER, onLoop);
			_timer.start();
		}
		
		
		private function onLoop(e:TimerEvent):void {
			_oldX = _nowX;
			_nowX = _xPin.value * 100;
	
			trace("now = " + _nowX + "old = " + _oldX);
			
			var val = _nowX - _oldX; //0.5秒前との差
			if (val > 10) { //差が10以上なら
				trace("ok");
				trace("val =", val);
				val *= 2; //差の2倍の数の泡を出す
				Bubble(val);
			}
		}
		
		private function Bubble(n:Number):void {
			
			for (var i:uint = 0; i < n; i++) {
				_p = new Particle(Math.random()*stage.stageWidth, stage.stageHeight + 30,  Math.random() * 2,  Math.random() * 3, Math.random()*0.5);	 // x, y, vx, vy, ay
				addChild(_p);
			}
		}
	}
}
  • Particle.as //パーティクルを生成するクラス
package {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import itsukichang.*;
	
	public class Particle extends Sprite {
		
		private var _b:Ball;
		
		private var _vx:Number;
		private var _vy:Number;
		private var _ay:Number;
		private var _x:Number;
		private var _y:Number;
		
		private var _rad:Number;
		
		public function Particle(x:Number,y:Number,vx:Number,vy:Number,ay:Number) {
			
			_x = x;
			_y = y;
			_vx = vx;
			_vy = vy;
			_ay = ay;
			_rad = -180 + Math.random()*360;
			init();
		}
		
		private function init():void {
			_b = new Ball(Math.random()*30, Math.random()*0xffffff);
			addChild(_b);
			_b.x = _x;
			_b.y = _y;
			_b.alpha = Math.random() * 0.8;
			_b.addEventListener(Event.ENTER_FRAME,onLoop);
			
		}
		
		private function onLoop(e:Event):void {
			_b.x += Math.sin(_rad) * _vx; //左右に振れる動き
			_b.y -= _vy;
			_vy += _ay;
			_rad += 0.1;
			
			if (_b.x > stage.stageWidth || _b.x < 0 || _b.y < 0) { //画面外に出たら解放
				_b.removeEventListener(Event.ENTER_FRAME, onLoop);
				removeChild(_b);
				parent.removeChild(this);
				_b = null;
			}
		}
	}
}
  • Ball.as //例によって丸を作るクラス
package {
	
	import flash.display.Sprite;
	
	public class Ball extends Sprite  {
		
		private var _col:uint;
		private var _r:Number;
		private var _sp:Sprite;
		
		public function Ball(r:Number = 10, col:uint = 0x0) {
			_r = r;
			_col = col;
			init();
		}
		
		private function init():void {
			_sp = new Sprite();
			_sp.graphics.beginFill(_col);
			_sp.graphics.drawCircle(0, 0, _r);
			_sp.graphics.endFill();	
			addChild(_sp);
		}
		
		public function colorTrans(col:uint):void {
			_col = col;	
			reDraw();
		}
		
		private function reDraw():void {
			_sp.graphics.clear();
			_sp.graphics.beginFill(_col);
			_sp.graphics.drawCircle(0, 0, _r);
			_sp.graphics.endFill();	
		}
	}
}
動作してる図(動画とか)



動画準備中なう (果たしてupされるかry)

wonderfl

今回もマウスで操作するPC版も用意して,wonderflに上げてみた.
クリックしたらランダムな数で泡がでるよ.
Arduino版では,泡の数が加速度の変化で決まる.

Bubble - wonderfl build flash online

余談

いいかげん電子部品買って,回路のほうにも力いれたいよー