xxxxxxxxxx
53
let rVal = 0;
let alpha = 255;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(255);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
// Print the current values
text('Potentiometer Value = ' + str(rVal), 20, 50);
//text('alpha = ' + str(alpha), 20, 70);
}
let xpos = map(rVal, 0, 1023, 0, width); // Map the sensor value to the canvas width
ellipse(xpos, height / 2, 50, 50); // Draw an ellipse at the mapped position
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 1) {
rVal = int(fromArduino[0]);
}
}
}