Introduction: Sonar Alarm

This is a small project that took a suprising amount of time to put together but here it is.

I will not go into detail on how to make it look just as you in the picture, because i simply cant be botherd to do so.

Its just a wooden box with holes where i put the arduino in.

What can you espect??

If you follow this tutorial you will have a sonar detector that will light an led when you get too close, and a rotor where you can place the sonar ontop to move it left or right with the buttons. You can also press the third button to let the rotor spin 180 degrees.

Without further a do lets start buidling.

Requirments:

3: buttons

1: led

1: breadboard

1: sonar detector

1: arduino rotor

1: arduino

Lots of cables

1: 255 resistor

1: brain

Step 1: Step 1

Place your buttons on the board

Step 2: Step 2

Connect the top right part of each button with a cable and connect that cable with the blue section of the board.

put one wire on the 5v hole of the arduino, and connect that with the top red section of the board.

Put one wire on the GND hole of the arduino, and connect that with the top blue section of the board.

Step 3: Step 3

connect the red cable of your rotor to the red section of your board, and connect the black/brown cable of the rotor with the blue section of your board.

Now connect the yellow/orange cable of your rotor to hole 10 of your arduino.

Step 4: Step 4

Connect the red cable of your sensor to the red section of your arduino, do the same with the black cable but put it in the blue section instead. Now connect the trigger cable with hole 13 of your arduino and echopin to hole 9.

Step 5: Step 5

Place a led on the board and connect the smal end of its legs with a cable to the blue section of the board, and place the resitor ajasond to the long leg of the led light, then connect that part with a cable and connect that cable to hole 3 on your arduino.

Step 6: Step 6

Connect the red cable of your buzzer with hole 3 of your arduino, and connect the blac kcable with the blue section of your board.

Step 7: Step 7

Good job! now just copy paste the code bellow and you're done, so hurray.

#include <Time.h>

#include <Time.Lib.h>

#include <Servo.h>

//Always make sure that you are using the right pins for the right button.

#define sw1_pin 6 //Left button

#define sw1_pin 5 //Right button

#define sw1_pin 11 // Rotate button

Servo myservo; // create servo object to control a servo

volatile boolean sw1 = false;
volatile boolean sw2 = false;

volatile boolean sw3 = false;

//Buttons have to start at 0
uint8_t sw1ButtonState = 0;

uint8_t sw2ButtonState = 0;

uint8_t sw3ButtonState = 0;

uint8_t lastsw1ButtonState = 0;
uint8_t lastsw2ButtonState = 0;

uint8_t lastsw3ButtonState = 0;

int timer = 0;
const int trigPin = 13;

const int echoPin = 9;

const int buzzer = 3;

const int ledPin = 12;

long duration;

//MIllis
long interval = 1;

//milisconds long previousMillis = 0;

int increment =1;

int distance;

int safetyDistance;

int pos1 = 0;

int updateInterval;

unsigned long lastUpdate;

void setup()

{
Serial.begin(9600);

while(!Serial) ;

// wait for serial port to connect.

//Sonar

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

//Buzzer

pinMode(buzzer, OUTPUT);

//LedLight

pinMode(ledPin, OUTPUT);

//servo motor

pinMode(sw1_pin, INPUT_PULLUP);

pinMode(sw2_pin, INPUT_PULLUP);

pinMode(sw3_pin, INPUT_PULLUP);

myservo.attach(10); // attaches the servo on pin 9 to the servo object

//CURRENT DATE

setTime(17, 14, 0, 15, 8, 2018);

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(10);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

safetyDistance = distance;
Checkem();

checkIfSw1ButtonIsPressed();

checkIfSw2ButtonIsPressed();

checkIfSw3ButtonIsPressed();

if( sw1){

//RIGHT

Serial.println("sw1");

sw1 = false;

sw3 = false;

myservo.write(20);

delay(15);

}

if( sw2){

//LEFT
Serial.println("sw2");

sw2 = false;

sw3 = false;

myservo.write(170);

delay(15);

}

unsigned long currentMillis = millis();
if(sw3){

//Serial.print("gay");

if(currentMillis - previousMillis > interval) {

// save the last time you blinked the LED

previousMillis = currentMillis;

pos1 += increment;

myservo.write(pos1);

//Serial.println(pos1);

if ((pos1 >= 180) || (pos1 <= 0)) // end of sweep {

// reverse direction

increment = -increment;

}

}

}

} //end update

void Checkem(){
if (safetyDistance <= 25) {

//time_t t = now();

// Store the current time in time

//Serial.print("Current hour: ");

//setTime(t);

//Serial.println(t);

TheTime();

digitalWrite(buzzer, HIGH);

Serial.println("Too close ");

checkDistance();

//printDigits(minute());

digitalWrite(ledPin, HIGH);

}else{

digitalWrite(buzzer, LOW);

digitalWrite(ledPin, LOW);

}

}

void Detach(){
myservo.detach();

}

void printDigits(int digits)
{ //Serial.print(":");

if(digits<0)

Serial.print('0');

Serial.print(digits);

}

void TheTime(){
Serial.print("Intruder detected at: ");

printDigits(hour());

Serial.print(":");

printDigits(minute());

Serial.print(":");

printDigits(second());

Serial.print(" ");

Serial.print(day());

Serial.print("/");

Serial.print(month());

Serial.print("/");

Serial.print(year());

Serial.println();

}

void checkDistance()
{

Serial.print("Distance in cm: ");

Serial.println(distance);

}

void checkIfSw1ButtonIsPressed() {

sw1ButtonState = digitalRead(sw1_pin);

if (sw1ButtonState != lastsw1ButtonState) {

if ( sw1ButtonState == 0) {

sw1=true;

}

delay(50);

}

lastsw1ButtonState = sw1ButtonState;

}

void checkIfSw2ButtonIsPressed(){

sw2ButtonState = digitalRead(sw2_pin);

if (sw2ButtonState != lastsw2ButtonState)

{

if ( sw2ButtonState == 0) {

sw2=true;

}

delay(50);

}

lastsw2ButtonState = sw2ButtonState;

}

void checkIfSw3ButtonIsPressed()
{

sw3ButtonState = digitalRead(sw3_pin);

if (sw3ButtonState != lastsw3ButtonState) {

if ( sw3ButtonState == 0) {

sw3=true; }

delay(50);

}

lastsw3ButtonState = sw3ButtonState;

}