All code examples are hosted on Github.
...
If everything works you are (almost) set! 🔥
BE CAREFUL! If you have Serial port monitor open the connection is going to break up.
To write code in p5.js we will use Processing, with p5.js mode enabled. Running the sketch opens a live server directly in the browser.
An index.html
file is created in each new sketch folder. It contains a section where each .js
file from the sketch is added automatically. Removing this block of code (it’s clearly marked in the file) will cause the sketch to no longer run inside the PDE.
Add library files or additional code to the libraries
subfolder of the sketch. References to that code will automatically be added to the HTML file, though the libraries won’t be visible as tabs in the Editor.
...
Install VS Code : https://code.visualstudio.com/docs/setup/setup-overview
Follow the steps in the video to properly setup p5.js in VS Code.
...
Two-way Communication Simple
...
Example
The following example demonstrates two-way communication between Arduino and P5js. Using This example shows a simple way to avoid the delay() function; delay() can cause many issues on Arduino , and can be a big problem if we want to read incoming serial communication. However, if we are only sending send serial out on the Arduino, the delay() function will generally be ok to use.
Setup
For this example, we need to include the serial library in our p5js sketch.
We can also set up the following circuit to check our inputs and outputs on the Arduino, although the code will still work without these parts connected , or with an alternative sensor or output for pins 10 and 11;on A0.
...
Arduino Code:
Code Block |
---|
#define NUM_VALUES 2 #define OUTPUT_PIN1 11 #define OUTPUT_PIN2 10 long lastSerialOut = 0; void setup() { Serial.begin(9600); pinMode(OUTPUT_PIN1, OUTPUT); pinMode(OUTPUT_PIN2, OUTPUT); } void loop() { if (millis() > lastSerialOut + 100) { // only send once every 100 miliseconds // Send sensor data out over Serial Serial.println(analogRead(A0)/4); lastSerialOut = millis(); } } /* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. */ void serialEvent() { // Check to see if there is any incoming serial data if (Serial.available() > 0) { int incomingValues[NUM_VALUES]; // read string until the end of the line String rcvdSerialData = Serial.readStringUntil('\n'); split(rcvdSerialData, incomingValues, NUM_VALUES); analogWrite(OUTPUT_PIN1, incomingValues[0]); analogWrite(OUTPUT_PIN2, incomingValues[1]); } } void split(String inputString, int returnData[], int numOfValues) { // split comma seperated values into an array int index = 0; int lastPos = 0; for (int i = 0; i < inputString.length(); i++) { if (inputString.charAt(i) == ',' || inputString.charAt(i) == ';' && index < numOfValues) { String tempStr = inputString.substring(lastPos, i); // uncoment this line to test // Serial.println(tempStr); returnData[index] = tempStr.toInt(); index++; lastPos = i + 1; } } } |
...