1

I am tyring to connect my arduino to P5.js through node js for a school assignment. We need to download the Serial Port application from github, install it in our terminal, connect our arduino, connect to p5js and I think also node js.

I have downloaded the serial port application as well as node js. I have followed instructions to get it running in my terminal. I have recieved the "p5.serialseriver is running!" message. However, in my p5.js sketch, when I try to run the code for the next step, I get an error that says "p5.SerialPort is not a constructor"

let serial;
let latestData = "waiting for data";

function setup(){
  createCanvas(windowWidth, windowHeight);
  
  serial = new p5.SerialPort();
  
  serial.list();
  serial.open('/dev/cu.usbserial-1120');
  serial.on('connected', serverConnected);
  serial.on('list', gotList);
  serial.on('data', gotData);
  serial.on('open', gotOpen);
  serial.on('colse', gotClose);
            
}

Hoping to connect the Arduino to P5.js

1 Answer 1

1

You have to add some code to index.html in order for editor to recognize Serial port:

    <script language="javascript" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.serialport.js"></script>

There is also an app called p5.serialcontrol which will help format code for serial connections.

For example, the following code was generated for my system using the app p5.serialcontrol:

let serial;
let latestData = "waiting for data";

function setup() {
 createCanvas(windowWidth, windowHeight);

 serial = new p5.SerialPort();

 serial.list();
 serial.open('/dev/tty.usbmodem143301');

 serial.on('connected', serverConnected);

 serial.on('list', gotList);

 serial.on('data', gotData);

 serial.on('error', gotError);

 serial.on('open', gotOpen);

 serial.on('close', gotClose);
}

function serverConnected() {
 print("Connected to Server");
}

function gotList(thelist) {
 print("List of Serial Ports:");

 for (let i = 0; i < thelist.length; i++) {
  print(i + " " + thelist[i]);
 }
}

function gotOpen() {
 print("Serial Port is Open");
}

function gotClose(){
 print("Serial Port is Closed");
 latestData = "Serial Port is Closed";
}

function gotError(theerror) {
 print(theerror);
}

function gotData() {
 let currentString = serial.readLine();
  trim(currentString);
 if (!currentString) return;
 console.log(currentString);
 latestData = currentString;
}

function draw() {
 background(255,255,255);
 fill(0,0,0);
 text(latestData, 10, 10);
 // Polling method
 /*
 if (serial.available() > 0) {
  let data = serial.read();
  ellipse(50,50,data,data);
 }
 */
}

I was able to connect to an Arduino UNO and successfully ran this code:

unsigned long LoopTimer = 0;
byte outputValue = 0;

const int LoopTime50 = 50;
const int LoopTime100 = 100;
const int LoopTime20000 = 20000;

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

void loop() {
  if (micros() >= LoopTimer) {
    LoopTimer += LoopTime100;
    outputValue += 8;
    Serial.println(outputValue);
  }
}

You will have to change the serial port name for your system. You should be able to download the app here: https://github.com/p5-serial . Use this app to open the serial port also.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you! I ran the first code that you suggested and it went through. I thought something was missing. The module video from my school didn't show the functions at all. I am new and though somehow they were being defined some other way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.