Production Assignment – Week 12

For this assignment, we were tasked to control an ellipse using sensor data from arduino.

For this, I’m using readings from a potentiometer.

Arduino Code

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0); // Read the value from the potentiometer
  Serial.println(sensorValue); // Give P5 the value over serial
  delay(10);
}

P5 Code

let sensor = 0;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  if (!serialActive) text("Click the screen to connect serial.", 50, 50); // display text to connect serial
  
  let x = map(sensor, 0, 1023, 0, 400); // map sensor to canvas
  ellipse(x, 200, 50, 50); // Draw the ellipse in the middle of the screen
}

// Serial code copied from example sketch, with some modifications

function mousePressed() {
  if (!serialActive) {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}

function readSerial(data) {
  if (data != null) {
    // make sure there is actually a message
    // split the message
    sensor = trim(data);
  }
}

P5 Embedded

Video

Leave a Reply