Skip to content

Instantly share code, notes, and snippets.

@danaabs
Last active October 5, 2015 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danaabs/efa55a189c4690f7262b to your computer and use it in GitHub Desktop.
Save danaabs/efa55a189c4690f7262b to your computer and use it in GitHub Desktop.
Werner
// Declare a "SerialPort" object
var serial;
// Variable for size
var switchState = 1;
var herzog;
function preload() {
werner = loadSound('Chickens.mp3');
herzog = loadImage("werner.png");
}
function setup() {
createCanvas(600, 400);
werner.setVolume(0.8);
werner.play();
// Instantiate our SerialPort object
serial = new p5.SerialPort();
// Assuming our Arduino is connected, let's open the connection to it
// Change this to the name of your arduino's serial port
serial.open("/dev/cu.usbmodemfd121");
// if you need to see the list
// serial.onList(gotList);
// This is a new concept!
// Whenever there is new data, the "gotData" function happens.
// This is called a *CALLBACK*
serial.onData(gotData);
}
// This happens when there is data
function gotData() {
// Read the data as text (a string)!
var data = serial.readLine();
// Check to make sure something really came in
if (data.length > 0); {
// Get the 0 or 1
switchState = Number(data);
}
}
function draw() {
// Do something based on whether
// switch is on or off!
if (switchState === 1) {
background(0);
fill(255);
textAlign(CENTER);
textSize(48);
text('PLEASE, SIT. RELAX.', width / 2, height/2);
if (werner.isPlaying()) {
werner.pause();
}
} else if (switchState === 0) {
background(255);
fill(0);
if (!werner.isPlaying()) {
werner.play();
}
image(herzog, 0, 0, 500, 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment