Creation

Create an arduino object with the following line:

arduino = new Arduino("127.0.0.1", 5331);
"127.0.0.1" is the IP address where serialproxy is running - in this case, localhost. 5331 is the port number that the serialproxy TCP socket server uses as defined in serproxy.cfg.

 

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

  • arduino.servoAttach(pin): Initialize servo timer routines on a pin. Pin must be 9 or 10 in the current implementation.
  • arduino.servoDetach(pin): Stop pulse output to a pin that was previously initialized with servoAttach().
  • arduino.servoSetMaxPulseTime(pin, value): Set the pulse duration (in microseconds) that causes a servo to rotate to its upper limit. This value is usually around 2500 for Futaba servos.
  • ardino.servoSetMinPulseTime(pin, value): Set the pulse duration (in microseconds) that causes a servo to rotate to its lower limit. This value is usually around 500 for Futaba servos.
  • arduino.servoWrite(pin, value):Write a servo angle (in degrees) to an output pin. value has to be a number between 0 and 180.

 

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

  • To see working examples, look inside the cs247_examples folder in as3glue-cs247-01.zip
  • Also take a look at the file as3glue/as3/examples/arduino/SimpleIO.as in the as3glue distribution.


Page Information

  • 6 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts