Clickteam Fusion 2.5 can really make some interesting projects aimed at video game creation (in one article, we looked at how to make a wireless RIOTOUS controller). In this project, we will see how to use the Serial Object in Clickteam Fusion 2.5 and use Clickteam Fusion 2.5 to communicate with serial devices such as the Arduino!
The Serial Object
Despite being a software package aimed at game creation, Clickteam Fusion 2.5 has communication capabilities including TCP (MooSock) and the serial port (Serial Object). Serial ports provide a simple yet effective communication method for transferring data between a host PC and an external device such as a microcontroller. However, serial ports have mostly been removed from motherboards and replaced by the now common USB port (Universal Serial Port). While USB supports high speeds and addressing techniques, it is also very complex and only a few hobbyists dare to use USB in projects. One of the many hurdles in implementing USB is the need for drivers, which can be tricky on Windows-based machines. However, some very clever people created bridge ICs, which essentially convert one bus protocol to another. One commonly used bridge is a serial-to-USB convert, which allows serial devices to communicate with a PC via the USB port.
In Windows, when a serial to USB bridge is connected to a USB port it recognizes it as a Virtual Serial Port. You can find out what the serial port name is by going to Device Manager > Ports.
If you cannot identify which of the PORT devices is the Arduino, you can disconnect the device, make a mental note of the remaining ports, and then reconnect the device. A new port should appear, and this is the Ardunio's serial port. In my case, the port is COM7 (USB-SERIAL CH340).
Now that we know the name of our serial port, it’s time to make the fusion application. Start by loading Clickteam Fusion 2.5 and creating a new empty project. Load the first frame and then add the Serial Object. If you cannot find the Serial Object, you need to install it into your extensions folder. To do this, first download the serial object by clicking here, then drag the contents of the zip file into the Clickteam Fusion 2.5 installation folder. Now that we have a serial object in our frame, we need to go into the event editor and start by creating the new condition “Start of Frame” then add the event “Open Serial Port”.
The first parameter that this event requires is the port that is to be opened — in our case this would be 7 (you don’t need to add COM, just the COM number). The second parameter is the baud rate, and for our project we will use a baud rate of 9600. The Serial Object does not allow for other features such as parity and stop bits, but this should not be a problem, as the defaults seem to be 8 bits of data, one stop bit, and no parity bits. While this works for our simple project, more advanced projects may want to be able to dynamically connect to serial devices. The best way to do this is to use a counter and sequentially connect to each port number. The next event would be to check if the COM Port is open, and if it is, send a simple message, such as “What’s Your ID?” The device that we want to connect to would then be programmed to respond with a unique ID when it receives such a message. Our MMF2.5 program would detect this response, and if the ID matches with what it is expected, then it knows it's connected to the correct device. If not, it can increment the counter and continue to connect to all possible ports until it finds the port its looking for.
With the Arduino connected, we need to send data via the serial port when one of the buttons is pressed. Doing this is easy by using the event “Send Data” upon the desired condition. The two different data packets we send are “LED On Please” for turning on the LED and “LED Off Please” for turning it off.
When the Arduino has completed the command, it will respond with the status of the LED. However, the println() function also appends a newline at the end of the string, so if we try to use that string, we will never get it to work properly. Therefore, we have to sanitize the data and remove the newline characters (0x0D 0x0A). To do this, we first start with the Serial Object condition “Data Is Waiting” (this condition fires whenever data is received over the serial port). When this condition fires, we execute three events: get the number of bytes waiting in the buffer and store that number, store the received string, and sanitize the string. The first two are easy to do, but sanitizing data requires a bit of calculation. First, we use Mid$ (mid string), which can take snippets out of a string. The string source is set to the received string over the serial port, the starting character is set to 0 (since we want the start of the message), but the number of characters is set to the number of received bytes minus 2. That way, the newline characters are ignored!
The last step in our program is to compare this sanitized string to one of two messages, and as a result, make the LED icon either appear or disappear.
The Arduino code is very simple and written using the free IDE provided by Arduino (written in C++). When our Arduino starts up, it runs the code found in void setup(), and this is where we configure the LED port to be an output, set up the serial port to use a baud rate of 9600, and then turn off the LED. When the main loop starts, the Arduino constantly checks the serial buffer to see if there is any data available. Once data is available, it transfers this data into a string called “message,” which we use for comparisons. The first comparison checks to see if the LED should be turned on, the second comparison checks to see if the LED should be turned off, and both comparisons send information about the LED status accordingly.
This tutorial demonstrates that most hardware projects that contain a microcontroller can easily be connected to Clickteam Fusion 2.5. This can be advantageous in situations where you want a stylish GUI with custom interfaces as opposed to a Windows Form Application. Custom hardware could also be used with games, including augmented reality and virtual reality. Overall, getting a serial device to talk to Clickteam Fusion 2.5 is very easy (easier than VB.net!), and devices like the Arduino will get you serial programming in no time!