Ping to processing

Hey I am trying to use arduino ping sensor so a user can control the movement of a bubble in processing as part of a project. I am trying to have the users movement measured by the ping and have processing pick up the distance to control the bubble.I have the code from a tutorial site modified slightly to shrink and grow a ball according to a users distance as a sample to try and get to two talking to each other but they are not communicating for some reason. The normal arduino Ping example is working fine is just the processing side of things that just won't cooperate - keep getting distance: 0.

import processing.serial.*;

  //DisplayItems di;  // backgroundcolor, grid, etc. are controlled in the DisplayItems object
  // width and height should be set here
  int xWidth = 600;
  int yHeight = 600;

  // set framerate
  int fr = 24;
  // set up the display items you want by choosing true
  boolean bck = true;
  //boolean grid = true;
  //boolean g_vert = true;
  //boolean g_horiz = true;
  //boolean g_values = true;
  boolean output = true;
  // these variables are for the serial port connection object
  Serial port;
  String portname = "COM4";  // find the name of your serial port in your system setup!
  int baudrate = 9600;  //  set baudrate here
  int value = 0;  // variables used to store value from serial port
  String buf=""; // String buffer to store serial values
  int value1 =0;  // value1 is the read value

// setup initializes displayItems and serial port objects
void setup(){
      size(xWidth, yHeight);

    //  di = new DisplayItems();

      port = new Serial(this, portname, baudrate);

// the serial event function takes the value of the event and store it in the corresponding variable
void serialEvent(int serial){

    if(serial!=10) {        
          buf += char(serial);          
          } else {
          //extract the value from the string 'buf'
          buf = buf.substring(1,buf.length());
          //cast the value to an integer
          value1 = int(buf);

void createCircle(){

   //Adding constraints to keep the circle within the framesize
   if(value1 > width){
       value1 = width;
     //Draw the circle
     ellipse(width/4, height/4, value1, value1);      

// draw listens to serial port, draw 
void draw(){
  while(port.available() > 0){
        value =;

   // di.drawBack();

    fill(5, 60, 255);
   // di.drawItems();  
    if(output) println("Distance:" +value1);

I've tried the frimata and arduino examples but had no luck. Any help would be much appriciated.

How are you sending values from Arduino?

You could try a simple test:

void setup()

void loop()
    for(int i=0; i < 10; i++){
      Serial.println(i * 10); // send 0 to 100 in steps of 10

I've been using the normal ping sketch to send the serial info from arduino based on a tutorial I found here I have got some of the other examples working - button etc - and talking to each other but the ultrasound one just won't communicate for some reason. I'll try that simple sketch mem and see if i can get it working and I'll post up my results.


Hey mem tried that simple example and i tried the graph example from the website but neither are working in processing. I have no bother getting arduino to output the serial info in a variety of formats, its just processing will not pick it up and use the information. any ideas?

I don't think your SerialEvent in processing is actually reading the character, try something like:

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte =;

hey mem thanks for all your help, finally got the simple graph sketch to ouput the serial info from the simple ping example on a mac and then on my old laptop. I had to change the serial.print to just cm (took out the "inches", inches and "cm") so i was just getting one reading coming through. What i am trying to do now is to use this info to move a circle on the x axis starting from a default position in the center. Any ideas how to convert the distance to control of the object?

Many thanks again

There are Processing examples that do what you want but I can't remember where I saw them. You may get a quicker response asking in the Processing forums.

good luck!

Have been looking for processing examples but can't seem to find the right code for taking in serial info and controlling objects. I've put up 2 messages on processing forums but they don't seem as helpful as the people here. will keep on lookin anyway. Even if i could just move a shape of some sort based on the distance to the sensor i'd be happy. This is all for my final year project in interactive multimedia. I'm fairly new to all this processing/arduino stuff, its been a steep learning curve since january. cheers for all the help