Processing – interface on steroids

Everyone who become interested, will, rather sooner than later, stumble on the name Processing. Processing is the software on which Arduino IDE is based. But, it isn’t the only common thing.

At first – what is the Processing?

It’s a programming language (under an open source license) with IDE environment created for the people involved in electronic arts and visual design communities (by the Wikipedia).
More clearly? Just look over, for example Vimeo for tag processing.org. Or processing group on YouTube. Data visualization, multimedia experiments, etc. We are able to see, for example, train traffic in Melbourne:

Ebb and Flow of Melbourne Trains by Flink Labs from Flink Labs on Vimeo.

Exactly, and from where the data comes? Well, it might be from Arduino, whose sensors can provide quite a lot of various data to visualize

Now I will give a simple recipe how to use the Arduino and Processing to build … an oscilloscope :) . Oscilloscope is too much said – visualization of the voltage on a single analog input is better description.

Arduino does one thing – it measures voltage on the analog input and sends data to serial port. Application written in Processing collects this data and displays a chart. From the beginning:

In Arduino IDE we are typing a short sketch:

 
void writeOscilloscope(int value) {
  Serial.print( 0xff, BYTE );                // send init byte
  Serial.print( (value >> 8) & 0xff, BYTE ); // send first part
  Serial.print( value & 0xff, BYTE );        // send second part
}

#define ANALOG_IN 0

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(ANALOG_IN);
  writeOscilloscope(val);
}

Compiling it, loading to Arduino and closing the IDE. Now, we are starting up the Processing and typing the following sketch:

import processing.serial.*;

Serial port;  // Create object from Serial class
int val;      // Data received from the serial port
int[] values;
int i;
void setup() 
{
  size(640, 480);
  // Open the port that the board is connected to and use the same speed (9600 bps)
  port = new Serial(this, Serial.list()[0], 9600);
  values = new int[width];
  smooth();
  i=0;
}

int getY(int val) {
  return (int)(val / 1023.0f * height) - 1;
}

void draw()
{
  while (port.available() >= 3) {
    if (port.read() == 0xff) {
      val = (port.read() << 8) | (port.read());
    }
  }
  i++;
  if (i>=width-1)
    i=0;
  values[i] = val;
  background(0);
  stroke(255);
  for (int x=1; x<width; x++) {
    line(width-x,   height-1-getY(values[x-1]), 
         width-1-x, height-1-getY(values[x]));
  }
}

After launchng it, we can begin to measure :) . How it practically works?

Arduino accelerometer with poorman’s oscilloscope in background from Starter Kit on Vimeo.

A few words of explanation:

  • myself, I’m just starting my adventure with Processing, but it seems to be fascinating. However, I’m aware that my knowledge about Processing is still poor
  • the main idea and code were taken from Accrochages and the displaying code was simplified a bit
  • THIS IS NOT AN OSCILLOSCOPE :) – not these times and frequencies, but it’s still useful tool, sometimes :) , if the requirements isn’t too high

The moving thing in the movie is a prototype of 3-axis acceleration sensor (only reading from one axis is shown) with 3G or 11G measuring range. This is a prototype, however these sensors are already in stock on Nettigo. I haven’t yet finished the documentation (description of the pins and how to connect), though, if someone is interested, the can already buy it :) . Surely, it will speed up creating of the documentation :)