Created
November 12, 2015 02:38
-
-
Save danaabs/a02a153038fd9e620e22 to your computer and use it in GitHub Desktop.
pcomp final test V1
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
// This empty example shows reading only | |
// Declare a "SerialPort" object | |
var serial; | |
var video; | |
var switchState = false; | |
var yay1; | |
var lastTime = 0; | |
var lastFC = 0; | |
var cnv; | |
var count = 0; | |
var images = []; | |
function preload() { | |
yay1 = loadSound('yay1.wav'); | |
} | |
function setup() { | |
cnv = createCanvas(600, 400); | |
video = createCapture(VIDEO); | |
video.hide(); | |
// 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); { | |
// Look at the data | |
// Do something with it, like to another variable? | |
console.log(data); | |
switchState = Number(data); | |
} | |
} | |
function draw() { | |
//lastFC = frameCount; | |
if (switchState) { | |
sound(); | |
if (floor(millis()) - lastTime > 5000) { | |
camera(); | |
count++ | |
// switch off after taking a picture | |
switchState = false; | |
} | |
} | |
// if (!switchState) { | |
// if (yay1.isPlaying()) { | |
// yay1.stop(); | |
// } | |
// } else if (switchState) { | |
// sound(); | |
// if (floor(millis()) - lastTime > 5000) { | |
// lastTime = floor(millis()); | |
// camera(); | |
// count++ | |
// } | |
// } | |
//print('frame: ' + frameCount); | |
print('last time: ' + lastTime); | |
print('millis: ' + millis()); | |
} | |
function lights() { | |
//turn on lights | |
} | |
function sound() { | |
//turn on sound | |
yay1.setVolume(0.8); | |
if (!yay1.isPlaying()) { | |
yay1.play(); | |
} | |
} | |
function camera() { | |
//capture image | |
var img = image(video, 0, 0, width, height); | |
save(cnv, 'bear' + count + '.jpg'); | |
} | |
/* | |
// Got the list of ports | |
function gotList(thelist) { | |
println("List of Serial Ports:"); | |
// theList is an array of their names | |
for (var i = 0; i < thelist.length; i++) { | |
// Display in the console | |
println(i + " " + thelist[i]); | |
} | |
}*/ | |
function switchPressed() { | |
switchState = !switchState; | |
lastTime = floor(millis()); | |
} | |
function mousePressed() { | |
switchPressed(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment