Skip to content

Instantly share code, notes, and snippets.

@sabotai
Created October 15, 2015 20:32
Show Gist options
  • Save sabotai/b77dd163944891f341dc to your computer and use it in GitHub Desktop.
Save sabotai/b77dd163944891f341dc to your computer and use it in GitHub Desktop.
connect spacebrew and arduino for cclab
/*
* Button Example WITH ARDUINO SERIAL SUPPORT -- SEE BOTTOM FOR THE ARDUINO CODE
*
* Spacebrew library button example that send and receives boolean messages.
* COMBINED WITH ARDUINO SERIAL RECEIVE
*/
import spacebrew.*;
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
String server="sandbox.spacebrew.cc";
String name="P5 Button Example";
String description ="Client that sends and receives boolean messages. Background turns yellow when message received.";
Spacebrew sb;
color color_on = color(255, 255, 50);
color color_off = color(255, 255, 255);
int currentColor = color_off;
void setup() {
frameRate(240);
size(500, 400);
// instantiate the spacebrewConnection variable
sb = new Spacebrew( this );
// declare your publishers
sb.addPublish( "button_pressed", "boolean", true );
// declare your subscribers
sb.addSubscribe( "change_background", "boolean" );
// connect to spacebrew
sb.connect(server, name, description );
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600); //YOU MAY NEED TO CHANGE THIS
}
void draw() {
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
// set background color
background( currentColor );
// draw button
fill(255,0,0);
stroke(200,0,0);
rectMode(CENTER);
if (val != 0){
currentColor = color_on;
} else {
currentColor = color_off;
}
ellipse(width/2,height/2,250,250);
// add text to button
fill(230);
textAlign(CENTER);
textSize(24);
if (mousePressed == true) {
text("That Feels Good", width/2, height/2 + 12);
} else {
text("Click Me", width/2, height/2 + 12);
}
}
void mousePressed() {
// send message to spacebrew
sb.send( "button_pressed", true);
}
void mouseReleased() {
// send message to spacebrew
sb.send( "button_pressed", false);
}
void onBooleanMessage( String name, boolean value ){
println("got bool message " + name + " : " + value);
// update background color
if (value == true) {
currentColor = color_on;
} else {
currentColor = color_off;
}
}
/////////////// ARDUINO CODE CUT-PASTE AND UPLOAD TO ARDUINO
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
/*
int switchPin = 2; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment