Created
October 22, 2015 05:13
-
-
Save ohc209/0db087b9bb05941570cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declare a "SerialPort" object | |
var serial; | |
var song; | |
// Variable for size | |
var switchState = 0; | |
function preload() { | |
song = loadSound('assets/sade.mp3'); | |
} | |
function setup() { | |
createCanvas(windowWidth, windowHeight); | |
background(0); | |
song.setVolume(1); | |
song.stop(); | |
// 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.usbmodem1411"); | |
// 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) { | |
song.playMode('sustain'); | |
song.play(); | |
background(255, 0, 255); | |
fill(255); | |
textAlign(CENTER); | |
textSize(24); | |
text('WAKE UP', width / 2, height - 12); | |
} | |
} | |
function mousePressed() { | |
if (song.isPlaying()) { // .isPlaying() returns a boolean | |
song.stop(); | |
background(0); | |
} else { | |
song.play(); | |
background(255, 255, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment