So finally i undestood why Arduino has its own software which is different from Processing.

With the Arduino platform, all you do is to write code that will be loaded into the Arduino board.
You tell to the board how to react to signals received.

So what kind of signals can the Arduino board receive?
Even if the Arduino board has a USB interface, to make the life of developers easy, the communications that the board makes, either in reception or transmission, are serail communications. This means that any language or any software that can transmit serial data, could be understood by the Arduino Board.

Considering these new discoveries, (it took me a while to understand) I will create two more categories to diversify code for the Arduino Interface and code for Processing and I will give a practical example on how two make Processing interact with the Arduino board.

So why should you use Processing instead of the Arduino software?
Even if the Arduino software can communicate in serial with the board, beside writing code that should be uploaded to the board itself, it lacks of all the libraries that Processing has, and interaction with the web for example could not be possible. Also, the Processing software can receive serial data sent by the Arduino board and can translate this information into graphical representation.

From now on, I will be carefull to indicate which one is the code for the Arduino software interface, and which is for Processing, and I will give you a practical example on how to make the two interact one with each other.

This example will guide you through the experience of switching on an LED on the Arduino Board using Processing. I know this is quite simple to achive, but it was difficoult for me to understand, so probably it will be of help to other peoples encountering my difficoultis as well.

So we will start with the Arduino Software first. We will write a code to be uploaded into the Arduino board that will put the serial port into listening and that will switch on the LED when it receives the character “A” capital from Processing.
You can find the original code at this address: Link, which will switch on and off an LED withing a time routine, but I have made my modifications so that it will only switch on when it receive the “A” character form Processing.

Arduino Code
/*Arduino Code
* Switch an LED on
* —————–
*
* turns on an LED when receiving an “A” character with any serial software
*
*Code modified by Francesco Saitta
*
* Code originally created 1 June 2005
* copyleft 2005 DojoDave
* http://arduino.berlios.de
*
* based on an orginal by H. Barragan for the Wiring i/o board
*/

We will assing now the pin number of the board that should be affected by the script and we declare the val variable that is the variable modified when the “A” character is received. We must declare this and assign to it a dummy value because otherwise the script will go on error. We also start the Serial port and we assign the BAUD rate. Here is the code, copy it into the Arduino software

int ledPin = 13; // LED connected to digital pin 13
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
}

Now we start the loop. A loop os needed because it will put the board on a listening status, and unless it receives the “A” character, than it will do nothing.

void loop()

{

Here we assing the value read from the serial port to the variable called “var”

val = Serial.read();

and now we start the conditional. As you can easily read, if the value of the “val” variable is “A” than the board will send electricity to the pin declared in the start with the variable “ledPin”

if (val == ‘A’) {

digitalWrite(ledPin, HIGH); // sets the LED on
}

Here I have set another conditional. If the variable “val” equals a lowercase “A” than switch off the LED.

if (val == ‘a’) {

digitalWrite(ledPin, LOW); // sets the LED off
}

And finally we close the loop.

}

Now you are ready to upload it into your Arduino Board. Press the reset button on the board and then Click on the UPLOAD button into your Arduino Software interface. For more information on how to connect your computer to your board and how to upload code into it you may refer to this page on the Arduino website. Is time now to put an LED into your board. So put the longer LED leg into the pin number 13 (you can modify this by changing the ledPin variable in the script, altought I am not sure it will work) and the shorter one into the GND pin. Remember that LED have a polarity so be carefull about this.

So now we will go into the Processing code. Again for this the code was originally found at this address: Link. I have cut out a lot of the script to make it simpler but that is for reference for anyone who would like to go a bit further.

So here is the code. We import the Serial Library first and declare the Serial variable:

// Processing code
// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;

We print out a list of the Serial Ports for debugging

// List all the available serial ports:
println(Serial.list());

and select the right one for us by changing the value on he Serial.list() variable inside the square brackets. Remember that arrays on the processing software start from zero so if your option is the second listed you will have to write 1 and not 2 as you would probably expect. My port set for communication with the Arduino Board is COM3 which comes listed after COM1 and COM2 in the list so I will write 2 inside the suqare brackets.

// Note from Tom Igoe
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[2], 9600);

Finally we send the capital A in ASCI format so is the number 65. You can find a conversion Table at this address:
Ascii table - Characters from 0 to 127
Ascii table - Characters from 128 to 256

// Send a capital A out the serial port
myPort.write(65);

If everything is ok, you should see you LED switch on! If you want to switch it off, you will have to change the code as follows
putting 97 ( the ASCII code for the lowercase “a” character) insted of 65.

// Send a capital A out the serial port
myPort.write(97);

I hope that this may be usefull to anyone, and I am ready to accept suggestion or mdification of the above article to make it more simple to understand or to correct any error committed.

3 Comments

  1. Comment by MikeD on November 16, 2006 10:31 pm

    Hey man — just stumbled across your site while looking for a how-to on getting Processing to talk to my Arduino, and found your write-up to be really helpful. Keep up the good work!

  2. Comment by cicciorama on November 17, 2006 3:50 am

    Ty!

  3. Pingback by rascunho » Seção de dados do blog » links for 2006-11-29 on November 29, 2006 3:19 pm

    […] everybody.is-a-cyb.org » Archive » So finally i undestood why Arduino has its own software which is different from Processing. So finally i undestood why Arduino has its own software which is different from Processing. (tags: everybody.is-a-cyb.org 2006 at_tecp processing arduino howto) […]

Comments RSS TrackBack Identifier URI

Sorry, the comment form is closed at this time.