Arduino Mad Scientist Laboratory EP# 1 Ultrasonic Tripwire Alarm

SCHEMATIC:

 

PARTS LIST:

ARDUINO NANO

HC-SR04

potentiometer

tip120 Darlington transistor

speaker

ARDUINO CODE:

#define trigPin 9
#define echoPin 10
long duration;
int distance;
int val;

void setup()
{

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop()
{
val = (analogRead(A1));

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//distance = duration * 0.034 / 2; // cm
distance = duration * 0.0133 / 2; // in
val = map(val, 0, 1023, 6, 36);
if (distance <= val)
{

tone(3, 500, 150);
delay(150);
tone(3, 300, 150);
delay(150);

}
}