Trying to establish a serial Communication with Processing and Arduino and getting error messages involving Serial and Port cannot be recognized

In the book Getting Started With Arduino I am using Processing and Arduino programs to a Serial Communication to run Processing with an Arduino Lamp.
I try to run the program and I get these error messages:
the type Serial is ambiguous
the variable port cannot be recognized
the name port cannot be recognized
the function "hasMoreTokens" does not exist.
My code is written correctly as in the book so this is not the problem.
I am a beginner so please consider that and thanks for any attention with help!

I don’t see any code

import processing.serial.;
import java.net.
;
import java.io.;
import java.util.
;
String feed ="Make: DIY Projects and Ideas for Makers";

int interval = 5 * 60 * 1000;
int lastTime;
int love = 0;
int peace = 0;
int arduino = 0;

int light = 0;
Serial port;
color c;
String cs;

String buffer = "";

PFont font;
void setup(){
size (640,480);
frameRate (10);
font = createFont ("Helvetica" , 24);
fill(255);
textFont(font,32);

String arduinoPort = Serial.list()[0];
port = new Serial (this, arduinoPort, 9600);

lastTime = millis();
fetchData();
}
void draw(){
background(c);
int n =(lastTime + interval - millis())/1000;

c = color(peace,love,arduino);
cs = "#" + hex(c, 6);
text("Arduino Networked Lamp",10,40);
text("Reading feed:",10,100);
text(feed,10,140);
text ("Next update in "+n+"seconds",10,450);
text("peace",10,200);
text(""+peace,130,200);
rect(200,172,peace,28);

text ("love",10,240);
text(""+love,130,240);
rect(200,212,love,28);

text("arduino",10,280);
text(""+arduino,130,280);
rect(200,252,arduino,28);

text("sending",10,340);
text(cs,200,340);
text("light level",.10,380);
rect(200,352,light/10.23,28);
if (n<=0){
fetchData();
lastTime=millis();
}
port.write(cs);
if (port.available()>0){
int inByte = port.read();
if (inByte !=10){
buffer = buffer + char (inByte);
}else{
if(buffer.length()>1){
buffer = buffer.substring(0,buffer.length()-1);
light = int(buffer);
buffer = "";
port.clear();
}
}
}
}
void fetchData(){
String data;
String chunk;
love = 0;
peace = 0;
arduino = 0;
try{
URL url = new URL (feed);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader in = new
BufferedReader(new InputStreamReader(conn.getInputStream()));
while ( (data = in.readLine())!=null){

  StringTokenizer st=
 new StringTokenizer(data, "\" <>,.()[]");
  while (st.hasMoreTokens()){
    chunk=st.nextToken().toLowerCase();
    
    if(chunk.indexOf("love")>=0)
    love++;
    if (chunk.indexOf("peace")>= 0)
    peace++;
    if (chunk.indexOf("arduino")>= 0)
    arduino++;
  }
}
if (peace>64) peace = 64;
if (love>64 ) love = 64;
if (arduino>64) arduino = 64;
peace = peace * 4;
love = love* 4;
arduino =arduino * 4;

}

catch (Exception ex) {
ex.printStackTrace();
System.out.println("ERROR:"+ex.getMessage());
}
}

import processing.serial.;
import java.net.
;
import java.io.;
import java.util.
;
String feed ="Make: DIY Projects and Ideas for Makers";

int interval = 5 * 60 * 1000;
int lastTime;
int love = 0;
int peace = 0;
int arduino = 0;

int light = 0;
Serial port;
color c;
String cs;

String buffer = "";

PFont font;
void setup(){
size (640,480);
frameRate (10);
font = createFont ("Helvetica" , 24);
fill(255);
textFont(font,32);

String arduinoPort = Serial.list()[0];
port = new Serial (this, arduinoPort, 9600);

lastTime = millis();
fetchData();
}
void draw(){
background(c);
int n =(lastTime + interval - millis())/1000;

c = color(peace,love,arduino);
cs = "#" + hex(c, 6);
text("Arduino Networked Lamp",10,40);
text("Reading feed:",10,100);
text(feed,10,140);
text ("Next update in "+n+"seconds",10,450);
text("peace",10,200);
text(""+peace,130,200);
rect(200,172,peace,28);

text ("love",10,240);
text(""+love,130,240);
rect(200,212,love,28);

text("arduino",10,280);
text(""+arduino,130,280);
rect(200,252,arduino,28);

text("sending",10,340);
text(cs,200,340);
text("light level",.10,380);
rect(200,352,light/10.23,28);
if (n<=0){
fetchData();
lastTime=millis();
}
port.write(cs);
if (port.available()>0){
int inByte = port.read();
if (inByte !=10){
buffer = buffer + char (inByte);
}else{
if(buffer.length()>1){
buffer = buffer.substring(0,buffer.length()-1);
light = int(buffer);
buffer = "";
port.clear();
}
}
}
}
void fetchData(){
String data;
String chunk;
love = 0;
peace = 0;
arduino = 0;
try{
URL url = new URL (feed);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader in = new
BufferedReader(new InputStreamReader(conn.getInputStream()));
while ( (data = in.readLine())!=null){

  StringTokenizer st=
 new StringTokenizer(data, "\" <>,.()[]");
  while (st.hasMoreTokens()){
    chunk=st.nextToken().toLowerCase();
    
    if(chunk.indexOf("love")>=0)
    love++;
    if (chunk.indexOf("peace")>= 0)
    peace++;
    if (chunk.indexOf("arduino")>= 0)
    arduino++;
  }
}
if (peace>64) peace = 64;
if (love>64 ) love = 64;
if (arduino>64) arduino = 64;
peace = peace * 4;
love = love* 4;
arduino =arduino * 4;

}

catch (Exception ex) {
ex.printStackTrace();
System.out.println("ERROR:"+ex.getMessage());
}
}

Did you try to put that code into the Arduino IDE?

no that is in the processing code I have different code i Arduino
const int SENSOR = 0;
const int R_LED = 9;
const int G_LED = 10;
const int B_LED = 11;
const int BUTTON = 12;

int val = 0;
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;

byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR);
Serial.println(val);

if (Serial.available()>0) {
inByte=Serial.read();
if (inByte == '#') {
while (pointer < 6) {
buffer[pointer] = Serial.read();
pointer++;
}
r=hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g=hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b=hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0;
}
}
btn = digitalRead(BUTTON);
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn;
if (state == 1){
analogWrite(R_LED, r);
analogWrite(G_LED, g);
analogWrite(B_LED, b);
}else {
analogWrite(R_LED, 0);
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100);
}
int hex2dec(byte c) {
if (c >= '0' && c <= '9'){
return c - '0';
}else if (c >= 'A' && c <= 'F'){
return c - 'A' + 10;
}
}

const int SENSOR = 0;
const int R_LED = 9;
const int G_LED = 10;
const int B_LED = 11;
const int BUTTON = 12;

int val = 0;
int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7] ;
int pointer = 0;
byte inByte = 0;

byte r = 0;
byte g = 0;
byte b = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT);
}
void loop() {
val = analogRead(SENSOR);
Serial.println(val);

if (Serial.available()>0) {
inByte=Serial.read();
if (inByte == '#') {
while (pointer < 6) {
buffer[pointer] = Serial.read();
pointer++;
}
r=hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
g=hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
b=hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
pointer = 0;
}
}
btn = digitalRead(BUTTON);
if ((btn == HIGH) && (old_btn == LOW)){
state = 1 - state;
}
old_btn = btn;
if (state == 1){
analogWrite(R_LED, r);
analogWrite(G_LED, g);
analogWrite(B_LED, b);
}else {
analogWrite(R_LED, 0);
analogWrite(G_LED, 0);
analogWrite(B_LED, 0);
}
delay(100);
}
int hex2dec(byte c) {
if (c >= '0' && c <= '9'){
return c - '0';
}else if (c >= 'A' && c <= 'F'){
return c - 'A' + 10;
}
}

So, what were the error messages, and where did they come from?
(please use code tags)

I apologize I do not know what code tags are>
I am reading at the bottom of the Processing code I am trying to run that ,
the type Serial is ambiguous
thje name port cannot be recognized
the function "hasMoretokens" does not exist

Are you sure about those periods, before the semicolons?

that is true the period before the semicolon is wrong I have neglected to fix that , I will admit I did not think that would create these errors. I will fix that ! thank you

It really says that?

Please, just cut and paste - don’t try to reinterpret

it is this without my typo earlier:
"the name port cannot be recognized"
I cannot cut and paste < these error messages are below the sketch and I cannot cut or copy and paste

I haven’t used Processing for quite some time, but the last time I used it, it had a feature to copy the error messages, very like the Arduino IDE.

Hi, @silverpaths
Welcome to the forum.

Can I suggest you forget your code for the moment, there is a lot of code and its hard to test.

Write some simple code for Processing and Arduino and check that you have that setup first.

Write your code in stages, first stage code to get comms between the two programs.

There are I can remember, simple code in both IDE to do basic data transfers that are nowhere as long as yours.

So please start small...

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

Screenshot.

Find Coding Train on YouTube and find his Interfacing with Arduino video. I had trouble with my comm port not showing, also.

thank you for this information!

That is excellent advice Tom !
Thank you !
Ron Henson

import processing.serial.*;
import java.net.*;
import java.io.*;
import java.util.*;
String feed ="http://makezine.com/feed/";

int interval = 5 * 60 * 1000;
int lastTime;
int love = 0;
int peace = 0;
int arduino = 0;

int light = 0;
Serial port;
color c;
String cs;

String buffer = "";

PFont font;
void setup(){
  size (640,480);
  frameRate (10);
 font = createFont ("Helvetica" , 24);
 fill(255);
 textFont(font,32);
 
 String arduinoPort = Serial.list()[0];
 port = new Serial (this, arduinoPort, 9600);
 
 lastTime = millis();
 fetchData();
}
void draw(){
  background(c);
  int n =(lastTime + interval - millis())/1000;
  
  c = color(peace,love,arduino);
  cs = "#" + hex(c, 6);
  text("Arduino Networked Lamp",10,40);
  text("Reading feed:",10,100);
  text(feed,10,140);
   text ("Next update in "+n+"seconds",10,450);
   text("peace",10,200);
   text(""+peace,130,200);
   rect(200,172,peace,28);
   
   text ("love",10,240);
   text(""+love,130,240);
   rect(200,212,love,28);
   
   text("arduino",10,280);
   text(""+arduino,130,280);
   rect(200,252,arduino,28);
   
   text("sending",10,340);
   text(cs,200,340);
   text("light level",10,380);
   rect(200,352,light/10.23,28);
   if (n<=0){
     fetchData();
     lastTime=millis();
   }
   port.write(cs);
   if (port.available()>0){
     int inByte = port.read();
     if (inByte !=10){
       buffer = buffer + char (inByte);
     }else{
       if(buffer.length()>1){
         buffer = buffer.substring(0,buffer.length()-1);
         light = int(buffer);
         buffer = "";
         port.clear();
       }
     }
   }
}
void fetchData(){
  String data;
  String chunk;
  love = 0;
  peace = 0;
  arduino = 0;
  try{
    URL url = new URL (feed);
    URLConnection conn = url.openConnection();
    conn.connect();
    BufferedReader in = new
    BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ( (data = in.readLine())!=null){
      
      StringTokenizer st=
     new StringTokenizer(data, "\" <>,.()[]");
      while (st.hasMoreTokens()){
        chunk=st.nextToken().toLowerCase();
        
        if(chunk.indexOf("love")>=0)
        love++;
        if (chunk.indexOf("peace")>= 0)
        peace++;
        if (chunk.indexOf("arduino")>= 0)
        arduino++;
      }
    }
    if (peace>64) peace = 64;
    if (love>64 ) love = 64;
    if (arduino>64) arduino = 64;
    peace = peace * 4;
    love = love* 4;
    arduino =arduino * 4;
  }

  catch (Exception ex) {
    ex.printStackTrace();
    System.out.println("ERROR:"+ex.getMessage());
  }
}

        
2 Likes

did I get that right?

2 Likes

Yes, that is how to use code tags.

Sorry, but this seems to be a common problem (I dislike Java, so I’m afraid I can’t help)

//import java.io.*;
import java.io.InputStreamReader;

Processing 4?

Try this:

import processing.serial.*; // I don't see the asterisk in yours

Serial myPort;

String[] com = {"COM2", "COM3", "COM4",
  "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11",
  "COM12", "COM13", "COM14", "COM15", "COM16", "COM17", "COM18",
  "COM19", "COM20", "COM21", "COM22", "COM23", "COM24", "COM25", "COM26"};

void setup() {
// other stuff before or after as appropriate
  connect(1, 24, 200); // start a new tab named connect and use the function that follows
}

void draw() {
// your stuff
}

// as long as your Arduino COM # is less than 25, you're good
// this will auto scan and connect. If it's equal to or over 25, revise the array in defs
// and revise this function, too. 
boolean connect(int portNumber, int numCom, int limit) {
  background (0, 14, 255);
  int tries = 0;
  while (tries < limit) {
    try {
      printArray (Serial.list()[portNumber]);
      myPort = new Serial(this, com[numCom], 115200);
    }
    catch(Exception e) {
      System.err.println("Retrying " + "Port " + com[numCom] + " on COM " + portNumber);
      // numCom -= 1;
      numCom += 1;
      tries++;
      if (numCom < 0) { // prevent ArrayIndexOutOfBoundsException
        numCom = 24;
      } else if (numCom > 24) { // prevent ArrayIndexOutOfBoundsException
        numCom = 0;
      }
      if (tries % 25 == 0) {
        portNumber += 1;
        if (portNumber > 4) {
          portNumber = 0;
        }
      }
      delay(200);
      continue;
    }
    break;
  }
  if (tries < limit) {
    println("Connected in " + str(tries + 1) + " tries.");
    background(0, 255, 0);
    delay(200);
    background(0);
    return true;
  } else {
    System.err.println("Connection failed.");
    return false;
  }
}

// I like serial handler in its own tab, called serial. No need to call it in draw,
// Processing is already looking for it while connected as above
void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n'); // read until newline character
  if (inString != null) {  // if there's something 
    inString = trim(inString); // trim off the terminator
    if (gameStage == 400) {  // in my state machine in void draw(), YMMV
      if (inString.equals("alarm")) { // It's a laser tripwire game to help you visualize
        println("alarm"); // print what we got in Processing console for debugging
        score+=1; // do a thing, again this is for a laser tripwire game
        gameStage = 670; // move to this in main draw() state machine
      }
    }
  } 
}

and in ^ project, my Arduino side code (the relevant parts) looked like:

void setup() {
  Serial.begin(115200);  // match your baud how ever you like as long as they match
}


void loop() {
  if (((digitalRead(sensor1) == 1) || ((digitalRead(sensor2) == 1))) {
     Serial.println("alarm");
  }
}

Credit to the Processing serial connect function goes to someone who I regret I must not have noted because I don't know who it was. It wasn't me in any case, and I sincerely thank the person who shared it online as it works great for connecting some Arduino to Processing without having to modify the code. It just works.