Sending Arduino to Processing

Hey, Its my first time connecting the arduino to processing. I followed a tutorial to the T and double checked the code but i am still getting an error. can someone tell me where i went wrong?

Processing Code —
import processing.serial.*;

Serial mySerial;

String myString = null;
int ml = 10;
float myVal;

void setup(){
size(200,400);

String myPort = Serial.list() [1];
mySerial = new Serial(this, myPort, 9600);

}

void draw(){
while (mySerial.available() > 0){
myString = mySerial.readStringUntil(ml);//striping data

   if (myString != null) {
     background(0);
     myVal = float(myString);  //data#
 //   myVal = myVal/100 *height;
    println(myVal);
//   rectMode(CENTER);
//    rect(width/2,height =(myVal/2), 100, myVal);         
   }
 }

}

Arduino Code-----

void setup() {
//Int serial port for data
Serial.begin(9600);

}

void loop() {

// capture data in arduino
int variable1 = int(random(100));

Serial.println(variable1);

delay(100);
}

Thank you!

Hello,

It works here when I connect to the correct serial port.

Try adding this to setup() to see which COM ports are available:
https://processing.org/reference/libraries/serial/Serial_list_.html

Another resource:
https://itp.nyu.edu/physcomp/lessons/serial-communication/interpreting-serial-data/

:slight_smile:

Arduino:

void setup() 
  {
  //Int serial port for data
  Serial.begin(9600);
  }

void loop() 
  {
  // capture data in arduino
  int variable1 = int(random(100));
  Serial.println(variable1);
  delay(100);
  }

Processing:

import processing.serial.*;

Serial mySerial;

String myString = null;
int ml = 10;
float myVal;

void setup() 
  {
  size(200, 400);
  
  // List all the available serial ports
  printArray(Serial.list());
  
  String myPort = Serial.list() [0];
  mySerial = new Serial(this, myPort, 9600);
  }

void draw() 
  {
  while (mySerial.available() > 0) 
    {
    myString = mySerial.readStringUntil(ml); //striping data

    if (myString != null) 
      {
      background(0);
      myVal = float(myString);
      println(myVal);
      }
    }
  }

This needs work:

//    rect(width/2, height =(myVal/2), 100, myVal);

I will leave that with you.

image

Thank you, question how do i find the correct serial number if the serial monitor only comes up blank?

This is what I see:

I have a few serial ports for different devices.

You should see the COM port for your Arduino if you are connected to the same PC.

:slight_smile:

1 Like