Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
//import the Serial library so can read from arudino input via serial communication
import processing.serial.*;
import spacebrew.*;
int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages
String serial; // declare a new string called 'serial'
Serial port; // The serial port, this is a new instance of the Serial class (an Object)
String server="";
String name="highlands";
String description ="Client that sends and receives range messages. Range values go from 0 to 1023.";
Spacebrew sb;
int amebia_1_val = 0;
int amebia_2_val = 0;
int moonscape_val = 0;
void setup() {
//serial reading code
port = new Serial(this, Serial.list()[10], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino)
port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial')
serial = null; // initially, the string will be null (empty)
// instantiate the spacebrewConnection variable
sb = new Spacebrew( this );
// declare your publishers
sb.addPublish( "amebia_1", "range", amebia_1_val );
sb.addPublish( "amebia_2", "range", amebia_2_val );
sb.addPublish( "moonscape", "range", moonscape_val );
// connect!
sb.connect(server, name, description );
void draw() {
//if there is data coming from the serial port read it/ store it
while (port.available() > 0) {
serial = port.readStringUntil(end);
//if the string is not empty, do this
if (serial != null) {
//capsense Input form Arduino, each value is seperated and split depending on the ','
//and then saved in seperate cells of the array so we can access each
String[] SensorInput = split(serial, ',');
//can help to print these to console at this point to check it's working
// //convert the string inputs that are stored in the Input array to ints so we can use them numerically
// int PRInt [];//Array that we will store the the Input from Arduino after we have converted it to int
// PRInt = int(SensorInput);
amebia_1_val = int(SensorInput[0]);
sb.send("amebia_1", amebia_1_val);
amebia_2_val = int(SensorInput[1]);
sb.send("amebia_2", amebia_2_val);
moonscape_val = int(SensorInput[2]);
sb.send("moonscape", moonscape_val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment