Introduction: Hiding Box

The "Hiding Box" is a project that I made, to make a secret door in a box to hide objects. This instructable demonstrates how to program an ultrasonic distance sensor to react to movement from a certain distance and to program a servo motor to react and move to the sensor and rotate to a certain degree.

Step 1: Materials

1. Servo Motor

2. Ultrasonic Distance Sensor

3. 11 male wires

4. Breadboard

5. Arduino

6. USB cable

7. Computer

Step 2: Code

#include <Servo.h>

const int echoPin = 4;

const int trigPin = 2;

Servo myservo;

int pos = 0;

void setup() {

myservo.attach(8);

Serial.begin(9600);

pinMode(2, OUTPUT);

pinMode(4, INPUT);

while(! Serial);

Serial.println("speed 0 to 225");

}

void loop() {

long duration, inches;

delay(15);

digitalWrite(2, LOW);

delayMicroseconds(2);

digitalWrite(2, HIGH);

delayMicroseconds(5);

digitalWrite(2, LOW);

duration = pulseIn(4, HIGH);

inches = duration / 74 / 2;

Serial.println(inches);

delay(200);

if (inches < 5)

{

for (pos = 0; pos <= 180; pos += 1)

myservo.write(pos);

delay(15);

}

if (inches > 5)

{

for (pos = 180; pos >= 0; pos -= 1)

myservo.write(pos);

delay(15);

}

}

Step 3: Wiring