| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

As3glueReference

Page history last edited by Bjoern Hartmann 11 years, 11 months ago

Creation

Create an arduino object with the following line:

arduino = new Arduino("127.0.0.1", 11100);

"127.0.0.1" is the IP address where serialproxy is running - in this case, localhost. 11100 is the port number that the serialproxy TCP socket server uses as defined in serproxy.cfg (if using serproxy) or in preferences.txt (if using Arduino IDE with built-in Serial Proxy).

 

Initialization

When your Flash application first starts up, you usually want to configure which pins on Arduino should be input, which output, which analog channels should report, etc. However, you don't know what state the Arduino hardware is in at startup - it may still have some old configuration from a previous run, or it may be in the middle of booting up. How do you know when it is safe to sent initialization messages to Arduino? We'll use the fact that the Firmata firmware running on Arduino sends its version in a special message to us whenever Arduino reboots. The pattern looks like this: we register a listener for ArduinoEvent.FIRMWARE_VERISON, and in the handler for that event, perform all initialization. When launching our application, we first start the Flash application, and then manually reset the Arduino hardware. 2 or 3 seconds later, Arduino will send it's version number, the callback function is called, and we're in business. In ActionScript:

// when Arduino reboots, Firmata firmware sends its version to indicate startup
arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onArduinoStartup);

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

Digital I/O

  • To set a pin to input or output, use
    arduino.setPinMode(pin, Arduino.INPUT);
    arduino.setPinMode(pin, Arduino.OUTPUT);
    
    where pin is a Number variable with value between 2 and 13.
  • To set an output pin high or low, use
    arduino.writeDigitalPin(pin, Arduino.HIGH);
    arduino.writeDigitalPin(pin, Arduino.LOW);
    
  • To enable or disable pin reporting for all digital input pins(you'll have to enable if you have input pins and want to read inputs), use
    arduino.enableDigitalPinReporting();
    arduino.disableDigitalPinReporting();
    
  • To read a value from a digital input pin use
    arduino.getDigitalData(pin);
    

 

Analog I/O

  • To set a pin between 9 and 11 to PWM output, use
    arduino.setPinMode(pin, Arduino.PWM);
    
  • To write a PWM value to a pin previously initialized for PWM output, use
    arduino.writeAnalogPin(pin, value);
    
    where value is a Number variable between 0 and 255.
  • To enable or disable pin reporting for individual analog input pins, use:
    arduino.setAnalogPinReporting(aPin, Arduino.ON);
    arduino.setAnalogPinReporting(aPin, Arduino.OFF);
    
    where aPin is a Number variable with value between 0 and 6.
  • To read an analog value from an analog input pin use
    arduino.getAnalogData(aPin);
    

 

Servos (uses class ArduinoWithSerial.as)

  • arduino.setupServo(pin,angle): Initialize servo timer routines on a pin. Pin must be 9 or 10 in the current implementation. Angle is the initial angle of the servo between 0 and 180.
  • arduino.writeAnalogPin(pin, angle): Write a servo angle (in degrees) to an output pin. value has to be a number between 0 and 180. Pin must have been initialized with setupServo() before.

 

Event listener functions

Instead of reading values through getAnalogData() and getDigitalData(), as3glue can also use event listener functions you supply whenever a digital or analog pin changes. You have to do two things: write a callback function and register that function with the object. Here are examples:

  • To add an event listener that is called whenever a digital input changes, fist declare the callback function:
    public function onReceiveDigitalData(e:ArduinoEvent):void {
    trace("Digital pin " + e.pin + " on port: " + e.port +" = " + e.value);
    }
    
    Then register it with the arduino object:
    arduino.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData);
    
  • To add an event listener that is called whenever an analog input changes, fist declare the callback function:
    public function onReceiveAnalogData(e:ArduinoEvent):void {
    trace("Analog pin " + e.pin + " on port: " + e.port +" = " + e.value);
    }
    
    Then register it with the arduino object:
    arduino.addEventListener(ArduinoEvent.ANALOG_DATA, onReceiveAnalogData);
    

 

Examples

 

 

 

Comments (0)

You don't have permission to comment on this page.