xxxxxxxxxx
36
//Class Exercise 1: Controlling P5JS objects from Arduino
let xValue = 0;
function setup() {
createCanvas(700, 700);
}
function draw() {
background(220);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
}
else {
text("Connected", 20, 30);
ellipse(xValue, height/2, 150, 150);
}
}
//Sets up the serial connection
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
//Functino to read in serial data from the Serial Port.
function readSerial(data) {
if (data != null) {
//Mapping the sensor values from 'data' - 0 to 500 - 0
xValue = map(data, 0, 1023, 0, width);
}
}