This is a demo/workshop to get your data from your Arduino board to your Flash application using a Processing sketch as the middleware. Since AS3 doesn’t directly interact with your computer’s local hardware including the serial port, a middleware, or a reflective broadcaster is needed to pick up the data from the serial port and broadcast it else where that higher level runtime like Flash can access. In most cases, especially with Flash, that else where is a socket server, and we are going to create this server with Processing. There are other applications out there that helps you do the same thing too.
This demo consist of three parts, however, once the data gets through the serial port from Arduino board to your laptop, it is pretty much up for grab by any applications, OF, MAXMSP, C#, Objective C, Python, Zinc … etc, that has the ability to access local serial port. Data will be sent from your Arduino character by character in ASCII code. On the computer end, it will see a data stream coming, ASCII by ASCII, however, it won’t be able to tell where is the beginning or the end of a particular set of data if we don’t define one for it.
Therefore, we need to set up an end ASCII code for the data set we are sending, so our Processing sketch knows that in between every two end ASCII codes is a complete data set. We will take advantage of a build-in command in Arduino to save some works for us. When uses the command “server.println(data)” in Arduino, it will add two extra ASCII codes, 10 and 13, at the end of your data. These two extra ASCII codes stand for “Newline” (10) and “Pointer returns to the header”(13), which is a perfect identifier for Processing to determine the end of a data set. In between every 1013 there is the completely data set.
Fun starts here:
Hook up your Arduino board to your computer with a USB cable, install USB driver that come with the software, and program your Arduino for serial communication as usual, see picture below. For the purpose of this demo, we are not setting up any sensor, you should already have something set up on your Arduino board that you desperately want to get it to your computer. Just read the pin on your Arduino board, digital or Analogue, and Serial.println the data, and you are good to go.
This is what’s in the Arduino program:
You can also print them in many different format, but no matter what format you are sending, it will be ASCII coded. Which means it will chop up your data, and send character by character in its ASCII code form. Upload you program and open the serial monitor, you should see random number flowing through the 9600 bps channel. After you done, completely exit the Arduino software, and open up Processing. It is crucial that you exit Arduino software completely because it will cause a port conflict with Processing if you don’t.
This is what’s in the Processing sketch:
If you decide to use Processing as your visual solution, you don’t have to implement the server part of this sketch. The data will be read one character at a time in its ASCII code form, so we need to concatenate them back to what it was. Because we set up NEWLINE (ASCII code: 10) as the end byte, we concatenate whatever that is in between the two end byte, with the right bps on both ends, we should be getting a complete data set from the serial port. At the every end of the sketch, we write the complete data to the XMLsocket server where Flash will pick them up.
This is what’s in the Flash document.as:
Make sure you plug in your Arduino before running Processing, and run you Flash application the last, it has to be done in this order.
Multiple input values:
If you have more than just one data that you want to send at any given time, ex. you have five buttons on your Arduino board and you want to send all of them to your laptop. You have to set up an standard divider so when Processing or Flash read the data set, they can split the set into individual ones. To make this happen, we will use “Serial.print(data)” to send each individual value and a divider in between and we use “Serial.println(data)” to send the last value. The difference between “Serial.print(data)” and “Serial.println(data)” is that the “Serial.print(data)” doesn’t send out the extra two ASCII codes. The Arduino code should look similar to this: (I use “,” as my divider)
Serial.print(value1);
Serial.print(“,”);
Serial.print(value2);
Serial.print(“,”);
Serial.print(value3);
Serial.print(“,”);
Serial.print(value4);
Serial.print(“,”);
Serial.println(value5);
And Processing receives it, the string will look like this:
value1,value2,value3,value4,value5
just split them using split(data, divider you defined); and you get yourself a array with all five data.