Hiding Box

by alexk944 in Circuits > Sensors

324 Views, 2 Favorites, 0 Comments

Hiding Box

WIN_20171031_11_46_46_Pro.jpg

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.

Materials

1. Servo Motor

2. Ultrasonic Distance Sensor

3. 11 male wires

4. Breadboard

5. Arduino

6. USB cable

7. Computer

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);

}

}

Wiring

WIN_20171101_11_37_31_Pro.jpg