[SOLVED] Unexpected token:processing error in code drawn from an author's code

The following code yields the error “unexpected token: processing”.
What am I missing? It’s a straight copy of an author’s code (from “Arduino Projects Book”) that apparently did not get this error…
Did I not install Processing correctly, or am I missing something in this code:

//***************************************************************************

//**
// Preprocessor Directives
//***

Import processing.serial.*;
Serial myPort;
PImage logo;

//***
// Global Declaration
//***

// Global Constants
final int BAUD_RATE     = 9600; // must be same as the Arduino
final String logoAdress = "http://www.arduino.cc/arduino_logo.png";

// Global variables
int bgcolor = 0; // holds the background color

//***
// Mandatory Routines
//***

void setup() {
    size(1,1);
    surface.setResizable(true);
    //set the color mode to Hue/Saturation/Brightness
    colormode(HSB, 255);
    
    // load the Arduinologo into the PImage instance
    logo = loadImage(logoAddress);
    
    // make the window the same size as the image
    surface.setSize(logo.width, logo.height);
    
    // print a list od available serial ports to the
    // Processing status window
    println("Available serial ports:");
    println(Serial.list());
    
    // Tell the serial object the informationit needs to communicate
    myport = new Serial(this, Serial.list()[0], BAUD_RATE);
}

void draw() {
    if (myPort.available() > 0) {

        // read and print for easy debugging
        bgcolor = myPort.read();
        println(bgcolor);
        
        // draw the background by RGB color (each channel ranges 0-255)
        background(bgcolor, 255, 255);
        
        // draw the Arduino logo
        image(logo, 0, 0);
    }
}

        

Thank you for your time and expertise.

1 Like

BTW, the error occurs on the line:

Import processing.serial.*;

Thanks!

I believe you need to add the serial library using the Contribution manager in the PDE.

Kf

I would love to do that. I just don’t see “Contribution Manager” listed under any of the tabs in Processing IDE.

I see tabs for File, Edit, Sketch, Debug, Tools, and Help. Under each of those I don’t see a Contribution Manager.

Is there another program that does this?

I found the Contribution Manager! Under the pull-down where it says Java on my Processing IDE windows. But there are many libraries. Do I have to install them all?

Contribution Manager is accessed in the PDE under Sketch >> Import Library >> Add library…. I am guessing you need to install the serial library. I might be wrong and the serial library could come with the Processing’s source files. Unfortunately I can’t do any test at the moment as I do not have PDE in my current machine. I can tell you later tonight…

Kf

That would be nice if you would.

I can’t find a library that says Serial, although I have not read through every description of a long list of available libraries. I’ll start reading each one now and see if I see Serial listed in the description.

Serial is 1 of the very few bundled libraries for the PDE’s Java Mode:

I’m in Java mode. I still get the error: unexpected token:processing

PDE Install Folder"/modes/java/libraries/serial/library/ is conspicuously missing from my installation.

That kinda error generally means the compiler failed to parse the source code. :confused:

Most of the time, some ;, or }, etc. might be missing somewhere. :flushed:

We can hit CTRL+T in the PDE in order to auto indent our code. :open_book:

After that, look for any lines w/ a wrong indentation. :eye:

The culprit statement should be there if you spot 1. :open_mouth:

Tried the Control T. It rearranged the code just a bit. I tried recompiling and still get the same error.

I’m going nuclear on this problem… I’m going to save my source code in a file away from the Processing folders, then I’m going to uninstall Processing from my computer and delete all directories associated with it including the installation zip file. I’ll then download it again carefully making sure it’s the correct version for me. Then reinstall it, move the questionable code back to its rightful place and give it another compile and see what happens. I’ve got nothing to lose.

Another technique: :innocent:

  1. Open a 2nd PDE window (CTRL+N).
  2. Copy parts of the code to the 2nd window 1 by 1.
  3. Try to compile the code on the 2nd window for each small copy.
  4. Repeat until u find the offensive code line.

OK, I completely uninstalled Processing and all its files along with installation files. Freshly Rebooted my laptop. Installed an older version of Processing (version 3.3.7) and used the 32 bit version rather than the 64 bit version I had been using. Checked to see if /modes/java/libraries/serial/library/ existed. It did. Created a desktop shortcut. Moved my offending program to the appropriate directory. Opened Processing, loaded the program, clicked the Run button and it errored out where it had been before with the error unexpected token:processing on the line with:

Import processing.serial.*;

Opened a second occurrence of Processing. Copied the first line of the program into it (which happens to be the offending line and got the error; “Syntax error, maybe a missing semicolon?” Obviously not. The semicolon is there. Copied the whole front-end of the program in 2nd Processing window. Still getting syntax error. Copied the entire program line by line. Now it switches back to “unexpected token:processing” error. A line by line copy shows that when the “void draw {” appears with its associated ending bracket “}”, that is when the error changes from a syntax error to “unexpected token:processing”.

This ordeal is quite perplexing. I really have no idea what to do and cannot proceed with doing anything useful until this problem is solved. If I eliminate the offending line then Processing has no idea what to do with the Serial command that follows it. Which means I can’t read serial data at all. I don’t know, maybe there is another way to perform serial reads that doesnt require the offending line of code.

1 Like

All Java reserved keywords are lowercase. :exclamation:
Therefore it’s import, not Import: Processing.org/reference/import.html :sneezing_face:

3 Likes

BINGO!!! You got it! It’s “import”, NOT “Import”

And to think that I copied this from another author’s work. I assumed his code was working code. Bad assumption.

Now the program works beyond that statement and the serial I/O works now. (There are other problems with the code specifically with “colormode”, but I can use normal troubleshooting techniques from here on).

Thanks for spotting that obvious error. Yet, it was SO well hidden. Many thanks to you!

bob

For the record, the “colormode” problem was also a lower/upper case problem… The actual function is “colorMode” with a capital M.

This language is SO picky. Oh well, lesson learned.

Just a warning, most computer languages are case-sensitive, for many reasons. In particular you can program Processing in Java, JavaScript, CoffeeScript, Python, Ruby, or R … and one thing they have in common is that they are ALL case-sensitive languages!

For those case-insensitive languages out there (such as Cobal, Pascal, Fortran, BASIC, Visual Basic) I don’t believe that there are Processing modes available. Although a Processing Fortran mode would be a pretty darn cool art project.

1 Like