Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
DC motor (generic)
Arduino Nano R3
Apps and platforms
Arduino IDE
Project description
Code
arduino code
arduino
upload this sketch into arduino
1#include <Servo.h> 2 3/* 4code for arduino bord ultrasonic radar system. 5for more arduino projects ---> teamdiy.in 6*/ 7 8Servo leftRightServo; // set a variable to map the servo 9int leftRightPos = 0; // set a variable to store the servo position 10const int numReadings = 10; // set a variable for the number of readings to take 11int index = 0; // the index of the current reading 12int total = 0; // the total of all readings 13int average = 0; // the average 14int echoPin = 6; // the SRF05's echo pin 15int initPin = 7; // the SRF05's init pin 16unsigned long pulseTime = 0; // variable for reading the pulse 17unsigned long distance = 0; // variable for storing distance 18 19/* setup the pins, servo and serial port */ 20void setup() { 21 leftRightServo.attach(10); 22 // make the init pin an output: 23 pinMode(initPin, OUTPUT); 24 // make the echo pin an input: 25 pinMode(echoPin, INPUT); 26 // initialize the serial port: 27 Serial.begin(9600); 28} 29 30/* begin rotating the servo and getting sensor values */ 31void loop() { 32 for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right. 33 leftRightServo.write(leftRightPos); 34 for (index = 0; index<=numReadings;index++) { // take x number of readings from the sensor and average them 35 digitalWrite(initPin, LOW); 36 delayMicroseconds(50); 37 digitalWrite(initPin, HIGH); // send signal 38 delayMicroseconds(50); // wait 50 microseconds for it to return 39 digitalWrite(initPin, LOW); // close signal 40 pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return 41 distance = pulseTime/58; // convert to centimetres 42 total = total + distance; // update total 43 delay(10); 44 } 45 average = total/numReadings; // create average reading 46 47 if (index >= numReadings) { // reset the counts when at the last item of the array 48 index = 0; 49 total = 0; 50 } 51 Serial.print("X"); // print leading X to mark the following value as degrees 52 Serial.print(leftRightPos); // current servo position 53 Serial.print("V"); // preceeding character to separate values 54 Serial.println(average); // average of sensor readings 55 } 56 /* 57 start going right to left after we got to 180 degrees 58 same code as above 59 */ 60 for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left 61 leftRightServo.write(leftRightPos); 62 for (index = 0; index<=numReadings;index++) { 63 digitalWrite(initPin, LOW); 64 delayMicroseconds(50); 65 digitalWrite(initPin, HIGH); 66 delayMicroseconds(50); 67 digitalWrite(initPin, LOW); 68 pulseTime = pulseIn(echoPin, HIGH); 69 distance = pulseTime/58; 70 total = total + distance; 71 delay(10); 72 } 73 average = total/numReadings; 74 if (index >= numReadings) { 75 index = 0; 76 total = 0; 77 } 78 Serial.print("X"); 79 Serial.print(leftRightPos); 80 Serial.print("V"); 81 Serial.println(average); 82 } 83} 84
processing sketch
processing
run this sketch in processing
1/* 2Radar Screen Visualisation for HC-SR04 3Maps out an area of what the HC-SR04 sees from a top down view. 4Takes and displays 2 readings, one left to right and one right to left. 5Displays an average of the 2 readings 6Displays motion alert if there is a large difference between the 2 values. 7for more projects ----> teamdiy.in 8*/ 9import processing.serial.*; // import serial library 10Serial arduinoport; // declare a serial port 11float x, y; // variable to store x and y co-ordinates for vertices 12int radius = 350; // set the radius of objects 13int w = 300; // set an arbitary width value 14int degree = 0; // servo position in degrees 15int value = 0; // value from sensor 16int motion = 0; // value to store which way the servo is panning 17int[] newValue = new int[181]; // create an array to store each new sensor value for each servo position 18int[] oldValue = new int[181]; // create an array to store the previous values. 19PFont myFont; // setup fonts in Processing 20int radarDist = 0; // set value to configure Radar distance labels 21int firstRun = 0; // value to ignore triggering motion on the first 2 servo sweeps 22/* create background and serial buffer */ 23void setup(){ 24// setup the background size, colour and font. 25size(1204, 650); 26background (0); // 0 = black 27myFont = createFont("verdana", 12); 28textFont(myFont); 29// setup the serial port and buffer 30arduinoport = new Serial(this, "COM7", 9600); 31} 32 33/* draw the screen */ 34void draw(){ 35fill(0); // set the following shapes to be black 36noStroke(); // set the following shapes to have no outline 37ellipse(radius, radius, 750, 750); // draw a circle with a width/ height = 750 with its center position (x and y) set by the radius 38rectMode(CENTER); // set the following rectangle to be drawn around its center 39rect(350,402,800,100); // draw rectangle (x, y, width, height) 40if (degree >= 179) { // if at the far right then set motion = 1/ true we're about to go right to left 41motion = 1; // this changes the animation to run right to left 42} 43if (degree <= 1) { // if servo at 0 degrees then we're about to go left to right 44motion = 0; // this sets the animation to run left to right 45} 46/* setup the radar sweep */ 47/* 48We use trigonmetry to create points around a circle. 49So the radius plus the cosine of the servo position converted to radians 50Since radians 0 start at 90 degrees we add 180 to make it start from the left 51Adding +1 (i) each time through the loops to move 1 degree matching the one degree of servo movement 52cos is for the x left to right value and sin calculates the y value 53since its a circle we plot our lines and vertices around the start point for everything will always be the center. 54*/ 55strokeWeight(7); // set the thickness of the lines 56if (motion == 0) { // if going left to right 57for (int i = 0; i <= 20; i++) { // draw 20 lines with fading colour each 1 degree further round than the last 58stroke(0, (10*i), 0); // set the stroke colour (Red, Green, Blue) base it on the the value of i 59line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w); // line(start x, start y, end x, end y) 60} 61} else { // if going right to left 62for (int i = 20; i >= 0; i--) { // draw 20 lines with fading colour 63stroke(0,200-(10*i), 0); // using standard RGB values, each between 0 and 255 64line(radius, radius, radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w); 65} 66} 67/* Setup the shapes made from the sensor values */ 68noStroke(); // no outline 69/* first sweep */ 70fill(0,50,0); // set the fill colour of the shape (Red, Green, Blue) 71beginShape(); // start drawing shape 72for (int i = 0; i < 180; i++) { // for each degree in the array 73x = radius + cos(radians((180+i)))*((oldValue[i])); // create x coordinate 74y = radius + sin(radians((180+i)))*((oldValue[i])); // create y coordinate 75vertex(x, y); // plot vertices 76} 77endShape(); // end shape 78/* second sweep */ 79fill(0,110,0); 80beginShape(); 81for (int i = 0; i < 180; i++) { 82x = radius + cos(radians((180+i)))*(newValue[i]); 83y = radius + sin(radians((180+i)))*(newValue[i]); 84vertex(x, y); 85} 86endShape(); 87/* average */ 88fill(0,170,0); 89beginShape(); 90for (int i = 0; i < 180; i++) { 91x = radius + cos(radians((180+i)))*((newValue[i]+oldValue[i])/2); // create average 92y = radius + sin(radians((180+i)))*((newValue[i]+oldValue[i])/2); 93vertex(x, y); 94} 95endShape(); 96/* if after first 2 sweeps, highlight motion with red circle*/ 97if (firstRun >= 360) { 98stroke(150,0,0); 99strokeWeight(1); 100noFill(); 101for (int i = 0; i < 180; i++) { 102if (oldValue[i] - newValue[i] > 35 || newValue[i] - oldValue[i] > 35) { 103x = radius + cos(radians((180+i)))*(newValue[i]); 104y = radius + sin(radians((180+i)))*(newValue[i]); 105ellipse(x, y, 10, 10); 106} 107} 108} 109/* set the radar distance rings and out put their values, 50, 100, 150 etc.. */ 110for (int i = 0; i <=6; i++){ 111noFill(); 112strokeWeight(1); 113stroke(0, 255-(30*i), 0); 114ellipse(radius, radius, (100*i), (100*i)); 115fill(0, 100, 0); 116noStroke(); 117text(Integer.toString(radarDist+50), 380, (305-radarDist), 50, 50); 118radarDist+=50; 119} 120radarDist = 0; 121/* draw the grid lines on the radar every 30 degrees and write their values 180, 210, 240 etc.. */ 122for (int i = 0; i <= 6; i++) { 123strokeWeight(1); 124stroke(0, 55, 0); 125line(radius, radius, radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w); 126fill(0, 55, 0); 127noStroke(); 128if (180+(30*i) >= 300) { 129text(Integer.toString(180+(30*i)), (radius+10) + cos(radians(180+(30*i)))*(w+10), (radius+10) + sin(radians(180+(30*i)))*(w+10), 25,50); 130} else { 131text(Integer.toString(180+(30*i)), radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w, 60,40); 132} 133} 134/* Write information text and values. */ 135noStroke(); 136fill(0); 137rect(350,402,800,100); 138fill(0, 100, 0); 139text("Degrees: "+Integer.toString(degree), 100, 380, 100, 50); // use Integet.toString to convert numeric to string as text() only outputs strings 140text("Distance: "+Integer.toString(value), 100, 400, 100, 50); // text(string, x, y, width, height) 141text("Radar screen code ", 540, 380, 250, 50); 142fill(0); 143rect(70,60,150,100); 144fill(0, 100, 0); 145text("Screen Key:", 100, 50, 150, 50); 146fill(0,50,0); 147rect(30,53,10,10); 148text("First sweep", 115, 70, 150, 50); 149fill(0,110,0); 150rect(30,73,10,10); 151text("Second sweep", 115, 90, 150, 50); 152fill(0,170,0); 153rect(30,93,10,10); 154text("Average", 115, 110, 150, 50); 155noFill(); 156stroke(150,0,0); 157strokeWeight(1); 158ellipse(29, 113, 10, 10); 159fill(150,0,0); 160text("Motion", 115, 130, 150, 50); 161fill(0,450,500); 162text("Developed by: VAISAKH ", 350, 400, 150, 50); 163text("teamdiy.in ", 400, 430, 150, 50); 164fill(0,110,0); 165} 166/* get values from serial port */ 167void serialEvent (Serial arduinoport) { 168String xString = arduinoport.readStringUntil('\n'); // read the serial port until a new line 169if (xString != null) { // if theres data in between the new lines 170xString = trim(xString); // get rid of any whitespace just in case 171String getX = xString.substring(1, xString.indexOf("V")); // get the value of the servo position 172String getV = xString.substring(xString.indexOf("V")+1, xString.length()); // get the value of the sensor reading 173degree = Integer.parseInt(getX); // set the values to variables 174value = Integer.parseInt(getV); 175oldValue[degree] = newValue[degree]; // store the values in the arrays. 176newValue[degree] = value; 177/* sets a counter to allow for the first 2 sweeps of the servo */ 178firstRun++; 179if (firstRun > 360) { 180firstRun = 360; // keep the value at 360 181} 182} 183} 184
arduino code
arduino
upload this sketch into arduino
1#include <Servo.h> 2 3/* 4code for arduino bord ultrasonic radar system. 5for more arduino projects ---> teamdiy.in 6*/ 7 8Servo leftRightServo; // set a variable to map the servo 9int leftRightPos = 0; // set a variable to store the servo position 10const int numReadings = 10; // set a variable for the number of readings to take 11int index = 0; // the index of the current reading 12int total = 0; // the total of all readings 13int average = 0; // the average 14int echoPin = 6; // the SRF05's echo pin 15int initPin = 7; // the SRF05's init pin 16unsigned long pulseTime = 0; // variable for reading the pulse 17unsigned long distance = 0; // variable for storing distance 18 19/* setup the pins, servo and serial port */ 20void setup() { 21 leftRightServo.attach(10); 22 // make the init pin an output: 23 pinMode(initPin, OUTPUT); 24 // make the echo pin an input: 25 pinMode(echoPin, INPUT); 26 // initialize the serial port: 27 Serial.begin(9600); 28} 29 30/* begin rotating the servo and getting sensor values */ 31void loop() { 32 for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) { // going left to right. 33 leftRightServo.write(leftRightPos); 34 for (index = 0; index<=numReadings;index++) { // take x number of readings from the sensor and average them 35 digitalWrite(initPin, LOW); 36 delayMicroseconds(50); 37 digitalWrite(initPin, HIGH); // send signal 38 delayMicroseconds(50); // wait 50 microseconds for it to return 39 digitalWrite(initPin, LOW); // close signal 40 pulseTime = pulseIn(echoPin, HIGH); // calculate time for signal to return 41 distance = pulseTime/58; // convert to centimetres 42 total = total + distance; // update total 43 delay(10); 44 } 45 average = total/numReadings; // create average reading 46 47 if (index >= numReadings) { // reset the counts when at the last item of the array 48 index = 0; 49 total = 0; 50 } 51 Serial.print("X"); // print leading X to mark the following value as degrees 52 Serial.print(leftRightPos); // current servo position 53 Serial.print("V"); // preceeding character to separate values 54 Serial.println(average); // average of sensor readings 55 } 56 /* 57 start going right to left after we got to 180 degrees 58 same code as above 59 */ 60 for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) { // going right to left 61 leftRightServo.write(leftRightPos); 62 for (index = 0; index<=numReadings;index++) { 63 digitalWrite(initPin, LOW); 64 delayMicroseconds(50); 65 digitalWrite(initPin, HIGH); 66 delayMicroseconds(50); 67 digitalWrite(initPin, LOW); 68 pulseTime = pulseIn(echoPin, HIGH); 69 distance = pulseTime/58; 70 total = total + distance; 71 delay(10); 72 } 73 average = total/numReadings; 74 if (index >= numReadings) { 75 index = 0; 76 total = 0; 77 } 78 Serial.print("X"); 79 Serial.print(leftRightPos); 80 Serial.print("V"); 81 Serial.println(average); 82 } 83} 84
processing sketch
processing
run this sketch in processing
1/* 2Radar Screen Visualisation for HC-SR04 3Maps out an area of what 4 the HC-SR04 sees from a top down view. 5Takes and displays 2 readings, one left 6 to right and one right to left. 7Displays an average of the 2 readings 8Displays 9 motion alert if there is a large difference between the 2 values. 10for more projects 11 ----> teamdiy.in 12*/ 13import processing.serial.*; // import serial library 14Serial 15 arduinoport; // declare a serial port 16float x, y; // 17 variable to store x and y co-ordinates for vertices 18int radius = 350; // 19 set the radius of objects 20int w = 300; // set an arbitary 21 width value 22int degree = 0; // servo position in degrees 23int 24 value = 0; // value from sensor 25int motion = 0; // 26 value to store which way the servo is panning 27int[] newValue = new int[181]; 28 // create an array to store each new sensor value for each servo position 29int[] 30 oldValue = new int[181]; // create an array to store the previous values. 31PFont 32 myFont; // setup fonts in Processing 33int radarDist = 0; // 34 set value to configure Radar distance labels 35int firstRun = 0; // 36 value to ignore triggering motion on the first 2 servo sweeps 37/* create background 38 and serial buffer */ 39void setup(){ 40// setup the background size, colour and 41 font. 42size(1204, 650); 43background (0); // 0 = black 44myFont = createFont("verdana", 45 12); 46textFont(myFont); 47// setup the serial port and buffer 48arduinoport 49 = new Serial(this, "COM7", 9600); 50} 51 52/* draw the screen */ 53void draw(){ 54fill(0); 55 // set the following shapes to be black 56noStroke(); 57 // set the following shapes to have no outline 58ellipse(radius, 59 radius, 750, 750); // draw a circle with a width/ height = 750 with its center 60 position (x and y) set by the radius 61rectMode(CENTER); // 62 set the following rectangle to be drawn around its center 63rect(350,402,800,100); 64 // draw rectangle (x, y, width, height) 65if (degree >= 179) { 66 // if at the far right then set motion = 1/ true we're about to 67 go right to left 68motion = 1; // this changes the animation 69 to run right to left 70} 71if (degree <= 1) { // if servo at 72 0 degrees then we're about to go left to right 73motion = 0; // 74 this sets the animation to run left to right 75} 76/* setup the radar sweep */ 77/* 78 79We use trigonmetry to create points around a circle. 80So the radius plus the 81 cosine of the servo position converted to radians 82Since radians 0 start at 90 83 degrees we add 180 to make it start from the left 84Adding +1 (i) each time through 85 the loops to move 1 degree matching the one degree of servo movement 86cos is for 87 the x left to right value and sin calculates the y value 88since its a circle we 89 plot our lines and vertices around the start point for everything will always be 90 the center. 91*/ 92strokeWeight(7); // set the thickness 93 of the lines 94if (motion == 0) { // if going left to right 95for 96 (int i = 0; i <= 20; i++) { // draw 20 lines with fading colour each 1 degree 97 further round than the last 98stroke(0, (10*i), 0); // set the stroke 99 colour (Red, Green, Blue) base it on the the value of i 100line(radius, radius, 101 radius + cos(radians(degree+(180+i)))*w, radius + sin(radians(degree+(180+i)))*w); 102 // line(start x, start y, end x, end y) 103} 104} else { // 105 if going right to left 106for (int i = 20; i >= 0; i--) { // draw 20 lines with 107 fading colour 108stroke(0,200-(10*i), 0); // using standard RGB values, 109 each between 0 and 255 110line(radius, radius, radius + cos(radians(degree+(180+i)))*w, 111 radius + sin(radians(degree+(180+i)))*w); 112} 113} 114/* Setup the shapes made 115 from the sensor values */ 116noStroke(); // no outline 117/* 118 first sweep */ 119fill(0,50,0); // set the fill colour of 120 the shape (Red, Green, Blue) 121beginShape(); // start drawing 122 shape 123for (int i = 0; i < 180; i++) { // for each degree in the array 124x 125 = radius + cos(radians((180+i)))*((oldValue[i])); // create x coordinate 126y = 127 radius + sin(radians((180+i)))*((oldValue[i])); // create y coordinate 128vertex(x, 129 y); // plot vertices 130} 131endShape(); // 132 end shape 133/* second sweep */ 134fill(0,110,0); 135beginShape(); 136for (int i 137 = 0; i < 180; i++) { 138x = radius + cos(radians((180+i)))*(newValue[i]); 139y = 140 radius + sin(radians((180+i)))*(newValue[i]); 141vertex(x, y); 142} 143endShape(); 144/* 145 average */ 146fill(0,170,0); 147beginShape(); 148for (int i = 0; i < 180; i++) { 149x 150 = radius + cos(radians((180+i)))*((newValue[i]+oldValue[i])/2); // create average 151y 152 = radius + sin(radians((180+i)))*((newValue[i]+oldValue[i])/2); 153vertex(x, y); 154} 155endShape(); 156/* 157 if after first 2 sweeps, highlight motion with red circle*/ 158if (firstRun >= 360) 159 { 160stroke(150,0,0); 161strokeWeight(1); 162noFill(); 163for (int i = 0; i < 180; 164 i++) { 165if (oldValue[i] - newValue[i] > 35 || newValue[i] - oldValue[i] > 35) 166 { 167x = radius + cos(radians((180+i)))*(newValue[i]); 168y = radius + sin(radians((180+i)))*(newValue[i]); 169ellipse(x, 170 y, 10, 10); 171} 172} 173} 174/* set the radar distance rings and out put their 175 values, 50, 100, 150 etc.. */ 176for (int i = 0; i <=6; i++){ 177noFill(); 178strokeWeight(1); 179stroke(0, 180 255-(30*i), 0); 181ellipse(radius, radius, (100*i), (100*i)); 182fill(0, 100, 0); 183noStroke(); 184text(Integer.toString(radarDist+50), 185 380, (305-radarDist), 50, 50); 186radarDist+=50; 187} 188radarDist = 0; 189/* draw 190 the grid lines on the radar every 30 degrees and write their values 180, 210, 240 191 etc.. */ 192for (int i = 0; i <= 6; i++) { 193strokeWeight(1); 194stroke(0, 55, 195 0); 196line(radius, radius, radius + cos(radians(180+(30*i)))*w, radius + sin(radians(180+(30*i)))*w); 197fill(0, 198 55, 0); 199noStroke(); 200if (180+(30*i) >= 300) { 201text(Integer.toString(180+(30*i)), 202 (radius+10) + cos(radians(180+(30*i)))*(w+10), (radius+10) + sin(radians(180+(30*i)))*(w+10), 203 25,50); 204} else { 205text(Integer.toString(180+(30*i)), radius + cos(radians(180+(30*i)))*w, 206 radius + sin(radians(180+(30*i)))*w, 60,40); 207} 208} 209/* Write information text 210 and values. */ 211noStroke(); 212fill(0); 213rect(350,402,800,100); 214fill(0, 100, 215 0); 216text("Degrees: "+Integer.toString(degree), 100, 380, 100, 50); // 217 use Integet.toString to convert numeric to string as text() only outputs strings 218text("Distance: 219 "+Integer.toString(value), 100, 400, 100, 50); // text(string, x, y, width, 220 height) 221text("Radar screen code ", 540, 380, 250, 50); 222fill(0); 223rect(70,60,150,100); 224fill(0, 225 100, 0); 226text("Screen Key:", 100, 50, 150, 50); 227fill(0,50,0); 228rect(30,53,10,10); 229text("First 230 sweep", 115, 70, 150, 50); 231fill(0,110,0); 232rect(30,73,10,10); 233text("Second 234 sweep", 115, 90, 150, 50); 235fill(0,170,0); 236rect(30,93,10,10); 237text("Average", 238 115, 110, 150, 50); 239noFill(); 240stroke(150,0,0); 241strokeWeight(1); 242ellipse(29, 243 113, 10, 10); 244fill(150,0,0); 245text("Motion", 115, 130, 150, 50); 246fill(0,450,500); 247text("Developed 248 by: VAISAKH ", 350, 400, 150, 50); 249text("teamdiy.in ", 400, 430, 150, 50); 250fill(0,110,0); 251} 252/* 253 get values from serial port */ 254void serialEvent (Serial arduinoport) { 255String 256 xString = arduinoport.readStringUntil('\ 257'); // read the serial port until a new 258 line 259if (xString != null) { // if theres data in between the new lines 260xString 261 = trim(xString); // get rid of any whitespace just in case 262String getX = xString.substring(1, 263 xString.indexOf("V")); // get the value of the servo position 264String getV = 265 xString.substring(xString.indexOf("V")+1, xString.length()); // get the value 266 of the sensor reading 267degree = Integer.parseInt(getX); // set the values to variables 268value 269 = Integer.parseInt(getV); 270oldValue[degree] = newValue[degree]; // store the values 271 in the arrays. 272newValue[degree] = value; 273/* sets a counter to allow for 274 the first 2 sweeps of the servo */ 275firstRun++; 276if (firstRun > 360) { 277firstRun 278 = 360; // keep the value at 360 279} 280} 281} 282
Downloadable files
Circuit Diagram
Connect the pins as shown in figure
Circuit Diagram
Comments
Only logged in users can leave comments