Introduction
We used the components from previous tutorials to create a radar.
Components
- Arduino UNO or similar
- Breadboard
- Jumper Wires
- LED
- Resistor
- Servo motor
- Ultrasonic Sensor
We combined the circuits from previous tutorials. Our circuit looks similar to the diagram below:
Arduino
Code explanation
Setup
We define the nesessary variables and objects and initialize the objects just like in previous lessons.
Loop function
In the loop()
function we sweep the servo and perform the measurements. To avoid having duplicate code, we call another function.
Measure and send
In the measureDistance()
function we
- Set the servo angle
- Measure the ultrasonic distance
- Turn LED on/off according to the distance
- Send the current angle and distance to Serial
Processing
Code explanation
Draw Radar Outline
drawRadar()
This function draws the arcs and lines for the radar. We also print the angles and distances.
Draw Scanning Line
drawCurrentScanningLine()
Draws the scanning line for the current angle.
Draw Detected object
drawRadar()
If an object is detected a line is drawn from the corrent distance on the radar.
Draw Information
drawCurrentScanningLine()
The current angle and distance are printed.
Fade
drawCurrentScanningLine()
Draws a semi-transparent black background to hide old values.
Push/Pop Matrix
pushMatrix()
saves current transformation.
popMatrix()
restores the last transformation.
Translate
translate(x, y)
moves the coordinate system
The coordinate system in Processing begins in the top-left corner of the screen. The x
value increases to the right. The y
value increases to the bottom.
Radar Running
Exercise
Display a text when an object is currently detected.
Challenge
Add any functionality to the radar or build something creative yourself!