Remote Controlled Radar System
Built a radar system with sound effect and remote control
Components and supplies
1
ELEGOO UNO R3 Board
1
Jumper wires (generic)
1
Micro servo SG90
1
5V Power Supply Module
3
LED Lights
1
9V Battery with clip
1
Ultrasonic Sensor - HC-SR04
1
Active Buzzer
3
Resistor 220 ohm
1
Remote and IR receiver
Apps and platforms
1
Arduino IDE program
Project description
Code
Arduino Code
cpp
This code is for the system itself
1#include <IRremote.hpp> 2 3#include <SR04.h> 4#include <Servo.h> 5#define TRIG_PIN 4 6#define ECHO_PIN 3 7 8SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); 9Servo myservo; // create servo object to control a servo 10 11int receiver = 11; 12int delayTime = 15; 13int RED_LED_PIN = 8; 14int YELLOW_LED_PIN = 6; 15int GREEN_LED_PIN = 12; 16int duration; 17int buzzer = 13; 18int sound_duration = 1000; 19IRrecv irrecv(receiver); // create instance of 'irrecv' 20uint32_t last_decodedRawData = 0;//vairable uses to store the last decodedRawData 21 22long distance; 23int pos = 0; // variable to store the servo position 24 25bool automatic_mode = true; 26 27 28int calculateDistance(){ 29 int res = 0; 30 digitalWrite(TRIG_PIN, LOW); 31 delayMicroseconds(2); 32 digitalWrite(TRIG_PIN, HIGH); 33 delayMicroseconds(10); 34 digitalWrite(TRIG_PIN, LOW); 35 duration = pulseIn(ECHO_PIN, HIGH); // reads echoPin and returns the sound wave travel time (ms) 36 res = duration * 0.034 / 2; 37 return res; 38} 39 40void checkIfEnteringAnObject(){ 41 distance = calculateDistance(); 42 43 44 // Green LED on when distance >= 40 45 if (distance >= 40) { 46 47 digitalWrite(GREEN_LED_PIN, HIGH); 48 digitalWrite(YELLOW_LED_PIN, LOW); 49 digitalWrite(RED_LED_PIN, LOW); 50 digitalWrite(buzzer, LOW); 51 // delay(sound_duration);//wait for sound_duration ms 52 } 53 54 // Yellow LED on when 20 <= distance < 40 55 else if (distance < 40 && distance >= 20){ 56 digitalWrite(GREEN_LED_PIN, LOW); 57 digitalWrite(YELLOW_LED_PIN, HIGH); 58 digitalWrite(RED_LED_PIN, LOW); 59 digitalWrite(buzzer, LOW); 60 // delay(sound_duration);//wait for sound_duration ms 61 } 62 63 // Red LED on when distance < 20 64 else if(distance >= 1 && distance < 20) { 65 66 digitalWrite(GREEN_LED_PIN, LOW); 67 digitalWrite(YELLOW_LED_PIN, LOW); 68 digitalWrite(RED_LED_PIN, HIGH); 69 digitalWrite(buzzer, HIGH); 70 // delay(sound_duration); 71 } 72 73} 74 75 76void setup() 77{ 78 myservo.attach(9); 79 automatic_mode = true; 80 pinMode(RED_LED_PIN, OUTPUT); 81 pinMode(YELLOW_LED_PIN, OUTPUT); 82 pinMode(GREEN_LED_PIN, OUTPUT); 83 irrecv.enableIRIn(); 84 85 pinMode(buzzer, OUTPUT); //initialize the buzzer pin as an output 86 87 digitalWrite(GREEN_LED_PIN, LOW); 88 digitalWrite(YELLOW_LED_PIN, LOW); 89 digitalWrite(RED_LED_PIN, LOW); 90 91 Serial.begin(9600); 92 93 94 // digitalWrite(RED_LED_PIN, HIGH); 95 96 // myservo.writeMicroseconds(delayTime00); 97 98 // digitalWrite(YELLOW_LED_PIN, HIGH); 99 // digitalWrite(GREEN_LED_PIN, HIGH); 100 101 delay(1000); 102} 103 104void loop() { 105 106 // perform rotation based on remote buttons 107 if (irrecv.decode()) // have we received an IR signal? 108 { 109 // Check if it is a repeat IR code 110 if (irrecv.decodedIRData.flags) 111 { 112 //set the current decodedRawData to the last decodedRawData 113 irrecv.decodedIRData.decodedRawData = last_decodedRawData; 114 } 115 116 117 switch (irrecv.decodedIRData.decodedRawData) 118 { 119 120 case 0xE916FF00: // "0" button is pressed -> Turn off the automatic mode 121 // Serial.println("I run"); 122 automatic_mode = false; 123 pos = 0; 124 myservo.write(pos); 125 // checkIfEnteringAnObject(); 126 delay(delayTime); 127 break; 128 129 case 0xF30CFF00: // "1" button is pressed -> Turn on the automatic mode 130 automatic_mode = true; 131 break; 132 133 case 0xB946FF00: // VOL+ button pressed -> Rotate the servo by 5 clockwise 134 pos += 5; 135 if(pos >= 180) pos = 180; 136 myservo.write(pos); 137 // checkIfEnteringAnObject(); 138 delay(delayTime); 139 break; 140 case 0xEA15FF00: // VOL- button pressed -> -> Rotate the servo by 5 counterclockwise 141 pos -= 5; 142 if(pos <= 0) pos = 0; 143 myservo.write(pos); 144 // checkIfEnteringAnObject(); 145 delay(delayTime); 146 break; 147 148 case 0xE718FF00: // "2" button is pressed -> Rotate the servo to 0 degree position 149 pos = 0; 150 151 myservo.write(pos); 152 // checkIfEnteringAnObject(); 153 delay(delayTime); 154 break; 155 case 0xA55AFF00: // "6" button is pressed -> Rotate the servo to 45 degree position 156 pos = 45; 157 myservo.write(pos); 158 // checkIfEnteringAnObject(); 159 delay(delayTime); 160 break; 161 162 case 0xAD52FF00: // "68 button is pressed -> Rotate the servo to 90 degree position 163 pos = 90; 164 myservo.write(pos); 165 // checkIfEnteringAnObject(); 166 delay(delayTime); 167 break; 168 case 0xF708FF00: // "4" button is pressed -> Rotate the servo to 135 degree position 169 pos = 135; 170 myservo.write(pos); 171 // checkIfEnteringAnObject(); 172 delay(delayTime); 173 break; 174 175 case 0xE31CFF00: 176 pos = 180; 177 myservo.write(pos); 178 // checkIfEnteringAnObject(); 179 delay(delayTime); 180 break; 181 182 183 184 default: break; 185 186 187 188 } 189 190 191 //store the last decodedRawData 192 last_decodedRawData = irrecv.decodedIRData.decodedRawData; 193 irrecv.resume(); // receive the next value 194 } 195 196 197 // perform automatic rotation 198 if (automatic_mode == true) { 199 pos = 0; 200 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees 201 // in steps of 1 degree 202 203 myservo.write(pos); // tell servo to go to position in variable 'pos' 204 checkIfEnteringAnObject(); 205 delay(delayTime); // waits delayTimems for the servo to reach the position 206 Serial.print(pos); 207 Serial.print(","); 208 Serial.print(distance); 209 Serial.print("."); 210 } 211 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees 212 myservo.write(pos); // tell servo to go to position in variable 'pos' 213 checkIfEnteringAnObject(); 214 delay(delayTime); // waits delayTimems for the servo to reach the position 215 Serial.print(pos); 216 Serial.print(","); 217 Serial.print(distance); 218 Serial.print("."); 219 } 220 221 222 }else{ 223 checkIfEnteringAnObject(); 224 Serial.print(pos); 225 Serial.print(","); 226 Serial.print(distance); 227 Serial.print("."); 228 } 229 230 231 232 // delay(1000); 233 234}
Interface
java
This code is for the radar interface
1import processing.serial.*; // imports library for serial communication 2import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 3import java.io.IOException; 4Serial myPort; // defines Object Serial 5// defubes variables 6String angle=""; 7String distance=""; 8String data=""; 9String noObject; 10float pixsDistance; 11int iAngle, iDistance; 12int index1=0; 13int index2=0; 14PFont orcFont; 15void setup() { 16 17 size (1500, 900); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION*** 18 smooth(); 19 myPort = new Serial(this, "COM5", 9600); // starts the serial communication 20 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 21} 22void draw() {; 23 fill(98, 245, 31); 24 // simulating motion blur and slow fade of the moving line 25 noStroke(); 26 fill(0, 4); 27 rect(0, 0, width, height-height*0.065); 28 29 fill(98, 245, 31); // green color 30 // calls the functions for drawing the radar 31 drawRadar(); 32 drawLine(); 33 drawObject(); 34 drawText(); 35} 36void serialEvent (Serial myPort) { // starts reading data from the Serial Port 37 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 38 data = myPort.readStringUntil('.'); 39 data = data.substring(0, data.length()-1); 40 41 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 42 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 43 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 44 45 // converts the String variables into Integer 46 iAngle = int(angle); 47 iDistance = int(distance); 48} 49void drawRadar() { 50 pushMatrix(); 51 translate(width/2, height-height*0.074); // moves the starting coordinats to new location 52 noFill(); 53 strokeWeight(2); 54 stroke(98, 245, 31); 55 // draws the arc lines 56 arc(0, 0, (width-width*0.0625), (width-width*0.0625), PI, TWO_PI); 57 arc(0, 0, (width-width*0.27), (width-width*0.27), PI, TWO_PI); 58 arc(0, 0, (width-width*0.479), (width-width*0.479), PI, TWO_PI); 59 arc(0, 0, (width-width*0.687), (width-width*0.687), PI, TWO_PI); 60 // draws the angle lines 61 line(-width/2, 0, width/2, 0); 62 line(0, 0, (-width/2)*cos(radians(30)), (-width/2)*sin(radians(30))); 63 line(0, 0, (-width/2)*cos(radians(60)), (-width/2)*sin(radians(60))); 64 line(0, 0, (-width/2)*cos(radians(90)), (-width/2)*sin(radians(90))); 65 line(0, 0, (-width/2)*cos(radians(120)), (-width/2)*sin(radians(120))); 66 line(0, 0, (-width/2)*cos(radians(150)), (-width/2)*sin(radians(150))); 67 line((-width/2)*cos(radians(30)), 0, width/2, 0); 68 popMatrix(); 69} 70void drawObject() { 71 pushMatrix(); 72 translate(width/2, height-height*0.074); // moves the starting coordinats to new location 73 strokeWeight(9); 74 stroke(255, 10, 10); // red color 75 pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels 76 // limiting the range to 40 cms 77 if (iDistance<40) { 78 // draws the object according to the angle and the distance 79 line(pixsDistance*cos(radians(iAngle)), -pixsDistance*sin(radians(iAngle)), (width-width*0.505)*cos(radians(iAngle)), -(width-width*0.505)*sin(radians(iAngle))); 80 } 81 popMatrix(); 82} 83void drawLine() { 84 pushMatrix(); 85 strokeWeight(9); 86 stroke(30, 250, 60); 87 translate(width/2, height-height*0.074); // moves the starting coordinats to new location 88 line(0, 0, (height-height*0.12)*cos(radians(iAngle)), -(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle 89 popMatrix(); 90} 91void drawText() { // draws the texts on the screen 92 93 pushMatrix(); 94 if (iDistance>40) { 95 noObject = "Out of Range"; 96 } else { 97 noObject = "In Range"; 98 } 99 fill(0, 0, 0); 100 noStroke(); 101 rect(0, height-height*0.0648, width, height); 102 fill(98, 245, 31); 103 textSize(25); 104 105 text("10cm", width-width*0.3854, height-height*0.0833); 106 text("20cm", width-width*0.281, height-height*0.0833); 107 text("30cm", width-width*0.177, height-height*0.0833); 108 text("40cm", width-width*0.0729, height-height*0.0833); 109 textSize(40); 110 //text("N_Tech ", width-width*0.875, height-height*0.0277); 111 text("Angle: " + iAngle +" ", width-width*0.875, height-height*0.0277); 112 text("Distance: ", width-width*0.26, height-height*0.0277); 113 if (iDistance<40) { 114 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 115 } 116 textSize(25); 117 fill(98, 245, 60); 118 translate((width-width*0.4994)+width/2*cos(radians(30)), (height-height*0.0907)-width/2*sin(radians(30))); 119 rotate(-radians(-60)); 120 text("30", 0, 0); 121 resetMatrix(); 122 translate((width-width*0.503)+width/2*cos(radians(60)), (height-height*0.0888)-width/2*sin(radians(60))); 123 rotate(-radians(-30)); 124 text("60", 0, 0); 125 resetMatrix(); 126 translate((width-width*0.507)+width/2*cos(radians(90)), (height-height*0.0833)-width/2*sin(radians(90))); 127 rotate(radians(0)); 128 text("90", 0, 0); 129 resetMatrix(); 130 translate(width-width*0.513+width/2*cos(radians(120)), (height-height*0.07129)-width/2*sin(radians(120))); 131 rotate(radians(-30)); 132 text("120", 0, 0); 133 resetMatrix(); 134 translate((width-width*0.5104)+width/2*cos(radians(150)), (height-height*0.0574)-width/2*sin(radians(150))); 135 rotate(radians(-60)); 136 text("150", 0, 0); 137 popMatrix(); 138}
Documentation
Schematic
Schematic of the project.
schematic.png

Comments
Only logged in users can leave comments