From Arduino to Processing

In this tutorial we see the differences between using TinkerKit! from Arduino or Processing.
We assume that you already followed the previous tutorial; you should have Arduino and TinkerKit libraries installed on Processing and Firmata running on your Arduino.

Let’s get started: we recommend to start from an example, we created the “empty” example so that you don’t have to write the required minimum instructions every time. Open the “empty” example from File->Examples->Contributed Libraries->TinkerKit->Empty

Empty Example

The empty example starts with importing all the required libraries:

import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;

Then we declare the Arduino object. This is part of the Arduino library, we need to tell Processing that we are using an Arduino.

Arduino arduino;

In the setup we initialize the arduino object. This is the part where we say on which port our board is connected, exactly like on the Arduino software. The difference is that here we select it using the code. You can see all the available ports in the console(the black part at the bottom of the Processing window), after running the code. Once you’ve found the right one, change the corresponding number inside the square brackets. If you don’t have other stuff connected to your USB port, it should be 0 so you’re fine with the default settings.

println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);

After this, you’re ready to write your own TinkerKit code straight from the Processing IDE

Get started

The code is not very different from the one you’re used to write in your Arduino IDE. Let’s start from printing out the values of a potentiometer. First we declare it into the globals, before the setup:

TKPotentiometer pot;

Then in the setup we initialize the pot object as a new TKPotentiometer. All the TinkerKit! objects, when initialized, require the arduino object and the ports that they are using.

pot = new TKPotentiometer(arduino, TK.I0);

Note how it’s not enough to write I0, but we have to write TK.I0. All the ports and constants (usually they are the ones in CAPS), in Processing they require the TK. prefix.
From now on, in the draw you can write exactly what you would write in the loop of the Arduino. Lets’ print the potentiometer values for example:

int val = pot.read();
println(val);
delay(20);

You should see the values in the console of the software. Full code:

import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;

Arduino arduino;

TKPotentiometer pot;

void setup() {  

  size(256, 256);

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);    

  pot = new TKPotentiometer(arduino, TK.I0);
}

void draw() {  
  int val = pot.read();
  println(val);
  delay(20);
}

  1. August 8, 2013

    richard jasmin

    this is in python. Arduino is in (micro) C++.