xxxxxxxxxx
100
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.wchusbserialfa130'; // fill in your serial port name here
var inData;
function setup() {
//Serial
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 = 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); // open a serial port
var options = { baudrate: 9600}; // change the data rate to whatever you wish
serial.open(portName, options);
//Interface
createCanvas(320, 480);
smooth(); // antialias drawing lines
background(46, 67, 114);
line(0, 0, width, height);
textSize(32); //text size
fill(21, 43, 85);
text('Temperature (ºC)',25,50);
textSize(20);
text('Port Hull',45,110);
text('StarBoard Hull',20,185);
text('Water',60,260);
text('Air',75,340);
//noFill();
fill(6, 22, 57);
rect(175, 70, 70, 70); // Draw rectangle, temperature port hull
rect(175, 145, 70, 70); // Draw rectangle, temperature startboard hull
rect(175, 220, 70, 70); // Draw rectangle, temperature water
rect(175, 295, 70, 70); // Draw rectangle, temperature air
textSize(32); //text size
fill(21, 43, 85);
text('UV index',25,390);
fill(6, 22, 57);
rect(25, 400, 250, 60); // UV index window
}
function draw() {
background(0);
fill(255);
text("sensor value: " + inData, 30, 30);
}
//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() {
inData = Number(serial.read());
// 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, ','); // split the string on the commas
if (sensors.length > 2) { // if there are three elements
locH = map(sensors[0], 250, 410, 0,width); // element 0 is the locH
locV = map(sensors[1], 250, 410, 0, height); // element 1 is the locV
circleColor = 255 - (sensors[2] * 255); // element 2 is the button
}
}
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
}