Arduino say 'hello' to flash.

Part 1.


Why Flash? (and not processing)

A Short intro into why we are using Flash

Setting up your Arduino (Yes. Again.)

Did it work?
Ask me. To Help.

Some Code Snippets to get you thinking. Edited from bjoern

Using a Mouse to control an LED.


    var ledState:int = Arduino.HIGH;
    
    function onMouseClickEvent(event:MouseEvent):void {
	if(ledState==Arduino.HIGH) {
		ledState=Arduino.LOW;
	}else {
		ledState=Arduino.HIGH;
	}
	arduino.writeDigitalPin(ledPin, ledState);
}




stage.addEventListener(MouseEvent.CLICK, onMouseClickEvent);  

Doing that Apple Flashign LED PWN thing.

var ledPin:Number = 9;
var arduino:Arduino = new Arduino("127.0.0.1", 5113);
var pwmValue:Number=0;
var rising:Boolean=true;
var step:Number=10;


// we first ask for the Firmware version, when it comes back, we know it's safe to initialize
arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onArduinoStartup);
arduino.requestFirmwareVersion();

// Arduino initialization
function onArduinoStartup(e:ArduinoEvent):void {
	arduino.setPinMode(ledPin, Arduino.PWM);
	addEventListener(Event.ENTER_FRAME, onEnterFrame);
	trace("Arduino initialized.");
}

function onEnterFrame(event:Event):void {
	if(rising) {
		pwmValue+=step;
		if(pwmValue>=254) {
			rising=false;
			pwmValue=254;
		} 
	} else {
		pwmValue-=step;
		if(pwmValue<=0) {
			rising=true;
			pwmValue=0;
		}
	}
	// show current pwmValue value in window
	label.text=pwmValue.toString();
	// write pwm value to Arduino
	arduino.writeAnalogPin(ledPin,pwmValue);
	
} 

Swith Input


import net.eriksjodin.arduino.Arduino;
import net.eriksjodin.arduino.events.ArduinoEvent;

var inputPin:Number = 2;
var arduino:Arduino = new Arduino("127.0.0.1", 5331);

// we first ask for the Firmware version, when it comes back, we know it's safe to initialize
arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onArduinoStartup);
arduino.requestFirmwareVersion();

// Arduino initialization
function onArduinoStartup(e:ArduinoEvent):void {
	arduino.setPinMode(inputPin, Arduino.INPUT);
	arduino.enableDigitalPinReporting();
	trace("Arduino initialized.");
}

function onReceiveDigitalData(e:ArduinoEvent):void {
	//trace(e);
	if(e.pin==inputPin) {
		if (e.value==Arduino.HIGH) {
			[NameOfMovieClip]visible=false;
		} else {
			[NameOfMovieClip].visible=true;
		}

	}
}

//PLACE A MOVIECLIP ON THE SCREEN
[NameOfMovieClip].visible=false;
arduino.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData);

Example of Haptics


// vibra_control_01
// turn vibration motor connected to pin2 on Arduino on when mouse is pressed,
// off when mouse is released
// bjoern@stanford.edu 2/12/08, REVISED 12/15/08

import net.eriksjodin.arduino.Arduino;
import net.eriksjodin.arduino.events.ArduinoEvent;
import flash.events.MouseEvent;

var pin:Number = 2;
var arduino:Arduino = new Arduino("127.0.0.1", 11100);

// we first ask for the Firmware version, when it comes back, we know it's safe to initialize
arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onArduinoStartup);
arduino.requestFirmwareVersion();

// Arduino initialization
function onArduinoStartup(e:ArduinoEvent):void {
	arduino.setPinMode(pin, Arduino.OUTPUT);
	arduino.writeDigitalPin(pin, Arduino.LOW);
	trace("Arduino initialized.");
}


function onMouseClickEvent(event:MouseEvent):void {

	if(event.buttonDown) {
		arduino.writeDigitalPin(pin, Arduino.HIGH);
	}else {
		arduino.writeDigitalPin(pin, Arduino.LOW);
	}
}


stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClickEvent);  
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseClickEvent);  

Next stop.. The Alcohol Sensor.