xxxxxxxxxx
32
let ellipseHorizental;
function setup() {
createCanvas(640, 480);
textSize(18);
ellipseHorizental = width/2;
}
function draw() {
background(220);
// Draw ellipse with width based on potentiometer value
fill("green");
ellipse(ellipseHorizental, height / 2, 100, 150);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
// Print the current potentiometer value
text('Potentiometer Value = ' + str(ellipseHorizental), 20, 50);
}
}
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
function readSerial(data) {
if (data != null) {
// convert the string to a number using int()
let fromArduino = split(trim(data), ",");
// Map the potentiometer value to the ellipse width
ellipseHorizental = map(int(fromArduino[0]), 0, 1023, 0, 640);
}
}