Processing research and simple projects

Audio Rotator

I wrote this code for a 2nd year group who wanted to have a physical device that would play and fade different audio dependent on the rotational position of an object. I wrote the code very quickly and was impressed by the speed and ease of use of processing and its effectiveness combined with the ess audio library. Feel free to have a look at the code and use it in your projects, you will need the ess library and a rotary sensor and arduino app. To view the code click here

Audio rate controller

I wrote this sketch for a workshop I was running. It uses the video library which itself uses quicktime java. Quicktime has a really good ability to change the rate of audio, so that you can speed up or slow down sound dynamically. I am sure that there must be another way to do this in another sound library but this works really well. You need to have a quictime file with audio in the data folder of your sketch, to view code click here

Compass Reader

I wrote this sketch to help me visualise the output from a compass sensor attached to an arduino. The code moves a dot to the position of the reading. It is useful code for using the matrix commands in processing and the rotate and translate commands. To view code click here

Linking processing to arduino or other Serial devices

Here is the processing code to attach to a Serial port and read data from it. The code changes the size of a ball dependent on the number given. This code will read the output from the arduino or other serial source and read it in until there is a line break and then it will try to turn it into a number which can be used however you like.

/**
Code for reading in data from one sensor on an arduino
Pete Rogers 19/12/2008 12.15
**/

import processing.serial.*;

int lf = 10;                  // Linefeed in ASCII
int myWidth = 1024;           // stage width
int myHeight = 1024;          //stage height
String sensorString = null;   //string  holding raw data from arduino
int sensorInt = 0;            //number from sensor
Serial myPort;                // The serial port 

void setup() {
  size(myWidth, myHeight);    //set up the stage window   
  frameRate(30);              //set frame rate playback
  smooth();                   //make the visual quality high 
//lines below open and configure the serial port
  println(Serial.list()); 
  myPort = new Serial(this, Serial.list()[1], 9600); 
  myPort.clear();            
  sensorString = myPort.readStringUntil(lf); 
  sensorString = null;                       
}

void draw() {
      background(0); //repaints background
      ellipse(myWidth/2,myHeight/2, sensorInt, sensorInt); //draws circle
}

//serialEvent function is triggered whenever there is data from the arduino
void serialEvent(Serial myPort){
       sensorString = myPort.readStringUntil(lf);
      if ( sensorString != null) {
             sensorString = trim( sensorString);
            sensorInt = int( sensorString);
    }
}
The code below is the Arduino code to use with the above code. It should be possible to read from multiple sensors, you just need to create some sort of ID (e.g. a letter before the number to identify which number it is).

int sensorPin = 0;
int val = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(sensorPin);
sendData(val);
delay(10);
}
void sendData(int data){
Serial.println(data);
}