Components and supplies
Arduino Mega 2560
USB-A to B Cable
SG90 Micro-servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
LED (generic)
Holder for Ultrasonic Sensor HC-SR04
Perma-Proto Breadboard Half Size
Apps and platforms
QuickStego tool
Processing
Arduino IDE
Project description
Code
Arduino IDE code
c_cpp
Copy and paste the code in Arduino IDE.
1// Includes the Servo library 2#include <Servo.h>. 3 4// Defines Tirg and Echo pins of the Ultrasonic Sensor and pin of LED 5const int trigPin = 10; 6const int echoPin = 11; 7const int LED = 13; 8// Variables for the duration and the distance 9long duration; 10int distance; 11 12Servo myServo; // Creates a servo object for controlling the servo motor 13 14void setup() { 15 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 16 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 17 pinMode(13, OUTPUT); // Sets the LED as an Input 18 Serial.begin(115200); 19 myServo.attach(12); // Defines on which pin is the servo motor attached 20} 21void loop() { 22 // rotates the servo motor from 15 to 165 degrees 23 for(int i=15;i<=165;i++){ 24 myServo.write(i); 25 delay(30); 26 distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree 27 28 Serial.print(i); // Sends the current degree into the Serial Port 29 Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 30 Serial.print(distance); // Sends the distance value into the Serial Port 31 Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 32 } 33 // Repeats the previous lines from 165 to 15 degrees 34 for(int i=165;i>15;i--){ 35 myServo.write(i); 36 delay(30); 37 distance = calculateDistance(); 38 Serial.print(i); 39 Serial.print(","); 40 Serial.print(distance); 41 Serial.print("."); 42 } 43} 44// Function for calculating the distance measured by the Ultrasonic sensor 45int calculateDistance(){ 46 47 digitalWrite(trigPin, LOW); 48 delayMicroseconds(2); 49 // Sets the trigPin on HIGH state for 10 micro seconds 50 digitalWrite(trigPin, HIGH); 51 delayMicroseconds(10); 52 digitalWrite(trigPin, LOW); 53 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds 54 distance= duration*0.034/2; 55 if (distance < 40) 56 { 57 digitalWrite(13, HIGH); 58 delay(10); 59 } 60 else 61 { 62 digitalWrite(13, LOW); 63 delay(10); 64 } 65 return distance; 66} 67
Processing PDE code
c_cpp
Copy and paste the code in Processing PDE.
1/* Arduino Radar Project 2 * 3 * Updated version. Fits any screen resolution! 4 * Just change the values in the size() function, 5 * with your screen resolution. 6 * 7 * by Dejan Nedelkovski, updated by Rexhep Mustafovski 8 * 9 */ 10import processing.serial.*; // imports library for serial communication 11import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 12import java.io.IOException; 13Serial myPort; // defines Object Serial 14// defubes variables 15String angle=""; 16String distance=""; 17String data=""; 18String noObject; 19float pixsDistance; 20float Razmer; // dodadeno 21int iAngle, iDistance; 22int index1=0; 23int index2=0; 24int pravec=1; 25PFont orcFont; 26void setup() { 27 28 //size (1200, 950); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION*** 29 30 fullScreen(); // ********************* Size e zameneto s fullScreen taka da avtomatski ke se zadadat height i with 31 32 Razmer = (height-height*0.21)*0.025; // cm pretvarame vo pikseli so formulata: x*Razmer = x * ((height-height*0.25)*0.025); Dolzina[vo cm] * (RabotnaVisinaEkranVoPixeli * 1/40) max=40cm 33 34 smooth(); 35 myPort = new Serial(this,"COM3", 115200); // starts the serial communication 36 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 37 orcFont = loadFont("OCRAExtended-30.vlw"); 38 39 //************************************************************ dopolna 40 //iAngle = 0; 41 //iDistance = 25; 42 //frameRate(100); 43 44 //************************************************************ 45 46 47} 48void draw() { 49 50 fill(98,245,31); 51 textFont(orcFont); 52 // simulating motion blur and slow fade of the moving line 53 noStroke(); 54 fill(0,5); 55 rect(0, 0, width, height-height*0.065); 56 57 fill(98,245,31); // green color 58 // calls the functions for drawing the radar 59 drawRadar(); 60 drawLine(); 61 drawObject(); 62 drawText(); 63 64 //************************************************************ dopolna za Avtomatski test na iscrtuvanjeto 65 66 //iAngle = iAngle+pravec; 67 // if (iAngle==0) { 68 // pravec=pravec*(-1); 69 // } 70 // else if (iAngle==180) 71 // { 72 // pravec=pravec*(-1); 73 // } 74 75 //************************************************************ 76} 77void serialEvent (Serial myPort) { // starts reading data from the Serial Port 78 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 79 data = myPort.readStringUntil('.'); 80 data = data.substring(0,data.length()-1); 81 82 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 83 84 // angle= (data.substring(0, index1)); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port 85 //distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance 86 87 angle= trim(data.substring(0, index1)); // dodadena funkcija trim za da gi otvrli praznite mesta 88 distance= trim(data.substring(index1+1, data.length())); // dodadena funkcija trim za da gi otvrli praznite mesta 89 90 91 // converts the String variables into Integer 92 iAngle = int(angle); 93 iDistance = int(distance); 94} 95void drawRadar() { 96 pushMatrix(); 97 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 98 noFill(); 99 strokeWeight(2); 100 stroke(98,245,31); 101 // draws the arc lines 102 103 arc(0,0,2*40*Razmer,2*40*Razmer,PI,TWO_PI); // Moi novi formuli so krugovi na 10, 20, 30 i 40 cm 104 arc(0,0,2*30*Razmer,2*30*Razmer,PI,TWO_PI); 105 arc(0,0,2*20*Razmer,2*20*Razmer,PI,TWO_PI); 106 arc(0,0,2*10*Razmer,2*10*Razmer,PI,TWO_PI); 107 108 //arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI); 109 //arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI); // Originalni formuli 110 //arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI); 111 //arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI); 112 113 // draws the angle lines 114 line(-width/2,0,width/2,0); 115 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 116 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 117 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 118 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 119 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 120 line((-width/2)*cos(radians(30)),0,width/2,0); 121 popMatrix(); 122} 123void drawObject() { 124 pushMatrix(); 125 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 126 strokeWeight(9); 127 stroke(255,10,10); // red color 128 129 pixsDistance = iDistance*Razmer; // converts the distance from the sensor from cm to pixels 130 // limiting the range to 40 cms 131 if(iDistance<40){ 132 // draws the object according to the angle and the distance 133 134 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // Popravena linija so novi krajni koordinati 135// line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(iDistance+2)*Razmer*cos(radians(iAngle)),-(iDistance+2)*Razmer*sin(radians(iAngle))); // Objekt so golemina od 1 cm 136 } 137 popMatrix(); 138} 139void drawLine() { 140 pushMatrix(); 141 strokeWeight(9); 142 stroke(30,250,60); 143 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 144 // line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle 145 line(0,0,40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // Nova popravena linija za krajnite koordinati 146 popMatrix(); 147} 148void drawText() { // draws the texts on the screen 149 150 pushMatrix(); 151 if(iDistance>40) { 152 noObject = "Out of Range"; 153 } 154 else { 155 noObject = "In Range"; 156 } 157 fill(0,0,0); 158 noStroke(); 159 rect(0, height-height*0.0648, width, height); 160 fill(98,245,31); 161 textSize(25); 162 163 164 //text("10cm",width-width*0.41,height-height*0.0833); 165 //text("20cm",width-width*0.3,height-height*0.0833); 166 //text("30cm",width-width*0.18,height-height*0.0833); 167 //text("40cm",width-width*0.06,height-height*0.0833); 168 169 text("10cm",width/2+7*Razmer,height-height*0.0833); 170 text("20cm",width/2+17*Razmer,height-height*0.0833); 171 text("30cm",width/2+27*Razmer,height-height*0.0833); 172 text("40cm",width/2+37*Razmer,height-height*0.0833); 173 174 textSize(40); 175 text("Object: " + noObject, width-width*0.875, height-height*0.0277); 176 text("Angle: " + iAngle +" ", width-width*0.48, height-height*0.0277); 177 text("Distance: ", width-width*0.26, height-height*0.0277); 178 if(iDistance<40) { 179 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 180 } 181 textSize(25); 182 fill(98,245,60); 183 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 184 rotate(-radians(-60)); 185 text("30",0,0); 186 resetMatrix(); 187 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 188 rotate(-radians(-30)); 189 text("60",0,0); 190 resetMatrix(); 191 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 192 rotate(radians(0)); 193 text("90",0,0); 194 resetMatrix(); 195 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 196 rotate(radians(-30)); 197 text("120",0,0); 198 resetMatrix(); 199 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 200 rotate(radians(-60)); 201 text("150",0,0); 202 popMatrix(); 203} 204
Processing PDE code
c_cpp
Copy and paste the code in Processing PDE.
1/* Arduino Radar Project 2 * 3 * Updated version. Fits any screen resolution! 4 * Just change the values in the size() function, 5 * with your screen resolution. 6 * 7 * by Dejan Nedelkovski, updated by Rexhep Mustafovski 8 * 9 */ 10import processing.serial.*; // imports library for serial communication 11import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 12import java.io.IOException; 13Serial myPort; // defines Object Serial 14// defubes variables 15String angle=""; 16String distance=""; 17String data=""; 18String noObject; 19float pixsDistance; 20float Razmer; // dodadeno 21int iAngle, iDistance; 22int index1=0; 23int index2=0; 24int pravec=1; 25PFont orcFont; 26void setup() { 27 28 //size (1200, 950); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION*** 29 30 fullScreen(); // ********************* Size e zameneto s fullScreen taka da avtomatski ke se zadadat height i with 31 32 Razmer = (height-height*0.21)*0.025; // cm pretvarame vo pikseli so formulata: x*Razmer = x * ((height-height*0.25)*0.025); Dolzina[vo cm] * (RabotnaVisinaEkranVoPixeli * 1/40) max=40cm 33 34 smooth(); 35 myPort = new Serial(this,"COM3", 115200); // starts the serial communication 36 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 37 orcFont = loadFont("OCRAExtended-30.vlw"); 38 39 //************************************************************ dopolna 40 //iAngle = 0; 41 //iDistance = 25; 42 //frameRate(100); 43 44 //************************************************************ 45 46 47} 48void draw() { 49 50 fill(98,245,31); 51 textFont(orcFont); 52 // simulating motion blur and slow fade of the moving line 53 noStroke(); 54 fill(0,5); 55 rect(0, 0, width, height-height*0.065); 56 57 fill(98,245,31); // green color 58 // calls the functions for drawing the radar 59 drawRadar(); 60 drawLine(); 61 drawObject(); 62 drawText(); 63 64 //************************************************************ dopolna za Avtomatski test na iscrtuvanjeto 65 66 //iAngle = iAngle+pravec; 67 // if (iAngle==0) { 68 // pravec=pravec*(-1); 69 // } 70 // else if (iAngle==180) 71 // { 72 // pravec=pravec*(-1); 73 // } 74 75 //************************************************************ 76} 77void serialEvent (Serial myPort) { // starts reading data from the Serial Port 78 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 79 data = myPort.readStringUntil('.'); 80 data = data.substring(0,data.length()-1); 81 82 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 83 84 // angle= (data.substring(0, index1)); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port 85 //distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance 86 87 angle= trim(data.substring(0, index1)); // dodadena funkcija trim za da gi otvrli praznite mesta 88 distance= trim(data.substring(index1+1, data.length())); // dodadena funkcija trim za da gi otvrli praznite mesta 89 90 91 // converts the String variables into Integer 92 iAngle = int(angle); 93 iDistance = int(distance); 94} 95void drawRadar() { 96 pushMatrix(); 97 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 98 noFill(); 99 strokeWeight(2); 100 stroke(98,245,31); 101 // draws the arc lines 102 103 arc(0,0,2*40*Razmer,2*40*Razmer,PI,TWO_PI); // Moi novi formuli so krugovi na 10, 20, 30 i 40 cm 104 arc(0,0,2*30*Razmer,2*30*Razmer,PI,TWO_PI); 105 arc(0,0,2*20*Razmer,2*20*Razmer,PI,TWO_PI); 106 arc(0,0,2*10*Razmer,2*10*Razmer,PI,TWO_PI); 107 108 //arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI); 109 //arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI); // Originalni formuli 110 //arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI); 111 //arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI); 112 113 // draws the angle lines 114 line(-width/2,0,width/2,0); 115 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 116 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 117 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 118 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 119 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 120 line((-width/2)*cos(radians(30)),0,width/2,0); 121 popMatrix(); 122} 123void drawObject() { 124 pushMatrix(); 125 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 126 strokeWeight(9); 127 stroke(255,10,10); // red color 128 129 pixsDistance = iDistance*Razmer; // converts the distance from the sensor from cm to pixels 130 // limiting the range to 40 cms 131 if(iDistance<40){ 132 // draws the object according to the angle and the distance 133 134 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // Popravena linija so novi krajni koordinati 135// line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(iDistance+2)*Razmer*cos(radians(iAngle)),-(iDistance+2)*Razmer*sin(radians(iAngle))); // Objekt so golemina od 1 cm 136 } 137 popMatrix(); 138} 139void drawLine() { 140 pushMatrix(); 141 strokeWeight(9); 142 stroke(30,250,60); 143 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 144 // line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle 145 line(0,0,40*Razmer*cos(radians(iAngle)),-40*Razmer*sin(radians(iAngle))); // Nova popravena linija za krajnite koordinati 146 popMatrix(); 147} 148void drawText() { // draws the texts on the screen 149 150 pushMatrix(); 151 if(iDistance>40) { 152 noObject = "Out of Range"; 153 } 154 else { 155 noObject = "In Range"; 156 } 157 fill(0,0,0); 158 noStroke(); 159 rect(0, height-height*0.0648, width, height); 160 fill(98,245,31); 161 textSize(25); 162 163 164 //text("10cm",width-width*0.41,height-height*0.0833); 165 //text("20cm",width-width*0.3,height-height*0.0833); 166 //text("30cm",width-width*0.18,height-height*0.0833); 167 //text("40cm",width-width*0.06,height-height*0.0833); 168 169 text("10cm",width/2+7*Razmer,height-height*0.0833); 170 text("20cm",width/2+17*Razmer,height-height*0.0833); 171 text("30cm",width/2+27*Razmer,height-height*0.0833); 172 text("40cm",width/2+37*Razmer,height-height*0.0833); 173 174 textSize(40); 175 text("Object: " + noObject, width-width*0.875, height-height*0.0277); 176 text("Angle: " + iAngle +" ", width-width*0.48, height-height*0.0277); 177 text("Distance: ", width-width*0.26, height-height*0.0277); 178 if(iDistance<40) { 179 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 180 } 181 textSize(25); 182 fill(98,245,60); 183 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 184 rotate(-radians(-60)); 185 text("30",0,0); 186 resetMatrix(); 187 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 188 rotate(-radians(-30)); 189 text("60",0,0); 190 resetMatrix(); 191 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 192 rotate(radians(0)); 193 text("90",0,0); 194 resetMatrix(); 195 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 196 rotate(radians(-30)); 197 text("120",0,0); 198 resetMatrix(); 199 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 200 rotate(radians(-60)); 201 text("150",0,0); 202 popMatrix(); 203} 204
Arduino IDE code
c_cpp
Copy and paste the code in Arduino IDE.
1// Includes the Servo library 2#include <Servo.h>. 3 4// Defines 5 Tirg and Echo pins of the Ultrasonic Sensor and pin of LED 6const int trigPin 7 = 10; 8const int echoPin = 11; 9const int LED = 13; 10// Variables for the 11 duration and the distance 12long duration; 13int distance; 14 15Servo myServo; 16 // Creates a servo object for controlling the servo motor 17 18void setup() { 19 20 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 21 pinMode(echoPin, 22 INPUT); // Sets the echoPin as an Input 23 pinMode(13, OUTPUT); // Sets the LED 24 as an Input 25 Serial.begin(115200); 26 myServo.attach(12); // Defines on which 27 pin is the servo motor attached 28} 29void loop() { 30 // rotates the servo 31 motor from 15 to 165 degrees 32 for(int i=15;i<=165;i++){ 33 myServo.write(i); 34 35 delay(30); 36 distance = calculateDistance();// Calls a function for calculating 37 the distance measured by the Ultrasonic sensor for each degree 38 39 Serial.print(i); 40 // Sends the current degree into the Serial Port 41 Serial.print(","); // Sends 42 addition character right next to the previous value needed later in the Processing 43 IDE for indexing 44 Serial.print(distance); // Sends the distance value into the 45 Serial Port 46 Serial.print("."); // Sends addition character right next to 47 the previous value needed later in the Processing IDE for indexing 48 } 49 // 50 Repeats the previous lines from 165 to 15 degrees 51 for(int i=165;i>15;i--){ 52 53 myServo.write(i); 54 delay(30); 55 distance = calculateDistance(); 56 57 Serial.print(i); 58 Serial.print(","); 59 Serial.print(distance); 60 Serial.print("."); 61 62 } 63} 64// Function for calculating the distance measured by the Ultrasonic 65 sensor 66int calculateDistance(){ 67 68 digitalWrite(trigPin, LOW); 69 70 delayMicroseconds(2); 71 // Sets the trigPin on HIGH state for 10 micro seconds 72 73 digitalWrite(trigPin, HIGH); 74 delayMicroseconds(10); 75 digitalWrite(trigPin, 76 LOW); 77 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the 78 sound wave travel time in microseconds 79 distance= duration*0.034/2; 80 if 81 (distance < 40) 82 { 83 digitalWrite(13, HIGH); 84 delay(10); 85 86 } 87 else 88 { 89 digitalWrite(13, LOW); 90 delay(10); 91 92 } 93 return distance; 94} 95
Downloadable files
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components
SCHEMATICS
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components
SCHEMATICS
SCHEMATICS
Arduino Mega 2560 Sketch of connecting the components
SCHEMATICS
Comments
Only logged in users can leave comments