forked from: Prototyping Lab: Introduction 4.10

by kotobuki forked from Prototyping Lab: Introduction 4.10 (diff: 3)
♥0 | Line 20 | Modified 2011-06-28 11:29:12 | MIT License
play

ActionScript3 source code

/**
 * Copyright kotobuki ( http://wonderfl.net/user/kotobuki )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oMra
 */

// forked from kotobuki's Prototyping Lab: Introduction 4.10
package {
  import flash.display.Sprite;
  import funnel.*;

  public class AnalogIOExample extends Sprite {
    private var arduino:Arduino;

    // 回転型ポテンショメータ(Twig - Rotary Angle Sensor)に接続したピン
    private var sensorPin:Pin;

    // LED(Twig - LED)に接続したピン
    private var ledPin:Pin;

    public function AnalogIOExample() {
      // LEDに接続したピン(D9)のモードをPWMにセット
      var config:Configuration = Arduino.FIRMATA;
      config.setDigitalPinMode(9, PWM);
      arduino = new Arduino(config);

      // センサとLEDに接続したピンを表す変数を初期化
      sensorPin = arduino.analogPin(0);
      ledPin = arduino.digitalPin(9);

      // センサの値が変化した際に発生するイベントに対するリスナをセット
      sensorPin.addEventListener(PinEvent.CHANGE, onChange);
    }

    // センサの値が変化したら以下を実行
    private function onChange(e:PinEvent):void {
      // LEDの値をセンサの値にセット
      ledPin.value = sensorPin.value;
    }
  }
}