Using an Arduino with Processing to use Spacebrew
Arduino
Okay for this, I’m going to use Processing with Arduino, so that I can take advantage of the Spacebrew library for JS. If you have done any Arduino, and haven’t touched Processing, you will feel right at home. We are going to need to download a few things:
- Processing IDE ( I used hte 32bit for Windows for the Serial Communication )
- Arduino IDE
- Processing Arduino Library
- Spacebrew Processing Library from Github
Processing and Arduino communicate over the serial port, so the Arduino library simplifies and abstracts a bit of this process. On my Windows machine, I had to download the 32bit version of Processing as it supports the serial communication.
Install the Libraries
Once everything is downloaded, the libraries need to be installed. Libraries are located where your Processing sketches ( documents) are stored – typically in the Documents folder under Processing.
- Start a new sketch and save it.
- Then find that location – and paste the libraries into the libraries folder.
- Restart Processing.
- You should see the libraries installed by going to Sketch>Import Library> and Spacebrew and Arduino should be listed.
Initialize the Arduino
For the Arduino, we just want to upload a standard Firmata sketch onto it. Firmata is a basic protocol that allows microcontrollers and software to communicate.
- To do this open the Arduino IDE – select your board type – in my case I selected Uno.
- Then you need to select which port. This was the first issue I ran into – my serial port option was grayed out. A little research and I discovered that in Windows 8, some of the Arduino’s drivers cannot be installed as you might want to normally, because they are unsigned. Follow these steps to fix that.
- Restart the Arduino IDE if need
- Upload the Firmata sketch by opening a sketch from Examples > Firmata > StandardFirmata. Click the upload button.
Okay, we have everything in place. Now it’s time to build some circuits what will allow us to send messages via a button push, and receive messages which will light up an led. Here is a blog post on how to do this, which I am going to build upon.
Build the Circuit
I did up a basic diagram in the Fritzing application.
For those of you that like a bit of reality rather than a diagram, here is a picture of my setup.
With our circuit all hooked up, we should test to make sure we set it up right just by uploading a sketch that has no dependancies such as the Spacebrew library. If all is well, it will blink our LED when we press the button, and we will at least know that we didn’t blow that sucker out, which happens all the time if you don’t use a resistor to limit the power to an LED.
- From the Arduino IDE, open File > Examples > 01.Basics> Blink
- My led is hooked up to pin 12. You will need to change the pin number in the sketch, or change the input to 13.
- Upload this sketch to the Arduino
- Watch the LED blink
Ok, so at its bare minimum we know that things *should* work. It’s time to hop into Processing – which will look very familiar as the Processing IDE and Arduino IDE are very similiar.
Processing, Arduino and Spacebrew
The good news here is that there is a few good tutorials on using Spacebrew with Processing. We are going to repeat the same steps that we did for the Unity client. We will need to be able to send messages ( via the push of a button ) and receive messages which will blink the LED. We could easily switch out any of these inputs or ouputs – an led strip for example, or in the case of my beloved DDR pad, a pressure sensor. Remember to make sure that your Spacebrew server is running – if you hit the server url and the page doesn’t render, navigate to the spacebrew directory and type:
node node_server_forever.js
into a command window.
- Open Processing and start a new sketch. Save it.
- Copy and paste this code:
- Run the sketch
- Click the button – and see if the LED blinks when you press down. No Love?
- Open the Spacebrew admin panel by droping the index page in the admin subdirectory of your Spacebrew into the browser – and find the client that represents your Arduino/Processing. Remember you need to connect the publishers and subscribers. Connect them between clients as well so that it looks like this:
- Try again
.
Now we should see that LED blink when we click that button from Unity. And as we were already in the admin panel, we hooked up all the clients, so the Arduino and Unity clients should be set up to broadcast and subscribe to each other. I didn’t need to make them all listen to each other – but it made it easier to be able to run them independantly if I needed to test a specific part.
Run the Unity Project – and click that button – does the LED blink? Does clicking the button from the Arduino result in the textfield change its display in the Unity Game?
All in all, it was pretty easy to get up and running to get something like Unity3d to communicate to an Arduino ( and vice versa ). Now I can’t wait to get my DDR mat into play – that will be next!