Can you help me debug my program syntax.

Hi, I am learning how to program my Arduino chip using the book "Making things Talk". I am stumped on one of the programs it has made me write. I can not seem to debug it. There must be a syntax error but I cant figure it out, could someone please help me see what I have done wrong.

Here is the code:

When I try to compile it it gives me this error message.
"error: variable or field 'serialEvent' declared void In function 'void setup()':
At global scope:"


/*
Serial String Reader
Language: Processing

reads in a string of characters from a serial port
until it gets a linefeed (ASCII 10).
The splits the string into sections seperated by commas.
Then converts the sections to ints, and prints them out.
*/

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

int linefeed = 10; //Linefeed in ASCII
Serial myPort; //The serial port

void setup() {
//List all the available serial ports
println(Serial.list());

//I know that the first port in the serial list on my mac is always my Arduino module,
//so I open Serial.list () [0]. Change 0 to the apropriate number of the serial port
//that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[3],9600);

//read bytes into a buffer until you get a linefeed (ASCII 10);
myPort.bufferUntil(linefeed);
}

void draw() {
// twiddle your thumbs
}

// serialEvent method is run automatically by the Processing sketch
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort){
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);

// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);

//split the string at the commas
//and convert the sections into integers:
int sensors[] = int(split(myString, ','));
//print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ":" + sensors[sensorNum] + "\t");
}
//add a linefeed after all the sensor values are printed:
println();
}
}

You're trying to compile a Processing sketch with your Arduino IDE.

/*
Serial String Reader
Language: Processing

Sorry I am very new to this, so can you explain what you mean.

Basically, you are using the wrong program to compile the code...

the "processing" language is made to be used with the "processing" IDE - the development environment, or program associated with taht language...

thus, for that code, you need this:

http://processing.org/ :wink:

Ok I think I see what you mean.
The first program I ran was made to run on the Arduino processor, using the Arduino program to compile and upload it. Correct ?
The second program is made to run on my computer using the Processing app, and is suppose to interpret the output from the first program (the output from the Arduino) correct ?

Can you follow along with me a little more here, as I am not getting the results that the book says I should be getting.

The first program this book ("Making Things Talk") asked me to write is code made to be uploaded to the arduino and I ran it and it did give me similar readout as what was expected. (So that is good)


/*
Sensor Reader
Lamguage: Wiring/Arduino

Reads two analog inputs and two digital inputs and outputs their values.

Connections:
analog sensors on analog input pins 0 and 1
switches on digital i/o pins 2 and 3

*/

int leftSensor = 0; //analog input for the left arm
int rightSensor = 1; //analog input fot the right arm
int resetButton = 2; //digital input for the reset button
int serveButton = 3; //digital input for the serve button

int leftValue = 0; //reading from the left arm
int rightValue = 0; //reading from the right arm
int reset = 0; //reading from the reset button
int serve = 0; //reading form the serve button

void setup() {
//configure the serial connection:
Serial.begin(9600);
//configure the digital inputs:
pinMode(resetButton, INPUT);
pinMode(serveButton, INPUT);
}

void loop(){
//read the analog sensor:
leftValue = analogRead(leftSensor);
rightValue = analogRead(rightSensor);

//read the digital sensors:
reset = digitalRead(resetButton);
serve = digitalRead(serveButton);

//print the results:
Serial.print(leftValue, DEC);
Serial.print(",");
Serial.print(rightValue, DEC);
Serial.print(",");
Serial.print(reset, DEC);
Serial.print(",");
//"print the last sensor value with a printIn() so that
//each set of four readings prints on a line by itselft:
Serial.println(serve, DEC);
}

/*
// print the results:
Serial.print(leftValue, BYTE);
Serial.print(44, BYTE);
Serial.print(rightValue, BYTE);
Serial.print(44, BYTE);
Serial.print(reset,BYTE);
Serial.print(44, BYTE);
Serial.print(serve, BYTE);
Serial.print(13, BYTE);
Serial.print(10, BYTE);
}
*/


I ran this arduino program and it appears to run as stated in the book.

Next it asks me to write code in Processing language: (Which is what I embeded in my first post. )

Now the book says.
"Make sure that you've shut down the Arduino application so that it releases the serial port, then run this Processing application. (the one in my first post). You should see a list of the sensor values in the message window like this:

Sensor 0: 482 Sensor 1: 488 Sensor 2: 1 Sensor 3: 0
Sensor 0: 482 Sensor 1: 488 Sensor 2: 1 Sensor 3: 0

Here is the problem;
I don't see any values appear in the sensor window.
A window does pop up when I run this program in Processing. Is that the message window where I am suppose to see a read out ? I am not seeing any read out as described.

Did you change the Port Number to match your port?

According to my Arduino application my arduino processor is connected to COM3 .
If I change the line in my Proccessing code from;
myPort = new Serial(this, Serial.list()[0],9600);
to;
myPort = new Serial(this, Serial.list()[3],9600);

I get the following error:
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 3
at sketch_jun14a.setup(sketch_jun14a.java:41)
at processing.core.PApplet.handleDraw(PApplet.java:1403)
at processing.core.PApplet.run(PApplet.java:1328)
at java.lang.Thread.run(Thread.java:619)

Am I correct in changing 0 to 3, which represents COM3 ?
If I leave it at 0 I get a window pop up but with no output. If I change it to 3 as I think it should be COM3 I get the above mentioned error, and no window pops up.

keep changing the number 0,1,2,3,4, etc. and see what happens?

Horray !!! I changed it to 1 and it worked. I'm getting the correct results.
Surely if my computer and the Arduino app tells me my arduino is connected to COM3 then that is the port I should be getting the read out from !?

Thank you all for your help . :slight_smile:

Not necessarily. It lists them in order of connected items. You may well have Comm Ports 1 and 2, but if nothing is connected to them then Comm Port 3 will appear is 0 in the list. In your case you must have something else in one of the other ports and therefore COM3 is 2nd in the list and hence number 1 (0 is first).

:slight_smile: