xxxxxxxxxx
77
let serial;
let indata=0;
let x=0;
let portname="/dev/tty.usbmodem141401";
let options={
baudRate:9600
};
function setup() {
createCanvas(windowWidth,windowHeight)
// Instantiate our SerialPort object
serial = new p5.SerialPort();
// Let's list the ports available
let portlist = serial.list();
// 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(portname, options);
// Register some callbacks
// When we connect to the underlying server
serial.on('connected', serverConnected);
// When we get a list of serial ports that are available
serial.on('list', gotList);
// When we some data from the serial port
serial.on('data', gotData);
// When or if we get an error
serial.on('error', gotError);
// When our serial port is opened and ready for read/write
serial.on('open', gotOpen);
}
// We are connected and ready to go
function serverConnected() {
console.log("We are connected!")
}
// Got the list of ports
function gotList(thelist) {
// theList is an array of their names
for (let i = 0; i < thelist.length; i++) {
// Display in the console
console.log(i + " " + thelist[i]);
}
}
// Connected to our serial device
function gotOpen() {
console.log("Serial Port is open!");
}
// Ut oh, here is an error, let's log it
function gotError(theerror) {
console.log(theerror);
}
// There is data available to work with from the serial port
function gotData() {
indata = serial.read();
x= map(indata,0,255,0,width)
// console.log(indata);
}
function draw() {
background(0,10,255);
fill(0,255,255);
ellipse(x, height/2,50,50);
}