Introduction: Simplest Short-range Radar+status Lights!
To make a radar using SRF sensors, I used @makerrobotics tutorial, here's the link, but I thought it will be awesome with status lights. So I changed the circuit and the codes a little and there it is!
what did I add?
The original project, offered analog distance write, dynamic-graphical and colored radar sheet by processing. I added two LEDs as status lights, so if the object is as close as a meter, the red LED will turn on; Otherwise the green LED will be on. you can change the red light turn on distance by changing the variable "led_turnon_dist"; By default, it's on 100cm.
Apps:
As you possibly know, you'll need Arduino IDE to compile and upload the codes to the board. Distance will be visible on serial monitor; But you're also able to use processing, a great graphical app to get warned by a colored radar shape. The processing source code is uploaded on this post.
note: You can't use processing and serial monitor at the same time.
object detection
An ultrasonic sensor uses mechanical waves(sound in this case) to measure the distance like a radar; But some objects are better in returning the wave. For example, The sensor is more sensitive to your phone's screen than your pillow. Because the screen of your phone, or laptop, returns the wave way better than a pillow; Actually, weather what you're pillow is made of, it may let the wave continue! enjoy!
Supplies
Arduino UNO or MEGA
SRF04 or 05 ultrasonic sensor
breadboard and jumper wires
green and red LED
Step 1: Download Processing
For best use of the device you make in this tutorial, You better have processing. You can download it for free from this link. Installed it? Let's proceed!
Step 2: Attachments
Power
Arduino GND =====> sensor GND
Arduino GND =====> red LED's negative pin
Arduino GND =====> green LED's negative pin
Arduino 5V =====> sensor VCC
Digital
Digital 8 ===> green LED's positive pin(It'd be better if you put a 200-1k resistor in the path)
Digital 9 ===> red LED's positive pin(It'd be better if you put a 200-1k resistor in the path)
Digital 10 ===> trigger pin on the sensor
Digital 11 ===> echo pin on the sensor
Step 3: Arduino Code
///////////////////////////////////////////////////////////////////////
int trigPin = 10;
int echoPin = 11;
int green_led = 8;
int red_led = 9;
int led_turnon_dist=101;
long duration;
int distance;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(green_pin, OUTPUT);
pinMode(red_pin, OUTPUT);
}
void loop()
{
print_serial();
}
void print_serial()
{
delay(50);
distance = measures_the_distance();
if(distance>=led_turnon_dist)
{
digitalWrite(green_pin, HIGH);
digitalWrite(red_pin, LOW);
Serial.println(distance);
}
else
{
digitalWrite(green_pin, LOW);
digitalWrite(red_pin, HIGH);
Serial.println(distance);
}
}
int measures_the_distance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
return distance;
}
//////////////////////////////////////////////////////////////////
Step 4: Processing Code
note: change port at line 18 to the port you're using.
//////////////////////////////////////////////////////////////
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
Serial myPort;
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup()
{
size (1900,1200);
smooth();
myPort = new Serial(this,"COM6", 9600);
myPort.bufferUntil('.');
}
void draw()
{
fill(98,245,31);
noStroke();
fill(0,4);
rect(0, 0, width, 1010);
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort)
{
data = myPort.readStringUntil('.');
data = data.substring(0,data.length()-1);
index1 = data.indexOf(",");
angle= data.substring(0, index1);
distance= data.substring(index1+1, data.length());
iAngle = int(angle);
iDistance = int(distance);
}
void drawRadar()
{
pushMatrix();
translate(700,700);
noFill();
strokeWeight(2);
stroke(255,245,255);// draws the arc lines
arc(0,0,1800,1800,PI,TWO_PI);
arc(0,0,1400,1400,PI,TWO_PI);
arc(0,0,1000,1000,PI,TWO_PI);
arc(0,0,600,600,PI,TWO_PI);
line(-960,0,960,0);
line(0,0,-960*cos(radians(30)),-960*sin(radians(30)));
line(0,0,-960*cos(radians(60)),-960*sin(radians(60)));
line(0,0,-960*cos(radians(90)),-960*sin(radians(90)));
line(0,0,-960*cos(radians(120)),-960*sin(radians(120)));
line(0,0,-960*cos(radians(150)),-960*sin(radians(150)));
line(-960*cos(radians(30)),0,960,0);
popMatrix();
}
void drawObject()
{
pushMatrix();
translate(700,700);
strokeWeight(9);
stroke(255,0,0);
pixsDistance = iDistance*22.5;
if(iDistance<40)
line(pixsDistance*cos(radians(iAngle)),pixsDistance*sin(radians(iAngle)),950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
popMatrix();
}
void drawLine()
{
pushMatrix();
strokeWeight(9);
stroke(30,10,250);
translate(700,700);
line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
popMatrix();
}
void drawText()
{
pushMatrix();
fill(0,0,0);
noStroke();
rect(0, 1010, width, 600);
textSize(25);
fill(255,255,255);
translate(800,650);
rotate(-radians(-60));
text("30°",0,0);
resetMatrix();
translate(740,600);
rotate(-radians(-30));
text("60°",0,0);
resetMatrix();
translate(680,590);
rotate(radians(0));
text("90°",0,0);
resetMatrix();
translate(620,620);
rotate(radians(-30));
text("120°",0,0);
resetMatrix();
translate(590,680);
rotate(radians(-60));
text("150°",0,0);
resetMatrix();
translate(50,650);
rotate(radians(0));
fill(5,0,0);
text("Soroush YP",2,5);
fill(255,255,255);
text("Soroush YP",0,0);
popMatrix();
}
//////////////////////////////////////////////////////////////////////////
Step 5: Finishing Up
Upload the code to your Arduino. Now you can use your radar via processing or serial monitor and of course status lights!