đź’»

Computational Media: Final

Computational Media: Final

Codebase available on GitHub:

As someone who had never coded before up until 1.5 years ago, something that fundamentally changed my understanding of computer systems was understanding where libraries like p5.js sit in the broader landscape. Running p5.js outside the web editor, serving it on my own machine and experimenting with different libraries was extremely valuable to me. When you run p5.js in your own local development environment, you gain greater control over the project's structure and can integrate and grow it with modern web development workflows. You can use package managers, build tools, and combine it with other libraries or frameworks. Part of that work is using the Web Serial API instead of a p5 specific library.

Not only that, integrating serial data with React Three Fiber and Arduino opens up a lot of possibilities for creating interactive installations by connecting physical sensors to 3D web environments through serial communication. By establishing real-time data flow between Arduino sensors and a web application, I can start creating responsive installations where physical interactions trigger digital responses. However, this setup requires careful management of the serial connection. Only one application can access a serial port at a time, for example.

At time of presentation, my final project ended up being a bit of a research project on how to integrate serial data with React Three Fiber and Three.js.

Bridging Physical and Digital: Serial Data in React Three Fiber

The process involves three main components:

  • Arduino sending sensor data in CSV format - using PlatformIO to execute and build the project
  • Serial communication handling in React
  • Data visualization in Three.js scene - using React.js, React Three Fiber and Drew

Key Considerations for future implementation

Serial Port Management:

  • Only one application can access a serial port at a time
  • Close the Serial Monitor in Arduino IDE when using WebSerial in the browser
  • Handle proper cleanup when component unmounts

Data Synchronization:

  • Add small delays between row readings to account for analog-to-digital converter limitations
  • Use proper calibration to establish baseline values - currently taking the average of 50 readings, however this could use some work.

State Management:

  • Use refs instead of state for visualization updates to prevent unnecessary re-renders
  • Trigger redraws only when new serial data arrives

Performance Optimization:

  • Avoid console.log during serial communication as it can slow down the process
  • Consider implementing a call-and-response pattern between p5 and Arduino

Other common pitfalls

  • Reset timing: If you reset without unplugging, the monitor reads correctly, but if you reset and unplug, the monitor may not complete successfully
  • React can't read if PlatformIO is reading the serial port - close the monitor!
  • Handle cleanup properly in your React component by closing the serial port when the component unmounts

Code samples: Getting serial data into Three.JS

image

It actually worked! The 36 sensors were mapped to the scale of the Tree.js object and it would move depending where the user touches one of the 36 homemade force sensing resistor sensor! Later on, to p5.js only (not a React app!) we managed to plug all 108 and ultimately showed a decent result on the Winter Show. The visuals there are lacking - will continue the work.

Code for Three JS scene

/src/App.jsx

/src/components/Scene.jsx

Arduino code

Next steps:

  • Fetching news story data from API. In the end we switched to p5 for simplicity’s sake within the team (visualisation below with all 108 sensors mapped to p5.)
  • How to render text within WebGL? We had a major bug when implementing p5 and I suspect it’s because WebGL would have some trouble rendering p5.
image

Notes from first feedback session

  • There’s a p5 speech library, because Chrome has text to speech.
  • For the news, can use dummy data like Reductress or The Onion
  • Prof recommends figuring out API last because what’s important is the interaction.
  • Condition the interaction.
  • People may treat it as game or press on it with brute force.
  • You can start pre-loading right as the user hovers.
  • For a project with a lot of images, load them on demand, but don’t call methods on the images until they’ve been loaded.