Help needed communications programs Arduino

Can someone help me find out why I have a problem compiling the code below? You can find the item at the link below. When I compile it I get a "serialEvent" problem in the code. If someone can help it would be appreciated. I have an ATMEGA88 and using the Arduino12 program.

/**

  • Serial Call-Response
  • by Tom Igoe.
  • Sends a byte out the serial port, and reads 3 bytes in.
  • Sets foregound color, xpos, and ypos of a circle onstage
  • using the values returned from the serial port.
  • Thanks to Daniel Shiffman and Greg Shakar for the improvements.
  • Note: This sketch assumes that the device on the other end of the serial
  • port is going to send a single byte of value 65 (ASCII A) on startup.
  • The sketch waits for that byte, then sends an ASCII A whenever
  • it wants more data.

*This example code is in the public domain
*/

import processing.serial.*;

int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller

void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn

// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;

// Print a list of the serial ports, for debugging purposes:
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}

void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;

// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
fgcolor = serialInArray[2];

// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + fgcolor);

// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}

Thanks....

You are having trouble compiling this using (a very outdated) Arduino IDE because this is a Processing application, not an Arduino application.

So are you saying if I upgrade to the new Ardino I can still not compile it. If so is there code similar to this that is available?

Thanks.

This is code that runs on the PC to send data to the Arduino. There is separate code that runs on the Arduino to read the data.

You need to compile this code with Processing, and compile (and upload) the other code (that you didn't show) with the Arduino software.

codOK, Here is the code. The first section compiles and it is the reciever section I am trying to compile. Can you help and explain?

/*
Serial Call and Response
Language: Wiring/Arduino

This program sends an ASCII A (byte of value 65) on startup
and repeats that until it gets some data in.
Then it waits for a byte in the serial port, and
sends three sensor values whenever it gets a byte in.

Thanks to Greg Shakar and Scott Fitzgerald for the improvements

The circuit:

  • potentiometers attached to analog inputs 0 and 1
  • pushbutton attached to digital I/O 2

Created 26 Sept. 2005
by Tom Igoe
Modified 14 April 2009
by Tom Igoe and Scott Fitzgerald
*/

int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}

void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.print(firstSensor, BYTE);
Serial.print(secondSensor, BYTE);
Serial.print(thirdSensor, BYTE);
}
}

void establishContact() {
while (Serial.available() <= 0) {
Serial.print(‘A’, BYTE); // send a capital A
delay(300);
}
}

/*
Processing sketch to run with this example:

import processing.serial.*;

int bgcolor; // Background color
int fgcolor; // Fill color
Serial myPort; // The serial port
int serialInArray = new int[3]; // Where we’ll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we’ve heard from the microcontroller

void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn

// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;

// Print a list of the serial ports, for debugging purposes:
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}

void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it’s an A,
// clear the serial buffer and note that you’ve
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == ‘A’) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you’ve had first contact from the microcontroller
myPort.write(‘A’); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;

// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos = serialInArray[1];
fgcolor = serialInArray[2];

// print the values (for debugging purposes only):
println(xpos + “\t” + ypos + “\t” + fgcolor);

// Send a capital A to request new sensor readings:
myPort.write(‘A’);
// Reset serialCount:
serialCount = 0;
}
}
}
*/

/*
Max/MSP version 5 patch to run with this example:

{
“boxes” : [ {
“box” : {
“maxclass” : “message”,
“text” : “65”,
“patching_rect” : [ 339.0, 466.0, 32.5, 18.0 ],
“numoutlets” : 1,
“fontsize” : 12.0,
“outlettype” : [ “” ],
“id” : “obj-9”,
“fontname” : “Arial”,
“numinlets” : 2
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “sel 1”,
“patching_rect” : [ 339.0, 437.0, 36.0, 20.0 ],
“numoutlets” : 2,
“fontsize” : 12.0,
“outlettype” : [ “bang”, “” ],
“id” : “obj-6”,
“fontname” : “Arial”,
“numinlets” : 2
}

}
, {
“box” : {
“maxclass” : “comment”,
“text” : “Serial Call-Response \n\nSends a byte out the serial port, and reads 3 bytes in. Sets foregound color, xpos, and ypos of a circle using the values returned from the serial port. \n\nNote: This patch assumes that the device on the other end of the serial port is going to send a single byte of value 65 (ASCII A) on startup. The sketch waits for that byte, then sends an ASCII A whenever it wants more data. \n\ncreated 14 Apr 2009\nby Scott Fitzgerald and Tom Igoe”,
“linecount” : 11,
“patching_rect” : [ 404.0, 52.0, 464.0, 158.0 ],
“numoutlets” : 0,
“fontsize” : 12.0,
“id” : “obj-5”,
“fontname” : “Arial”,
“numinlets” : 1
}

}
, {
“box” : {
“maxclass” : “comment”,
“text” : “trigger (or [t]) forces right-left conventions. All the drawing and processing will happen before Max requests new values. When this trigger fires, it sends an ASCII A to ask Arduino for new values.”,
“linecount” : 3,
“patching_rect” : [ 239.0, 505.0, 425.0, 48.0 ],
“numoutlets” : 0,
“fontsize” : 12.0,
“id” : “obj-65”,
“fontname” : “Arial”,
“numinlets” : 1
}

}
, {
“box” : {
“maxclass” : “comment”,
“text” : “reinitializes the gates when turned on and off”,
“linecount” : 2,
“patching_rect” : [ 170.0, 370.0, 135.0, 34.0 ],
“numoutlets” : 0,
“fontsize” : 12.0,
“id” : “obj-64”,
“fontname” : “Arial”,
“numinlets” : 1
}

}
, {
“box” : {
“maxclass” : “comment”,
“text” : “checks for the ascii value of “A” to begin cominucation. After initial communication is made, this block shuts down.”,
“linecount” : 3,
“patching_rect” : [ 460.0, 355.0, 233.0, 48.0 ],
“numoutlets” : 0,
“fontsize” : 12.0,
“id” : “obj-63”,
“fontname” : “Arial”,
“numinlets” : 1
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “p “draw the circle””,
“patching_rect” : [ 217.0, 645.0, 269.0, 19.0 ],
“numoutlets” : 1,
“fontsize” : 10.0,
“outlettype” : [ “” ],
“id” : “obj-62”,
“fontname” : “Verdana”,
“numinlets” : 3,
“patcher” : {
“fileversion” : 1,
“rect” : [ 54.0, 94.0, 640.0, 480.0 ],
“bglocked” : 0,
“defrect” : [ 54.0, 94.0, 640.0, 480.0 ],
“openrect” : [ 0.0, 0.0, 0.0, 0.0 ],
“openinpresentation” : 0,
“default_fontsize” : 10.0,
“default_fontface” : 0,
“default_fontname” : “Verdana”,
“gridonopen” : 0,
“gridsize” : [ 25.0, 25.0 ],
“gridsnaponopen” : 0,
“toolbarvisible” : 1,
“boxanimatetime” : 200,
“imprint” : 0,
“boxes” : [ {
“box” : {
“maxclass” : “message”,
“text” : “frgb 255 255 255”,
“patching_rect” : [ 375.0, 150.0, 98.0, 18.0 ],
“numoutlets” : 1,
“fontsize” : 11.595187,
“outlettype” : [ “” ],
“id” : “obj-47”,
“fontname” : “Arial”,
“numinlets” : 2
}

}
, {
“box” : {
“maxclass” : “message”,
“text” : “frgb 0 0 0”,
“patching_rect” : [ 275.0, 125.0, 59.0, 18.0 ],
“numoutlets” : 1,
“fontsize” : 11.595187,
“outlettype” : [ “” ],
“id” : “obj-46”,
“fontname” : “Arial”,
“numinlets” : 2
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “sel 255 0”,
“patching_rect” : [ 300.0, 100.0, 66.0, 21.0 ],
“numoutlets” : 3,
“fontsize” : 12.0,
“outlettype” : [ “bang”, “bang”, “” ],
“id” : “obj-45”,
“fontname” : “Verdana”,
“numinlets” : 1
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “pack 0 0 0 0”,
“patching_rect” : [ 50.0, 125.0, 180.0, 21.0 ],
“numoutlets” : 1,
“fontsize” : 12.0,
“outlettype” : [ “” ],
“id” : “obj-43”,
“fontname” : “Verdana”,
“numinlets” : 4
}

}
, {
“box” : {
“maxclass” : “newobj”,
“text” : “+ 10”,
“patching_rect” : [ 200.0, 100.0, 40.0, 21.0 ],
“numoutlets” : 1,
“fontsize” : 12.0,
“outlettype” : [ "int

There is code here for 3 different applications. The first part of the code is for the Arduino application to compile and upload to the Arduino.

The second part of the code is for the Processing application to compile and execute. You need to install Processing to be able to use it. http://processing.org/

The third part of the code is for Max/MSP. You're on your own with that code.