Pages: [1]   Go Down
Author Topic: Multiplexers in ActionScript 3 (using AS3Glue)  (Read 668 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I've set up 2 multiplexers on my Arduino, which I can read via an array, using this script:
Code:
//Give convenient names to the control pins
#define CONTROL0 6   
#define CONTROL1 5
#define CONTROL2 4
#define CONTROL3 3
#define CONTROL4 11   
#define CONTROL5 10
#define CONTROL6 9
#define CONTROL7 8

//Create arrays for data from the the MUXs
int chip1[16];
int chip2[16];

void setup()
{
//Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  pinMode(CONTROL4, OUTPUT);
  pinMode(CONTROL5, OUTPUT);
  pinMode(CONTROL6, OUTPUT);
  pinMode(CONTROL7, OUTPUT);
//Open the serial port at ? bps
  Serial.begin(19200);
}

void loop()
{
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
 for (int i=0; i<16; i++)
 {
//The following 4 commands set the correct logic for the control pins to select the desired input
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2); 
    digitalWrite(CONTROL2, (i&3)>>1); 
    digitalWrite(CONTROL3, (i&1));     
   
//Read and store the input value at a location in the array
    chip1[i] = analogRead(1);
 }
 
//This for loop is used to scroll through the SECOND multiplexer
 for (int i=0; i<16; i++)
 {
    digitalWrite(CONTROL4, (i&15)>>3);
    digitalWrite(CONTROL5, (i&7)>>2); 
    digitalWrite(CONTROL6, (i&3)>>1); 
    digitalWrite(CONTROL7, (i&1));     
    chip2[i] = analogRead(2);
 }

//The following lines are for printing out results of CHIP1
  Serial.print("Chip 1: ");
  for (int i=0; i<16; i++)
  {
    Serial.print("-");
    Serial.print(chip1[i]);
  }
  Serial.println();
//The following lines are for printing out results of CHIP2
  Serial.print("Chip 2: ");
  for (int i=0; i<16; i++)
  {
    Serial.print("-");
    Serial.print(chip2[i]);
  }
  Serial.println();
}

Now, I'd like to convert that code to run exactly the same in ActionScript 3.

I did some looking up, and made this:
Code:
//Give convenient names to the control pins
var CONTROL0:Number=6   
var CONTROL1:Number=5
var CONTROL2:Number=4
var CONTROL3:Number=3
var CONTROL4:Number=11   
var CONTROL5:Number=10
var CONTROL6:Number=9
var CONTROL7:Number=8

//Create arrays for data from the the MUXs
var chip1:Array=[];
var chip2:Array=[];

void setup() //this isnt proper As3 code, as I'm not sure what to put here
{
//Set MUX control pins to output
  arduino.setPinMode(CONTROL0, OUTPUT);
  arduino.setPinMode(CONTROL1, OUTPUT);
  arduino.setPinMode(CONTROL2, OUTPUT);
  arduino.setPinMode(CONTROL3, OUTPUT);
  arduino.setPinMode(CONTROL4, OUTPUT);
  arduino.setPinMode(CONTROL5, OUTPUT);
  arduino.setPinMode(CONTROL6, OUTPUT);
  arduino.setPinMode(CONTROL7, OUTPUT);
//Open the serial port at ? bps
  Serial.begin(19200);
}

void loop() //this isnt proper As3 code, as I'm not sure what to put here
{
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
 for (int i=0; i<16; i++)
 {
//The following 4 commands set the correct logic for the control pins to select the desired input
    if (i=>15) { arduino.writeDigitalPin(CONTROL0, 3); }
if (i=>7)  { arduino.writeDigitalPin(CONTROL1, 2); }
if (i=>3)  { arduino.writeDigitalPin(CONTROL2, 1); }
else { arduino.writeDigitalPin(CONTROL3, 0); }
//Read and store the input value at a location in the array
    chip1[i] = arduino.getAnalogData(1);
 }
 
//This for loop is used to scroll through the SECOND multiplexer
 for (int i=0; i<16; i++)
 {
    if (i=>15) { arduino.writeDigitalPin(CONTROL4, 3); }
if (i=>7)  { arduino.writeDigitalPin(CONTROL5, 2); }
if (i=>3)  { arduino.writeDigitalPin(CONTROL6, 1); }
else { arduino.writeDigitalPin(CONTROL7, 0); }
//Read and store the input value at a location in the array
    chip2[i] = arduino.getAnalogData(2);
 }

As you can see, the code above won't run as is, in AS3.
Now, can you help me to make it work as it should? All help is welcome!

But how can I get this to run in ActionScript nonstop? (I know, by adding an Enter_Frame function, but to what? the stage?)
Using onReceiveAnalogData is too CPU intensive to run all the time.


The method I used to get Arduino data to Flash, is AS3Glue: http://code.google.com/p/as3glue/

Please help me out AS SOON AS POSSIBLE!
Thanks in advance
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I worked a bit more on my AS3 code, which almost works now.

The problem is, I don't get any data from the following line:
arduino.getAnalogData(1); [or arduino.getAnalogData(2);, which is basically the same]
That is weird, because the Arduino does send data to those lines...

What could I be doing wrong?

Note: communicating with the arduino works fine, as arduino.writeDigitalPin(13, 1); lights up the LED.

Code:
import net.eriksjodin.arduino.*;
import net.eriksjodin.arduino.events.ArduinoEvent;
import net.eriksjodin.arduino.events.ArduinoSysExEvent;
import Multiplexer2;

var arduino = new Arduino("127.0.0.1", 5331);

//Give convenient names to the control pins
var CONTROL0:Number=6    
var CONTROL1:Number=5
var CONTROL2:Number=4
var CONTROL3:Number=3
var CONTROL4:Number=11    
var CONTROL5:Number=10
var CONTROL6:Number=9
var CONTROL7:Number=8

//Create arrays for data from the the MUXs
var chip1:Array=[];
var chip2:Array=[];

stage.addEventListener (Event.ENTER_FRAME, multiplexer1);
stage.addEventListener (Event.ENTER_FRAME, multiplexer2);

function multiplexer1(e:Event):void {
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
for (var teller1:int=0; teller1<16; teller1++) {
//The following 4 commands set the correct logic for the control pins to select the desired input
   if (teller1>=15) { arduino.writeDigitalPin(CONTROL0, 3);}
if (teller1>=7)  { arduino.writeDigitalPin(CONTROL1, 2);}
if (teller1>=3)  { arduino.writeDigitalPin(CONTROL2, 1);}
else { arduino.writeDigitalPin(CONTROL3, 0);}
//Read and store the input value at a location in the array
   chip1[teller1] = arduino.getAnalogData(1);
trace (arduino.getAnalogData(1));
}
}
function multiplexer2(e:Event):void {
//This for loop is used to scroll through the SECOND multiplexer
for (var teller2:int=0; teller2<16; teller2++) {
     if (teller2>=15) { arduino.writeDigitalPin(CONTROL4, 3); }
if (teller2>=7)  { arduino.writeDigitalPin(CONTROL5, 2); }
if (teller2>=3)  { arduino.writeDigitalPin(CONTROL6, 1); }
else { arduino.writeDigitalPin(CONTROL7, 0); }
//Read and store the input value at a location in the array
   chip2[teller2] = arduino.getAnalogData(2);
}
}
« Last Edit: June 07, 2011, 04:10:26 pm by Swoosh88 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As you can see, the code above won't run as is, in AS3.
Nope. Can't see that.

Do you often ask Arduino-related questions in the AS3 or Flash forums?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As you can see, the code above won't run as is, in AS3.
Nope. Can't see that.

Do you often ask Arduino-related questions in the AS3 or Flash forums?
What do you mean with that last sentence?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What do you mean with that last sentence?
Isn't it obvious. The web site here is www.arduino.cc, not www.as3.something or www.flash.help. This is a forum for the Arduino. You are needing help with Flash/AS3/whatever, not Arduino.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I know, but the people over at the Flash forums don't know about the Arduino language...

And I was posting here, because this is the Interfacing etc. forum area, which seems suited for this...

Ontopic:
Everything is stable now, and running, but not working properly:
For some reason I can't control my Multiplexer from Actionscript anymore.
I am able to write digital data to pin 13 [easy to test], but when writing to my multiplexer chip, it seems like it is not responding or something...
It does work with the Arduino code...
Logged

Pages: [1]   Go Up
Jump to: