xxxxxxxxxx
43
let circleX = 0;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(190);
stroke(0);
fill("purple");
circle(map(circleX,0, 1023, 0, width), height/2, 50);
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) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
circleX = int(trim(data));
}
print(circleX);
}