xxxxxxxxxx
48
let port;
let connectBtn;
let strCont =0;
let nombre;
function setup() {
createCanvas(400, 400);
background(220);
port = createSerial();
let usedPorts = usedSerialPorts();
if (usedPorts.length > 0) {
port.open(usedPorts[0], 57600);
}
connectBtn = createButton('Connect to Arduino');
connectBtn.position(10, 10);
connectBtn.mousePressed(connectBtnClick);
}
function draw() {
background(220);
// reads in complete lines and prints them at the
// bottom of the canvas
nombre = port.readUntil("\n");
if (nombre.length > 0) {
strCont = nombre;
}
text(strCont, 10, height-20);
ellipse(200,200, strCont, strCont);
// changes button label based on connection status
if (!port.opened()) {
connectBtn.html('Connect to Arduino');
} else {
connectBtn.html('Disconnect');
}
}
function connectBtnClick() {
if (!port.opened()) {
port.open('Arduino', 57600);
} else {
port.close();
}
}