programming help? using as3glue to read a sensor

Hi! :smiley: I manged to get arduino talking to flash! I can make the "Blink" program work though flash. Hurray!

I have a couple of questions. I have the following code which outputs values though the serial port from a sensor when uploaded to arduino using the normal arduino program :

// www.plusea.at
// reads analog input from the five inputs from your arduino board 
// and sends it out via serial



// variables for input pins and
  int analogInput0 = 0;

  
// variable to store the value 
  int value0 = 0; 

  
 
 
 
void setup(){
// declaration of pin modes
  pinMode(analogInput0, INPUT);

  
// pullup resistors
  digitalWrite(14, HIGH); 
  digitalWrite(15, HIGH); 
  digitalWrite(16, HIGH); 
  digitalWrite(17, HIGH);
  
// begin sending over serial port
  Serial.begin(9600);
}// end setup





void loop(){
// read the value on analog input
  value0 = analogRead(analogInput0);


// print out value over the serial port
    Serial.print(1, BYTE); //prefix
    Serial.print(value0);
    Serial.print(10, BYTE); //end signal
      


// wait for a bit to not overload the port
  delay(100);
}// end loop

First question- if i want to interface with flash and use AS3glue, do i need to "translate" this into as3glue format? or just upload it to the arduino using the normal program before closing it and opening flash?

Second question - i want to use the analog input from the sensor to act in a similar way to the mouse click in the following code, so when it goes over a certain threshold it causes something to happen? How on earth can i code this into the flash program? Here is my existing code...

package 
{
      import flash.display.MovieClip;
      import flash.events.MouseEvent;

      
      public class AvoiderGame extends MovieClip 
      {
            public var army:Array;
            public var enemy:Enemy;
            
            
            

            public function AvoiderGame() 
            {
                  army = new Array();
                  var newEnemy = new Enemy( 100, -30 );
                  army.push( newEnemy );
                  addChild( newEnemy );
                  
                  stage.addEventListener(MouseEvent.CLICK, onClick);
                  
            }
            
            public function onClick( event:MouseEvent ):void 
            {
                  
                        var randomX:Number = (Math.random() * 200) + 100;
                        var randomY:Number = (Math.random() * 150) + 50;
                        var newEnemy:Enemy = new Enemy( randomX, randomY);
                        army.push( newEnemy );
                        addChild( newEnemy );

                          
                              trace("click");
                  }

             
                  
                  
      }
}

Any help at all would be greatly appreciated! Even if it's to tell me that it's too complicated?

Huge thanks,

Susan

I've not programmed Flash to interface with the Arduino, but it seems to me that you'd just need to add a event listener that listens to serial events. How? Beats me. You've got the manual.

The manual says this:

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 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);

The problem is, i don't want it to make an object appear on the screen every time the analog input changes, just when it goes over a certain threshold. When the sensor is pressed hard enough.

As may be painfully obvious, i've not programmed with arduino before.

I really don't understand what needs to be "on" the arduino itself, and what needs to be "in" flash. Do i use the default arduino program, or flash? Does it make a difference? will one overwrite the other?

Basically....i am a complete noob, please can someone talk very slowly in small words and help me stretch my brain around this?

Ok, i guess what i'm asking is, do i need to upload code to the arduino using flash, or can I do it using the normal arduino software, before reading the serial input from the arduino using flash?

If someone can tell me this i might have a better chance.

Huge thanks!

do i need to upload code to the arduino using flash

No.

can I do it using the normal arduino software

Yes.

Thank you! :smiley: That really helps. Now i just need to get flash to read the serial output using an event listener