xxxxxxxxxx
31
let xVal = 0; //initializing the X value for the position
function setup() {
createCanvas(640, 480);
}
function draw() {
background(230);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
// draw an ellipse
ellipse(xVal, height/2, 100, 50); //giving the ellipse the value of the x position initialized above
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data) {
if (data != null) {
xVal = map(data, 0, 500, 0, width); //mapping the values received from the photosensor to control the X position of the ellipse
console.log(data);
}
}