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

Two-in-One Radar System

This system can be used as both a backup radar and a collision warning system.

BeginnerFull instructions provided878
Two-in-One Radar System

Things used in this project

Story

Read more

Schematics

wechatimg64_qKXeZMsklO.jpeg

Code

Source Code

C/C++
#include "Ultrasonic.h"
#define BUZZER_PIN               39            /* sig pin of the Grove Buzzer */
#define ULTRASONIC_PIN           38            /* pin of the Ultrasonic Ranger */


Ultrasonic ultrasonic(ULTRASONIC_PIN);    /* Ultrasonic Ranger object */
int distance = 0;                         /* variable to store the distance to obstacles in front */
int tempo = 200;

void setup() {
  // put your setup code here, to run once:
/* set buzzer pin as output */
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  distance = ultrasonic.MeasureInCentimeters();   /* read the value from the sensor */ 
  if (distance<30)
  {
    tempo=100;
    playTone(1200,tempo);
  }
  else if(distance<50)
  {
    tempo=200;
    playTone(1400,tempo);
  }
  else if (distance<80)
  {
    tempo=300;
    playTone(1500,tempo);
  }
  else if (distance<100)
  {
    tempo=400;
    playTone(1700,tempo);
  }
  else if (distance<150)
  {
    tempo=500;
    playTone(1900,tempo);
  }
  delay(tempo / 2);
}

/* play tone */
void playTone(int tone, int duration) 
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) 
  {
    digitalWrite(BUZZER_PIN, HIGH);
    delayMicroseconds(tone);
    digitalWrite(BUZZER_PIN, LOW);
    delayMicroseconds(tone);
  }
}

Credits

Gary Li
1 project • 0 followers
Contact
Winnie Li
1 project • 1 follower
Contact
Yiyang Xu
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.