
RFID to Flash AS3.0
To sell the concept for the radar and detection room we needed a working prototype. I managed to get a hold of a Parallax RFID reader which is compatible with Arduino. On the Arduino website you will find a sketch for the Parillax RFID reader. Because I had only one RFID reader I added a button and modified the sketch so you can cycle through modes. This is to emulate the game/login part and the node part (scan if you find a object) of the prototype.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// RFID reader for Arduino // Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> // Modified for Arudino by djmatic // Added button for different modes and extended delay time by Barry <http://www.barryvandam.com/blog> int val = 0; char code[10]; int bytesread = 0; int buttonPin = 11; int voltPin = 8; int buttonState = 0; void setup() { Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin digitalWrite(2, LOW); // Activate the RFID reader pinMode(buttonPin, INPUT); pinMode(voltPin, OUTPUT); digitalWrite(voltPin, HIGH); } void loop() { buttonState = digitalRead(buttonPin); if(Serial.available() > 0) { // if data available from reader if((val = Serial.read()) == 10) { // check for header bytesread = 0; while(bytesread if( Serial.available() > 0) { val = Serial.read(); if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading break; // stop reading } code[bytesread] = val; // add the digit bytesread++; // ready to read next digit } } if(bytesread == 10) { // if 10 digit read is complete if (buttonState == HIGH) { Serial.print("readmode,"); } else { Serial.print("sendmode,"); } Serial.println(code); // print the TAG code } bytesread = 0; digitalWrite(2, HIGH); // deactivate the RFID reader for a moment so it will not flood delay(3000); // wait for a bit digitalWrite(2, LOW); // Activate the RFID reader } } } |
The next step was to set up a way to communicate between Arduino and Flash (AS3). The Firmata/AS3GLUE method won’t work with this setup because the RFID reader uses digital pin 0 (RX) which is used for serial communication. However Flash can receive data from Arduino through serialproxy and a AS3 class that listens to incoming serial data. Flash cannot sent data back to the Arduino (another reason why I use a button) and acts as the serial monitor so it receives the Serial.print() and Serial.println() form the Arduino sketch.
Download the Arduino Flash communication package (from kasperkamperman.com), extract it and put SerialPort.as in the same map as your Flash file (or link it). Include the following code in your AS3 script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var modeStr:String = new String; var idStr:String = new String; // Arduino init and event var arduino:SerialPort = new SerialPort("127.0.0.1", 5331); arduino.addEventListener(DataEvent.DATA, onArduinoData ); arduino.connect(); // Called when data is received from the Arduino function onArduinoData( event:DataEvent ):void { var s:String = event.data; // send Arduino data to string var dataArray:Array = s.split(","); // Split string at , and save to dataArray modeStr = dataArray[0]; idStr = dataArray[1]; trace("mode", modeStr ); trace("RFID", idString ); } |
Open serialproxy.cfg with a code editor and change the baudrate to 2400. Don’t forget to set the COM port to the one Arduino uses. Connect the Arduino to your PC, start serialproxy and run the Flash file. Flash can now receive serial data. On a sidenote, if you want to send longer strings of data it’s probably better to use the Messenger/CmdMessenger Library.
No comments