xxxxxxxxxx
64
let fVal = 0;
let micVal = 0;
function setup() {
createCanvas(900, 600);
textSize(18);
}
function draw() {
background(255)
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
fill(0);
text("Connected", 20, 30);
text("Mic", 20, 50);
text(micVal, 100, 50);
text("Force", 20, 70);
text(fVal, 100, 70);
background(255);
// map the fVal variable to the height of the canvas
let y = map(fVal, 0, 1023, height-50, 50);
// draw the graph
stroke(0);
fill("pink");
circle(width/2, y, 50);
}
}
function keyPressed() {
if (key == " ") {
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 == 2) {
micVal = fromArduino[0];
fVal = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "0,0\n";
writeSerial(sendToArduino);
}
}