2 HC-SR04 Ping distance sensor:
8 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
9 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
10 And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
15 #define echoPin 7 // Echo Pin
16 #define trigPin 8 // Trigger Pin
17 #define LEDPin 13 // Onboard LED
19 int maximumRange = 200; // Maximum range needed
20 int minimumRange = 0; // Minimum range needed
21 long duration, distance; // Duration used to calculate distance
25 pinMode(trigPin, OUTPUT);
26 pinMode(echoPin, INPUT);
27 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
31 /* The following trigPin/echoPin cycle is used to determine the
32 distance of the nearest object by bouncing soundwaves off of it. */
33 digitalWrite(trigPin, LOW);
36 digitalWrite(trigPin, HIGH);
37 delayMicroseconds(10);
39 digitalWrite(trigPin, LOW);
40 duration = pulseIn(echoPin, HIGH);
42 //Calculate the distance (in cm) based on the speed of sound.
43 distance = duration/58.2;
45 if (distance >= maximumRange || distance <= minimumRange){
46 /* Send a negative number to computer and Turn LED ON
47 to indicate "out of range" */
49 digitalWrite(LEDPin, HIGH);
52 /* Send the distance to the computer using Serial protocol, and
53 turn LED OFF to indicate successful reading. */
54 Serial.println(distance);
55 digitalWrite(LEDPin, LOW);
58 //Delay 50ms before next reading.
62 // End Arduino, start Processing
63 // Take care of: myPort (for serial comm)
65 ///* The following Processing Sketch was created by ScottC on
66 // the 10 Nov 2012 : http://arduinobasics.blogspot.com/
68 // Inspired by this Processing sketch by Daniel Shiffman:
69 // http://processing.org/learning/basics/sinewave.html
72 //import processing.serial.*;
75 //int numOfShapes = 60; // Number of squares to display on screen
76 //int shapeSpeed = 2; // Speed at which the shapes move to new position
77 // // 2 = Fastest, Larger numbers are slower
80 //Square[] mySquares = new Square[numOfShapes];
81 //int shapeSize, distance;
82 //String comPortString;
85 ///* -----------------------Setup ---------------------------*/
87 // size(displayWidth,displayHeight); //Use entire screen size.
88 // smooth(); // draws all shapes with smooth edges.
90 // /* Calculate the size of the squares and initialise the Squares array */
91 // shapeSize = (width/numOfShapes);
92 // for(int i = 0; i<numOfShapes; i++){
93 // mySquares[i]=new Square(int(shapeSize*i),height-40);
96 // /*Open the serial port for communication with the Arduino
97 // Make sure the COM port is correct - I am using COM port 8 */
98 // myPort = new Serial(this, "/dev/ttyACM2", 9600);
99 // myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
102 ///* ------------------------Draw -----------------------------*/
104 // background(0); //Make the background BLACK
105 // delay(50); //Delay used to refresh screen
106 // drawSquares(); //Draw the pattern of squares
110 ///* ---------------------serialEvent ---------------------------*/
111 //void serialEvent(Serial cPort){
112 // comPortString = cPort.readStringUntil('\n');
113 // if(comPortString != null) {
114 // comPortString=trim(comPortString);
116 // /* Use the distance received by the Arduino to modify the y position
117 // of the first square (others will follow). Should match the
118 // code settings on the Arduino. In this case 200 is the maximum
119 // distance expected. The distance is then mapped to a value
120 // between 1 and the height of your screen */
121 // distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
123 // /*If computer receives a negative number (-1), then the
124 // sensor is reporting an "out of range" error. Convert all
125 // of these to a distance of 0. */
132 ///* ---------------------drawSquares ---------------------------*/
133 //void drawSquares(){
134 // int oldY, newY, targetY, redVal, blueVal;
136 // /* Set the Y position of the 1st square based on
137 // sensor value received */
138 // mySquares[0].setY((height-shapeSize)-distance);
140 // /* Update the position and colour of each of the squares */
141 // for(int i = numOfShapes-1; i>0; i--){
142 // /* Use the previous square's position as a target */
143 // targetY=mySquares[i-1].getY();
144 // oldY=mySquares[i].getY();
146 // if(abs(oldY-targetY)<2){
147 // newY=targetY; //This helps to line them up
149 // //calculate the new position of the square
150 // newY=oldY-((oldY-targetY)/shapeSpeed);
152 // //Set the new position of the square
153 // mySquares[i].setY(newY);
155 // /*Calculate the colour of the square based on its
156 // position on the screen */
157 // blueVal = int(map(newY,0,height,0,255));
158 // redVal = 255-blueVal;
159 // fill(redVal,0,blueVal);
161 // /* Draw the square on the screen */
162 // rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
166 ///* ---------------------sketchFullScreen---------------------------*/
167 //// This puts processing into Full Screen Mode
168 //boolean sketchFullScreen() {
172 ///* ---------------------CLASS: Square ---------------------------*/
174 // int xPosition, yPosition;
176 // Square(int xPos, int yPos){
189 // void setY(int yPos){