Task 1: make something that uses only one sensor on Arduino and makes the ellipse in p5 move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by p5.
We used a potentiometer as the analog sensor on Arduino to move the ellipse on the horizontal axis.
p5js code:
// Serial port object
let port;
// Latest sensor value
let sensorVal = 0;
function setup() {
createCanvas(400, 300);
background(220);
// Create the serial connection object
port = createSerial();
// If a port was used before, auto-reconnect
let used = usedSerialPorts();
if (used.length > 0) {
port.open(used[0], 9600);
}
}
function draw() {
background(220);
// Read one line of text until newline "\n"
let str = port.readUntil("\n");
// Make sure we actually received something
if (str.length > 0) {
// Convert the string into an integer
let val = int(str.trim());
// Map sensor value (0–1023) to screen X position (0–400)
let x = map(val, 0, 1023, 0, width);
// Draw a circle at mapped position
ellipse(x, height / 2, 40, 40);
} else {
// If empty data is received, print it for debugging
console.log("Empty:", str);
}
}
Arduino code:
const int sensor=A2;
int sensorValue;
void setup() {
// put your setup code here, to run once:
pinMode(sensor,INPUT);
Serial.begin(9600);
Serial.println(0); // send a starting message
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue=analogRead(sensor);
delay(60);
Serial.println(sensorValue);
}
Task 2: make something that controls the LED brightness from p5.
We decided to control the LED’s brightness through four buttons on p5js.
p5js code:
let port;
let brightnessToSend = 0;
const baudrate = 9600;
function setup() {
createCanvas(400, 400);
// Create the serial port
port = createSerial();
// connect automatically if used before
let usedPorts = usedSerialPorts();
if (usedPorts.length > 0) {
port.open(usedPorts[0], baudrate);
} else {
connectBtn = createButton("Connect to Serial");
connectBtn.position(10, 10);
connectBtn.mousePressed(() => port.open(baudrate));
}
}
function draw() {
background(30);
drawStarButton(80, 150, 40, 20, 5, 64);
drawStarButton(160, 150, 40, 20, 5, 128);
drawStarButton(240, 150, 40, 20, 5, 192);
drawStarButton(320, 150, 40, 20, 5, 255);
fill(255);
textAlign(CENTER);
text("Current brightness: " + brightnessToSend, width / 2, 280);
}
function mousePressed() {
if (!port.opened()) return; // p5.webserial function to check if open
let stars = [
{ x: 80, brightness: 64 },
{ x: 160, brightness: 128 },
{ x: 240, brightness: 192 },
{ x: 320, brightness: 255 }
];
for (let s of stars) {
if (dist(mouseX, mouseY, s.x, 150) < 30) {
brightnessToSend = s.brightness;
// Send brightness (0–255)
port.write(brightnessToSend);
}
}
}
function drawStarButton(x, y, radius1, radius2, points, brightness) {
let angle = TWO_PI / points;
let halfAngle = angle / 2;
if (brightnessToSend === brightness) fill(255, 200, 0);
else fill(255);
push();
translate(x, y);
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
vertex(cos(a) * radius1, sin(a) * radius1);
vertex(cos(a + halfAngle) * radius2, sin(a + halfAngle) * radius2);
}
endShape(CLOSE);
pop();
}
Arduino code:
int ledPin = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
int brightness = Serial.read(); // 0–255
analogWrite(ledPin, brightness); // PWM LED brightness
}
}
Video:
Task 3: take the gravity wind example and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor.
p5js code: