Tutorial

At this page there’s this tutorial:

Now, I don’t understand why there are two code

Code

int outputPin = 13;
int val;

void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
}

void loop()
{
if (Serial.available()) {
val = Serial.read();
if (val == ‘H’) {
digitalWrite(outputPin, HIGH);
}
if (val == ‘L’) {
digitalWrite(outputPin, LOW);
}
}
}

AND

Processing Code

// mouseover serial
// by BARRAGAN http://people.interaction-ivrea.it/h.barragan

// Demonstrates how to send data to the Arduino I/O board, in order to
// turn ON a light if the mouse is over a rectangle and turn it off
// if the mouse is not.

// created 13 May 2004

import processing.serial.*;

Serial port;

void setup()
{
size(200, 200);
noStroke();
frameRate(10);

// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());

// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}

// function to test if mouse is over square
boolean mouseOverRect()
{
return ((mouseX >= 50)&&(mouseX <= 150)&&(mouseY >= 50)&(mouseY <= 150));
}

void draw()
{
background(#222222);
if(mouseOverRect()) // if mouse is over square
{
fill(#BBBBB0); // change color
port.write(‘H’); // send an ‘H’ to indicate mouse is over square
} else {
fill(#666660); // change color
port.write(‘L’); // send an ‘L’ otherwise
}
rect(50, 50, 100, 100); // draw square
}

Now, Have I to upload which code to my arduino? Or what do I do?

Jack,

The top, C+±derived part runs on the Arduino. The bottom (“Processing”) Java-derived stuff runs on the host computer. In this example, the host program reacts to mouse activity and sends simple serial messages via port.write() to the Arduino code. On the Arduino side, the Serial.read() function accepts these commands and triggers an output pin (presumably attached to an LED) accordingly.

Mikal

ok, so the top code has to upload to my arduino via arduino IDE, right?

and the bottom code has to run on my computer via processing IDE, right?

2 quest:

  • Can I use ONLY the top code and send “H” or “L” by keyboard, via serial monitor?

  • The bottom code has to be compiled with processing IDE? I have mobile processing ide, but the bottom code does not work!

Thanks

Yes, you can use just the Arduino code and the Arduino serial monitor.

The Processing code probably doesn't work in Mobile Processing because it uses the serial port. If you have problems running it in regular Processing, let us know.

Ok, I'll try with Processing.

One question:

I have to interface a j2me application (not a mobile processing sketch) with arduino bt, but I can't. Any advice?

Thanks