xxxxxxxxxx
44
let xposition = 100;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(0, 255, 0);
fill(0);
ellipse(xposition, height / 2, 50, 50);
}
function keyPressed() {
if (key == " ") {
// Calls a function to set up the serial connection
setUpSerial();
}
}
// Function to read data from the serial port
function readSerial(data) {
// Check if data received is not null
if (data != null) {
// Maps the integer value of data from range 0-1023 to 0-640 and updates xposition
xposition = map(int(data), 0, 1023, 0, 640);
}
}
//Arduino Code
// void setup() {
// // Start serial communication so we can send data
// // over the USB connection to our p5js sketch
// Serial.begin(9600);
// }
// void loop() {
// int sensor = analogRead(A0);
// delay(5);
// Serial.println(sensor);
// }