Skip to content

Instantly share code, notes, and snippets.

@CalvinLogan
Created May 24, 2021 05:59
Show Gist options
  • Save CalvinLogan/2c0702621e0bcda3c0cfafd7f1da3e56 to your computer and use it in GitHub Desktop.
Save CalvinLogan/2c0702621e0bcda3c0cfafd7f1da3e56 to your computer and use it in GitHub Desktop.
//input your port on line 12, OR delete this and copy the starter code from the P5.serialcontrol application
let serial;
let latestData = "waiting for data";
let potLeft = 0;
let potRight = 0;
let slider = 0;
let button = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort();
serial.list();
serial.open('/dev/tty.usbmodem14601');
serial.on('connected', serverConnected);
serial.on('list', gotList);
serial.on('data', gotData);
serial.on('error', gotError);
serial.on('open', gotOpen);
serial.on('close', gotClose);
}
function serverConnected() {
print("Connected to Server");
}
function gotList(thelist) {
// print("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
// print(i + " " + thelist[i]);
}
}
function gotOpen() {
print("Serial Port is Open");
}
function gotClose(){
print("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
function gotError(theerror) {
print(theerror);
}
//where data comes in and we can parse it
//data comes in as a string
function gotData() {
let currentString = serial.readLine();//reading the code, looking for //commas and line breaks
if(currentString.length > 0) {
let sensorArray = split(currentString, ","); //splits string at //delimiter
slider = Number(sensorArray[0]);
potLeft = Number(sensorArray[1]);
potRight = Number(sensorArray[2]);
button = Number(sensorArray[3]);
}
}
function draw() {
background(255,255,255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment