HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here:
And modified further by ScottC here:
on 10 Nov 2012.

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin (9600);

//Calculate the distance (in cm) based on the speed of sound. } //Delay 50ms before next reading. distance = duration/58. pinMode(LEDPin.println(distance). delayMicroseconds(2). distance.blogspot. digitalWrite(trigPin. // Use LED indicator (if required) } void loop() { /* The following trigPin/echoPin cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. LOW). delay(50). Larger numbers are slower //Global Variables Square[] mySquares = new Square[numOfShapes]. int shapeSize.2. */ digitalWrite(trigPin. duration = pulseIn(echoPin. LOW). if (distance >= maximumRange || distance <= minimumRange){ /* Send a negative number to computer and Turn LED ON to indicate "out of range" */ Serial. pinMode(echoPin. // Number of squares to display on screen int shapeSpeed = 2. INPUT). HIGH). } else { /* Send the distance to the computer using Serial protocol.*. int numOfShapes = 60.serial.pinMode(trigPin.org/learning/basics/sinewave.html */ import processing.println("-1"). . digitalWrite(LEDPin. LOW). OUTPUT). // Speed at which the shapes move to new position // 2 = Fastest. digitalWrite(trigPin.com/ Inspired by this Processing sketch by Daniel Shiffman: http://processing. HIGH). and turn LED OFF to indicate successful reading. OUTPUT). */ Serial. delayMicroseconds(10). } /* The following Processing Sketch was created by ScottC on the 10 Nov 2012 : http://arduinobasics. digitalWrite(LEDPin. HIGH).

/* Use the distance received by the Arduino to modify the y position of the first square (others will follow). /* -----------------------Setup ---------------------------*/ void setup(){ size(displayWidth. In this case 200 is the maximum distance expected.bufferUntil('\n'). Serial myPort. targetY. . "COM8". //Delay used to refresh screen drawSquares().1. // draws all shapes with smooth edges.height)). i<numOfShapes.200. //Use entire screen size. then the sensor is reporting an "out of range" error. /* Calculate the size of the squares and initialise the Squares array */ shapeSize = (width/numOfShapes). 9600). newY. */ distance = 0.readStringUntil('\n'). // Trigger a SerialEvent on new line } /* ------------------------Draw -----------------------------*/ void draw(){ background(0). //Make the background BLACK delay(50).1. redVal. for(int i = 0.height-40). if(distance<0){ /*If computer receives a negative number (-1).I am using COM port 8 */ myPort = new Serial(this. } } } /* ---------------------drawSquares ---------------------------*/ void drawSquares(){ int oldY. blueVal. The distance is then mapped to a value between 1 and the height of your screen */ distance = int(map(Integer. myPort. if(comPortString != null) { comPortString=trim(comPortString). } /*Open the serial port for communication with the Arduino Make sure the COM port is correct . Convert all of these to a distance of 0. smooth().parseInt(comPortString).String comPortString.displayHeight). Should match the code settings on the Arduino. //Draw the pattern of squares } /* ---------------------serialEvent ---------------------------*/ void serialEvent(Serial cPort){ comPortString = cPort. i++){ mySquares[i]=new Square(int(shapeSize*i).

255)).getY(). /* Draw the square on the screen */ rect(mySquares[i].blueVal). } } /* ---------------------sketchFullScreen---------------------------*/ // This puts processing into Full Screen Mode boolean sketchFullScreen() { return true.0. Square(int xPos. int yPos){ xPosition = xPos. i>0.setY(newY). /*Calculate the colour of the square based on its position on the screen */ blueVal = int(map(newY. /* Update the position and colour of each of the squares */ for(int i = numOfShapes-1. i--){ /* Use the previous square's position as a target */ targetY=mySquares[i-1]. } int getY(){ return yPosition.0.shapeSize). . yPosition.setY((height-shapeSize)-distance). oldY=mySquares[i]. } //Set the new position of the square mySquares[i].0.shapeSize. //This helps to line them up }else{ //calculate the new position of the square newY=oldY-((oldY-targetY)/shapeSpeed).getX().getY(). } int getX(){ return xPosition. if(abs(oldY-targetY)<2){ newY=targetY. } /* ---------------------CLASS: Square ---------------------------*/ class Square{ int xPosition. fill(redVal.getY(). redVal = 255-blueVal./* Set the Y position of the 1st square based on sensor value received */ mySquares[0]. } void setY(int yPos){ yPosition = yPos. mySquares[i]. yPosition = yPos.height.

} } .