xxxxxxxxxx
30
let sensor = 0;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
if (!serialActive) text("Click the screen to connect serial.", 50, 50); // display text to connect serial
let x = map(sensor, 0, 1023, 0, 400); // map sensor to canvas
ellipse(x, 200, 50, 50); // Draw the ellipse in the middle of the screen
}
// Serial code copied from example sketch, with some modifications
function mousePressed() {
if (!serialActive) {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data) {
if (data != null) {
// make sure there is actually a message
// split the message
sensor = trim(data);
}
}