Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Webotricks
Published

Make a simple radar system with Arduino UNO R4 WIFI board

In this project, we will learn how to make a simple radar system with Arduino UNO R4 WIFI board.

BeginnerProtip106
Make a simple radar system with Arduino UNO R4 WIFI board

Things used in this project

Hardware components

Ultrasonic Sensor
×1
Breadboard
×1
Servo Motor (SG90)
×1
Jumper Wires
×1

Story

Read more

Code

Code

C/C++
#include <Servo.h>//include library
Servo myServo;//create object your own name
int dis;
#include "thingProperties.h"
 
void setup() {
  pinMode(3, OUTPUT);//define arduino pin
  pinMode(2, INPUT);//define arduino pin
  myServo.attach(9);//servo connect pin
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
 
  // Defined in thingProperties.h
  initProperties();
 
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}
 
void loop() {
  // Your code here 
  for (int x = 0; x <= 180; x++) { //servo turn left
    myServo.write(x);//rotete servo
    range=distance();  
    Serial.print(x);//print servo angle
    Serial.print(",");
    Serial.print(range);//print ultrasonic readings
    Serial.print(".");
    if(range < 30){
      status = true;      
      }else{
        status = false;
    }
    ArduinoCloud.update();
    delay(20);
  }
  for (int y = 179; y > 0; y--) {//servo turn right
    myServo.write(y);//rotete servo
    range=distance(); 
    Serial.print(y);////print servo angle
    Serial.print(",");
    Serial.print(range);//print ultrasonic readings
    Serial.print(".");
    if(range < 30){
      status = true;      
      }else{
        status = false;
    }
    ArduinoCloud.update();
    delay(20);
  }
 
range = distance();
Serial.println(range);
delay(10);
 
  
}
 
 
//ultrasonic sensor code
int distance() {
  digitalWrite(3, LOW);
  delayMicroseconds(4);
  digitalWrite(3, HIGH);
  delayMicroseconds(10);
  digitalWrite(3, LOW);
 
 
  int t = pulseIn(2, HIGH);
  int cm = t / 29 / 2; //time convert distance
  return cm;//return value
}

Code

C/C++
	import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
 
Serial myport;  // create serial object
PFont f;
int Angle, Distance;
String angle = "";
String distance = "";
String data;
int index1 = 0;
int index2 = 0;
float pixsDistance;
float sweepAngle = 0; // Angle for radar sweep effect
float sweepSpeed = 0.5; // Speed of the sweep
 
// Additional variables for sensor data
String status = "Idle";  // Status of radar (Idle, Scanning)
String detectionStatus = "No Object"; // "Object Detected" or "No Object"
String dateTime = "";  // To hold current date/time
int targetCount = 0;  // Count of detected objects
 
void setup() {
  size(1300, 700);  // screen size
  smooth();
  f = createFont("David Bold", 20);  // font name and size
  textFont(f);
 
  String portName = "*******";  // set COM port
  myport = new Serial(this, portName, 9600);
  myport.bufferUntil('.');  // Expect data to end with a period (.)
}
 
void draw() {
  noStroke();
  fill(0, 10);  // Light background fill
  rect(0, 0, width, height);  // Clear the screen each frame
  fill(98, 245, 31);
  
  // Draw radar area
  greenmesh();
  radararea();
  words();
  
  // Draw the green line and red line
  greenLine();
  redline();
  
  // Add a sweeping effect to simulate radar scanning
  sweepEffect();
  
  // Display additional info
  displayAdditionalInfo();
}
 
// Get Arduino board serial values
void serialEvent(Serial myport) {
  data = myport.readStringUntil('.');
  if (data != null) {
    data = data.substring(0, data.length() - 1);  // Remove the period
    index1 = data.indexOf(",");
    angle = data.substring(0, index1);
    distance = data.substring(index1 + 1, data.length());
 
    // Converts the String variables into Integer
    Angle = int(angle);
    Distance = int(distance);
    
    // Update status
    status = "Scanning";
    
    // Update detection status based on distance
    if (Distance < 50) {
      detectionStatus = "Object Detected";
      targetCount++;
    } else {
      detectionStatus = "No Object";
    }
    
    // Update the current date and time
    dateTime = year() + "/" + nf(month(), 2) + "/" + nf(day(), 2) + " " + nf(hour(), 2) + ":" + nf(minute(), 2) + ":" + nf(second(), 2);
  }
}
 
// Half circle and lines (arc, angles)
void radararea() {
  pushMatrix();
  translate(625, 680);  // Move to center of the screen
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
 
  // Draw arcs (distance rings)
  arc(0, 0, 1150, 1150, PI, TWO_PI);
  arc(0, 0, 850, 850, PI, TWO_PI);
  arc(0, 0, 550, 550, PI, TWO_PI);
  arc(0, 0, 250, 250, PI, TWO_PI);
 
  // Draw angle lines (sector lines)
  line(-450, 0, 450, 0);  // 180°
  for (int i = 30; i <= 150; i += 30) {
    line(0, 0, -600 * cos(radians(i)), -600 * sin(radians(i)));  // Draw each angle line
  }
  popMatrix();
}
 
// Green grid mesh
void greenmesh() {
  stroke(98, 245, 31);
  strokeWeight(0.1);
  for (int x = 0; x <= 700; x += 5) {
    line(0, x, width, x);
  }
  for (int y = 0; y <= 1300; y += 5) {
    line(y, 0, y, height);
  }
}
 
// Display text on screen
void words() {
  fill(98, 245, 31);
  text("180'", 10, 670);
  fill(98, 245, 31);
  text("0'", 1210, 670);
  fill(98, 245, 31);
  text("30'", 1160, 380);
  fill(98, 245, 31);
  text("60'", 940, 160);
  fill(98, 245, 31);
  text("90'", 615, 70);
  fill(98, 245, 31);
  text("120'", 310, 150);
  fill(98, 245, 31);
  text("150'", 80, 370);
  fill(255);
  text("SriTu Hobby Radar system", 20, 30);
  fill(255);
  text("Angle -- " + Angle + " '", 20, 60);
  fill(255);
  text("Distance -- " + Distance + " cm", 20, 90);
}
 
// Green line (angle representation)
void greenLine() {
  pushMatrix();
  strokeWeight(7);
  stroke(30, 250, 60);  // green color
  translate(625, 680);
  line(0, 0, 600 * cos(radians(Angle)), -600 * sin(radians(Angle)));
  popMatrix();
}
 
// Red line (distance representation)
void redline() {
  pushMatrix();
  translate(625, 680);
  strokeWeight(7);
 
  // Dynamically adjust the color of the red line based on distance
  if (Distance < 20) {
    stroke(0, 255, 0);  // Green if within 20cm
  } else if (Distance < 50) {
    stroke(255, 10, 10);  // Yellow if within 50cm
  } else {
    stroke(255, 255, 0);  // Red for longer distances
  }
 
  pixsDistance = Distance * 22.5;  // Scale the distance
  
  // Draw the red line based on the scaled distance
  line(pixsDistance * cos(radians(Angle)), -pixsDistance * sin(radians(Angle)), 
       600 * cos(radians(Angle)), -600 * sin(radians(Angle)));
  popMatrix();
}
 
// Radar sweep effect
void sweepEffect() {
  pushMatrix();
  translate(625, 680);
  strokeWeight(2);
  stroke(255, 255, 255, 150);  // White color for the sweep effect
  
  // Calculate the current position of the sweeping line
  sweepAngle += sweepSpeed;
  if (sweepAngle > 180) {
    sweepAngle = 0;  // Reset the sweep angle after one full sweep
  }
 
  // Draw the sweeping line
  line(0, 0, 600 * cos(radians(sweepAngle)), -600 * sin(radians(sweepAngle)));
  popMatrix();
}
 
// Display additional data
void displayAdditionalInfo() {
  fill(255);
  text("Status: " + status, 950, 40);
  text("Detection: " + detectionStatus, 950, 70);
  text("Date/Time: " + dateTime, 950, 100);
  text("Target Count: " + targetCount, 950, 130);
}

Credits

Webotricks
24 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.