Processing problem

I've copied out the programme for project 14 into processing and it doesn't let me run it but instead shows an error message.
The error message says:
"The size of this sketch could not be determined from your code.
Use only numbers (not variables) for the size() command"

Any help would be greatly appreciated.
Alan

I've copied out the programme for project 14

From the book "Men are from Mars; Women are from Venus"?

Any help would be greatly appreciated.

So would some details, like the code!

It's from the arduino projects book.
This is the code:

import processing.serial.*;
Serial myPort;
PImage logo;
int bgcolor = 0;
void setup(){
colorMode(HSB, 255);
logo = loadImage("http://arduino.cc/logo.png");
size(logo.width, logo.height);
println("Available serial ports:");
myPort =
new Serial(this, Serial.list()[0], 9600);
}
void draw(){
if(myPort.available() > 0){
bgcolor = myPort.read();
println(bgcolor);
}
background(bgcolor, 255, 255);
image(logo, 0, 0);
}

Add to setup (you are free to pick your own size :wink: ):

  size(640, 480);

Or download the image, and use the path to the local image.

PaulS:
Or download the image, and use the path to the local image.

Nope. Gives the same result (under Processing 3).

I get the same error today from the same code.
Then I changed the code like this.
In Arduino IDE I choose this among examples in the File / Examples / 10.StarterKit_BasicKit:
p14_TweakTheArduinoLogo

In this sketch I found the commented out code for Processing IDE.
I did copy of it and paste the code into Processing IDE, then remove the comment /* from the beginning and from the end of the code, like this:

// Processing code for this example

  // Tweak the Arduino Logo

  // by Scott Fitzgerald
  // This example code is in the public domain.

  // import the serial library
  import processing.serial.*;

  // create an instance of the serial library
  Serial myPort;

  // create an instance of PImage
  PImage logo;

  // a variable to hold the background color
  int bgcolor = 0;

  void setup() {
    size(1, 1);
    surface.setResizable(true);
    // set the color mode to Hue/Saturation/Brightness
    colorMode(HSB, 255);

    // load the Arduino logo into the PImage instance
    logo = loadImage("http://www.arduino.cc/arduino_logo.png");

    // make the window the same size as the image
    surface.setSize(logo.width, logo.height);

    // print a list of available serial ports to the Processing status window
    println("Available serial ports:");
    printArray(Serial.list());

    // Tell the serial object the information it needs to communicate with the
    // Arduino. Change Serial.list()[0] to the correct port corresponding to
    // your Arduino board.  The last parameter (e.g. 9600) is the speed of the
    // communication.  It has to correspond to the value passed to
    // Serial.begin() in your Arduino sketch.
    myPort = new Serial(this, Serial.list()[0], 9600);

    // If you know the name of the port used by the Arduino board, you can
    // specify it directly like this.
    // port = new Serial(this, "COM1", 9600);
    //myPort = new Serial(this, "/dev/ttyACM0", 9600);
  }

  void draw() {

    // if there is information in the serial port
    if ( myPort.available() > 0) {
      // read the value and store it in a variable
      bgcolor = myPort.read();
      // print the value to the status window
      println(bgcolor);
    }

    // Draw the background. the variable bgcolor contains the Hue, determined by
    // the value from the serial port
    background(bgcolor, 255, 255);

    // draw the Arduino logo
    image(logo, 0, 0);
  }

I changed the line 55 as one can see above.
Then I start this code in Processing, and get the Arduino Logo Image, but the background is not changed as I turning the Potmeter, not at all.
Instead, it seems to me that that it read the state of the potmeter only once and change the background color of the Arduino Logo just once.

The potmeter has no constant effect on the Arduino Logo Image.
Why?

Why?

Could be lots of reasons. The most likely is that the Arduino isn't connected to the port that is the first one in the list.

PaulS:
Could be lots of reasons. The most likely is that the Arduino isn't connected to the port that is the first one in the list.

We can exclude this.
The Processing IDE won't start this sketch if there is no connection to the Serial port at all.
Secondly, before I start Processing sketh I verify that that the Arduino UNO hardware is connected already to the port. The port is here on GNU/Linux the /dev/ttyACM0 and is used when I started in Processing IDE the sketch.

I realized the following.
When I turn the knob, the Analog Input from the knob is readed through analog A0 port of Arduino.
The values changes in the Arduino Serial Monitor accordinlgy to the knob state.

But when in Processing IDE and I started the sketch - the image appeare out there - the background of the Arduino Logog changes, but very-very slowly.

So it works, but I expect that that the background changes in synchrone with the knob.
But it does not. As I say, the change of the background is very-very slow.

Why?

The piece of the code in the Processing sketch is this:

void draw() {

    // if there is information in the serial port
    if ( myPort.available() > 0) {
      // read the value and store it in a variable
      bgcolor = myPort.read();
      // print the value to the status window
      println(bgcolor);
    }

    // Draw the background. the variable bgcolor contains the Hue, determined by
    // the value from the serial port
    background(bgcolor, 255, 255);

    // draw the Arduino logo
    image(logo, 0, 0);
  }

What does mean the zero value in this line below?

if ( myPort.available() > 0) {

If one changes this value, what should expect?

As I say, the change of the background is very-very slow. Why?

The draw() function, in Processing, unlike the loop() function in Arduino, is NOT called again and again, as fast as possible. It is called just often enough to maintain the default frame rate.

Therefore, you should NEVER read serial data in draw(). Processing does not recommend that, and shows what to do instead.

What does mean the zero value in this line below?

The value returned by the available() method is the number of bytes in the buffer that have not been read. That statement is saying "if the number of bytes that can be read is greater than 0", which means that if there are bytes to be read, read them.

PaulS:
Therefore, you should NEVER read serial data in draw(). Processing does not recommend that, and shows what to do instead.

Really? And where is it shown?

csanyi_pal:
Really? And where is it shown?

On the Processing web site. This is NOT the Processing website or forum.

PaulS:
On the Processing web site. This is NOT the Processing website or forum.

But we use Arduino and Processing in one project, right?
So if you know the answer, why do not show it?
By the way, here is the link to the Serial read() reference:

I can't se there anything but the exact way of using the read() function as we use it in this project of Arduino Starter Kit / project 14 and which does not work as we expected.

But we use Arduino and Processing in one project, right?

You can make a project involving the Arduino sending data to, and/or receiving data from, Processing. But, that does not mean that this is the best place to ask Processing questions.

So if you know the answer, why do not show it?

There are examples in Processing that receive serial data from the Arduino. They all use a serialEvent() method, called when there is any serial data to read, or called when the desired trigger condition is met. Look at the examples.

I can't se there anything but the exact way of using the read() function as we use it in this project of Arduino Starter Kit / project 14 and which does not work as we expected.

Most functions do not document when to call them. You are expected to know, based on what the function does, and how the overall program works, when/where to call them.

It IS possible to call the read() method in draw() as you are doing. But, it is not a good idea, because the Arduino MIGHT be sending data faster than the draw() method can read the data. Since you have not posted your Arduino code, it is impossible to say whether that is the case here.

But, if you use prinrln() in the Arduino code, and bufferUntil() and serialEvent() in Processing, you can read the serial data much faster, and end up using only every other, every third, every fourth, whatever, value received by the serialEvent() method in draw(), making the background color change much more quickly.

Thanks to the Processing Forum, I solve this like this, in the Arduino IDE code:

int oldpotmeter = 0;

void setup() {
  // initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // read the value of A0, divide by 4 and send it as a byte over the
  // serial connection
  int potmeter = analogRead(A0) / 4;

  if ( potmeter != oldpotmeter ) {
      Serial.write(potmeter);
      //Serial.println(potmeter);
      delay(500);
      oldpotmeter = potmeter;
  }
}

It works!

I solve this like this

What did the code look like before?

PaulS:
What did the code look like before?

You can look the thread on Processing Forum here:

The code was this:

/*
  Arduino Starter Kit example
  Project 14 - Tweak the Arduino Logo

  This sketch is written to accompany Project 14 in the Arduino Starter Kit

  Parts required:
  - 10 kilohm potentiometer

  Software required:
  - Processing (3.0 or newer) http://processing.org
  - Active Internet connection

  created 18 Sep 2012
  by Scott Fitzgerald

  http://www.arduino.cc/starterKit

  This example code is part of the public domain.
*/


void setup() {
  // initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // read the value of A0, divide by 4 and send it as a byte over the
  // serial connection
  Serial.write(analogRead(A0) / 4);
  //Serial.println(analogRead(A0) / 4);
  delay(1);
}

The code was this:

So, the change you made was to send the data only if it changed (good idea) and to look for changes 1/500th as often. That last part is NOT going to improve responsiveness.

Did you make any changes to the Processing side? THAT is where the most important changes should have been made.