Basics: Arduino + Spacebrew with Firmata
/Hey there space cadets! Today, I want to talk about using Spacebrew for Processing together with an Arduino. Have you ever wanted to use analog and digital readings from an Arduino in your Processing sketch? This is the place to start.
Step 1: Load Firmata onto your Arduino
To make our lives easier, were going to keep things simple on the Arduino side. Instead of having to re-upload software onto the Arduino each time your project changes, we’re just going to use the Standard Firmata sketch in Arduino. You’ll be able to find it in examples folder under Firmata.
Step 2: Processing Sketch to Read + Send Arduino values to Spacebrew
Now we’re going to write a Processing sketch thats going to forward the information to Spacebrew. First, open up a new sketch and start by adding the Serial, Arduino and Spacebrew libraries. (If you doing have SB yet, head over to the github page at: https://github.com/Spacebrew/spacebrewP5).
import processing.serial.*; import cc.arduino.*; import spacebrew.*;
Now, time to declare our Spacebrew variables:
Spacebrew sb; String server=“ip address here”; // the ip address goes in the quotes. i.e. “255.255.255.0” String name="Arduino Firmata Input"; String description ="This app sends out inputs received from Arduino analog and digital inputs";
The ip address is where you SB server is set up. The name is what it will show up as on the spacebrew admin page. The description is just that, a description of the app that will show up on the admin page.
To finish up declaring our variables, lets create an instance of our Arduino and make a few colors we will use to show whether our digital app is receiving Arduino input:
Arduino arduino; color off = color(4, 79, 111); color on = color(84, 145, 158);
Now onto the setup loop. Lets start by instantiating our spacebrew and Arduino variables.
// instantiate the spacebrew Connection variable sb = new Spacebrew( this ); // Modify this line, by changing the "0" to the index of the serial // port corresponding to your Arduino board (as it appears in the list // printed by the line above). arduino = new Arduino(this, "/dev/tty.usbmodem1411", 57600); // Alternatively, use the name of the serial port corresponding to your // Arduino (in double-quotes), as in the following line. //arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600);
Now that those have been initialized, lets create our space brew publishers that will send out analog and digital pin information from the Aruduino. Remember, the digital pins are going to be booleans since they are going to be either on (1) or off (0). The analog pins, on the other hand will use range spacebrew publishers since they will send out a value between 0 and 127:
// Set the Arduino digital pins as inputs and create Spacebrew Publishers for digital reads for (int i = 0; i <= 13; i++) { arduino.pinMode(i, Arduino.INPUT); sb.addPublish( "button_"+i+"_pressed", "boolean", false ); } // Set the Arduino analog pin Spacebrew Publishers for (int i = 0; i<= 5; i++) { sb.addPublish( "analog_"+i+"_val", "range", arduino.analogRead(i) ); }
Our last step in the setup loop will be to connect to our spacebrew server:
sb.connect(server, name, description );
Now we’re all set up and our variables initialized. It’s time to set up our draw function to send our data as it updates to space brew. To give us some visual feedback and to make sure we are getting the input, we are going to make rectangles that fill when a digital pin has been pressed and circles that change in size based on analog input. This is going to be done within 2 for loops. One for the digital pins, the other for the analog:
void draw() { background(off); stroke(on); // Draw a filled box for each digital pin that's HIGH (5 volts). for (int i = 0; i <= 13; i++) { if (arduino.digitalRead(i) == Arduino.HIGH) { sb.send( "button_"+i+"_pressed", true ); fill(on); } else fill(off); rect(420 - i * 30, 30, 20, 20); } // Draw a circle whose size corresponds to the value of an analog input. noFill(); for (int i = 0; i <= 5; i++) { ellipse(280 + i * 30, 240, arduino.analogRead(i) / 16, arduino.analogRead(i) / 16); if (arduino.analogRead(i) > 0) { sb.send( "analog_"+i+"_val", arduino.analogRead(i)); } }
Now your Arduino is set up to send digital and analog pin information to the spacebrew server of your choice.
Step 3: Make connection through Admin
When you log onto the Spacebrew admin page for your server you should now see all your analog and digital inputs for your Arduino. They are now ready to be hooked up to any Spacebrew subscriber set up on the same server.
There you have it. Get out those Arduinos and start making!