Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
for jordan
//serial variables
var serial; // variable to hold an instance of the serialport library
var portName = "/dev/cu.usbmodem1421"; // fill in your serial port name here
// sensor variables
var lineBegin = 0;
var button1 = 0;
var button2 = 0;
function setup() {
serial = new p5.SerialPort(); // make a new instance of the serialport library
// serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
// serial.list(); // list the serial ports
serial.open(portName);
}
/////////////////////////// Serial Functions ///////////////////////////
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
function serverConnected() {
println('connected to server.'); }
function portOpen() {
println('the serial port opened.');
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
var inString = serial.readStringUntil('\r\n');
//check to see that there's actually a string there:
if (inString.length > 0) {
var sensors = split(inString, '\t'); // split the string on the commas
if (sensors.length > 2) { // if there are three elements
lineBegin = sensors[0];
if ( lineBegin === '(!)') {
button1 = sensors[1];
button2 = sensors[2];
console.log(lineBegin + ", " + button1 + ", " + button2);
}
}
}
}
/////////////////////////// End Serial Functions ///////////////////////////
function draw() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment