xxxxxxxxxx
44
let xPos = 0;
let bgColor;
let ellipseColor;
function setup() {
createCanvas(640, 480);
textSize(18);
bgColor = color(0);
ellipseColor = color(255, 0, 0);
}
function draw() {
background(bgColor);
fill(ellipseColor);
ellipse(xPos, height / 2, 40, 60);
if (!serialActive) {
fill(255);
text("Press Space Bar to select Serial Port", 20, 30);
} else {
fill(255);
text("Connected", 20, 30);
text('xPos = ' + str(xPos), 20, 50);
}
}
function keyPressed() {
if (key == " ") {
// Start the serial connection
setUpSerial();
}
}
function readSerial(data) {
if (data != null) {
let fromArduino = split(trim(data), ",");
if (fromArduino.length == 1) {
xPos = map(int(fromArduino[0]), 150, 750, 0, width);
bgColor = color(map(int(fromArduino[0]), 0, 1023, 0, 255), 0, 255 - map(int(fromArduino[0]), 0, 1023, 0, 255));
ellipseColor = color(255 - map(int(fromArduino[0]), 0, 1023, 0, 255), map(int(fromArduino[0]), 0, 1023, 0, 255), 0);
}
}
}