]> git.piffa.net Git - sketchbook_andrea_bak/blob - hardware/ultrasonic/ultrasonic_distance_programming/ultrasonic_distance_programming.ino
Initial Commit
[sketchbook_andrea_bak] / hardware / ultrasonic / ultrasonic_distance_programming / ultrasonic_distance_programming.ino
1 /*
2  HC-SR04 Ping distance sensor:
3  VCC to arduino 5v 
4  GND to arduino GND
5  Echo to Arduino pin 7 
6  Trig to Arduino pin 8
7  
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
11  on 10 Nov 2012.
12  */
13
14
15 #define echoPin 7 // Echo Pin
16 #define trigPin 8 // Trigger Pin
17 #define LEDPin 13 // Onboard LED
18
19 int maximumRange = 200; // Maximum range needed
20 int minimumRange = 0; // Minimum range needed
21 long duration, distance; // Duration used to calculate distance
22
23 void setup() {
24  Serial.begin (9600);
25  pinMode(trigPin, OUTPUT);
26  pinMode(echoPin, INPUT);
27  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
28 }
29
30 void loop() {
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); 
34  delayMicroseconds(2); 
35
36  digitalWrite(trigPin, HIGH);
37  delayMicroseconds(10); 
38  
39  digitalWrite(trigPin, LOW);
40  duration = pulseIn(echoPin, HIGH);
41  
42  //Calculate the distance (in cm) based on the speed of sound.
43  distance = duration/58.2;
44  
45  if (distance >= maximumRange || distance <= minimumRange){
46  /* Send a negative number to computer and Turn LED ON 
47  to indicate "out of range" */
48  Serial.println("-1");
49  digitalWrite(LEDPin, HIGH); 
50  }
51  else {
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); 
56  }
57  
58  //Delay 50ms before next reading.
59  delay(50);
60 }
61
62 // End Arduino, start Processing
63 // Take care of: myPort (for serial comm)
64 ///*
65 ///* The following Processing Sketch was created by ScottC on
66 // the 10 Nov 2012 : http://arduinobasics.blogspot.com/
67 // 
68 // Inspired by this Processing sketch by Daniel Shiffman:
69 // http://processing.org/learning/basics/sinewave.html
70 // 
71 //*/
72 //import processing.serial.*;
73 //
74 //
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
78 //
79 ////Global Variables 
80 //Square[] mySquares = new Square[numOfShapes];
81 //int shapeSize, distance;
82 //String comPortString;
83 //Serial myPort;
84 //
85 ///* -----------------------Setup ---------------------------*/
86 //void setup(){
87 // size(displayWidth,displayHeight); //Use entire screen size.
88 // smooth(); // draws all shapes with smooth edges.
89 // 
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);
94 // }
95 // 
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
100 //}
101 //
102 ///* ------------------------Draw -----------------------------*/
103 //void draw(){
104 // background(0); //Make the background BLACK
105 // delay(50); //Delay used to refresh screen
106 // drawSquares(); //Draw the pattern of squares
107 //}
108 //
109 //
110 ///* ---------------------serialEvent ---------------------------*/
111 //void serialEvent(Serial cPort){
112 // comPortString = cPort.readStringUntil('\n');
113 // if(comPortString != null) {
114 // comPortString=trim(comPortString);
115 // 
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));
122 // if(distance<0){
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. */
126 // distance = 0;
127 // }
128 // }
129 //}
130 //
131 //
132 ///* ---------------------drawSquares ---------------------------*/
133 //void drawSquares(){
134 // int oldY, newY, targetY, redVal, blueVal;
135 // 
136 // /* Set the Y position of the 1st square based on 
137 // sensor value received */
138 // mySquares[0].setY((height-shapeSize)-distance);
139 // 
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();
145 // 
146 // if(abs(oldY-targetY)<2){
147 // newY=targetY; //This helps to line them up
148 // }else{
149 // //calculate the new position of the square
150 // newY=oldY-((oldY-targetY)/shapeSpeed);
151 // }
152 // //Set the new position of the square
153 // mySquares[i].setY(newY);
154 // 
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);
160 // 
161 // /* Draw the square on the screen */
162 // rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
163 // }
164 //}
165 //
166 ///* ---------------------sketchFullScreen---------------------------*/
167 //// This puts processing into Full Screen Mode
168 //boolean sketchFullScreen() {
169 // return true;
170 //}
171 //
172 ///* ---------------------CLASS: Square ---------------------------*/
173 //class Square{
174 // int xPosition, yPosition;
175 // 
176 // Square(int xPos, int yPos){
177 // xPosition = xPos;
178 // yPosition = yPos;
179 // }
180 // 
181 // int getX(){
182 // return xPosition;
183 // }
184 // 
185 // int getY(){
186 // return yPosition;
187 // }
188 // 
189 // void setY(int yPos){
190 // yPosition = yPos;
191 // }
192 //}
193