SerialPort: Troubleshooting P5 - Arduino data

I have a basic Arduino FSR testing sketch. I have found my port and I can run this sketch in Arduino and it works fine. However I'm trying to have p5 read my Arduino data. The P5 sketch seems to work fine, but my Sensor Value is "undefined". I have tried variations of this P5 sketch and I get the same results. I'm hoping someone has had this same issue or knows where I am going wrong. Here is my P5 sketch:

var serial;          // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1411';  // fill in your serial port name here
var inData;                             // for incoming serial data


function setup() {
  createCanvas(400, 300);
  serial = new p5.SerialPort();       // make a new instance of the serialport library
  serial.on('list', printList);  // set a callback function for the serialport list event
  serial.on('connected', serverConnected); // callback for connecting to the server
  serial.on('open', portOpen);        // callback for the port opening
  serial.on('data', serialEvent);     // callback for when new data arrives
  serial.on('error', serialError);    // callback for errors
  serial.on('close', portClose);      // callback for the port closing

  serial.list();                      // list the serial ports
  serial.open(portName);              // open a serial port
}

function draw() {
  background(0);
  fill(255);
  text("sensor value: " + inData, 30, 30);
}

function printList(portList) {
 // portList is an array of serial port names
 for (var i = 0; i < portList.length; i++) {
 // Display the list the console:
 println(i + " " + portList[i]);
 }
}

function serverConnected() {
  println('connected to server.');
}

function portOpen() {
  println('the serial port opened.');
}

function serialEvent() {
  inData = Number(serial.read());
}

function serialError(err) {
  printIn('Something went wrong with the serial port. ' + err);
}

function portClose() {
  printIn('The serial port closed.');
}
Tagged:
Sign In or Register to comment.