xxxxxxxxxx
49
let value = 0;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(255);
stroke(255);
fill(0);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
// This function will be called by the web-serial library
// with each new *line* of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
function readSerial(data) {
if (data != null) {
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
value = int(map(mouseY,0, height, 0, 255));
value2 = int(map(mouseX,0, width, 0, 255));
let sendToArduino = value + "\n";
let sendToArduino2 = value2 + "\n";
writeSerial(sendToArduino);
writeSerial(sendToArduino2);
print(value);
print(value2)
}
}