Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capsense for Processing - Connecting Via Serial
//Simple Soft Circuits/Capsense Connection via Spacebrew toolkit
//by Joselyn McDonald and Nicole Messier
//This is Part B of input - You should have already run the Arduino Serial and CapSense Sketch - Soft_Circuits_Capsense_Input_Arduino_Part_A -
//You will need to download Spacebrew and put the spacebrew library in the libraries folder
import spacebrew.*;
//import the Serial library so can read from arudino input via serial communication
import processing.serial.*;
// the number 10 is ASCII for linefeed (end of serial.println),
//later we will look for this to break up individual messages
int end = 10;
String serial; // declare a new string called 'serial'
Serial port; // The serial port, this is a new instance of the Serial class (an Object)
//These are general requirements of Spacebrew: Server, name, and description.
//Change the name of this server to "";
String server="sandbox.spacebrew.cc";
//name your project
String name="soft-circuits";
//Tell them what your code does. It will come up in the hover message.
String description ="Client that sends and receives boolean messages. Background turns yellow when message received.";
//Spacebrew Object
Spacebrew sb;
//name your value as an integer
int capSensor = 0;
void setup() {
// instantiate the spacebrewConnection variable
sb = new Spacebrew( this );
// declare your publishers
sb.addPublish( "Y_or_N", "boolean", true );
//Connecet your sketch online!
sb.connect(server, name, description );
//serial reading code
// initializing the object by assigning a port and baud rate (must match that of Arduino)
// you may(will!) need to change the number in [] to match which serial port your arduino is connected to
//println(Serial.list()); uncomment this if you need to list your serial ports - remember it starts at 0
port = new Serial(this, Serial.list()[5], 9600);
// function from serial library that throws out the first reading,
// in case we started reading in the middle of a string from Arduino
port.clear();
// function that reads the string from serial port
// until a println and then assigns string to our string variable (called 'serial')
serial = port.readStringUntil(end);
// initially, the string will be null (empty)
serial = null;
}
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
println(SensorInput);
//your sensorInput is coming in as a string separated by "," commas, use this to convert the string to a
//number so you can use boolean operators to compare.
capSensor = int(SensorInput[0]);
//the numbers coming in via serial are mapped to be low (0-3 range) if noone is touching the copper tape/capsensor,
// and towards the higher end of the 0-10 scale (7-10 range) if someone is touching it. So youre comparing the incoming
//sensor measure against "5" to see if the sensor is being touched.
if(capSensor >= 3){
sb.send("Y_or_N", true);
//^^if touched, send true
//the processing sketch on the other side (connected via spacebrew) will do something (light up LED) when it receives true
} else {
sb.send("Y_or_N", false); }
//^^ if not touched, send false.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.