Alex Wulff
Published © CC BY-NC-SA

Secret Door Opener

Open your door with a few simple taps; no keys needed!

BeginnerFull instructions provided1.5 hours12,778

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
DFRobot Stepper Motor
×1
DFRobot Stepper Driver
×1
DFRobot Buzzer
×1
DFRobot Capacitive Sensor
×1
DFRobot 12V Adapter
×1
Jumpers
×1
Mounting Tape
×1
Copper Tape
×1

Story

Read more

Code

Door_Opener

Arduino
const int sensorPin     = 3;
const int soundPin      = 10;
const int pulsePin      = 7;
const int directionPin  = 6;
const int enablePin     = 5;

const int numPresses = 5;
const int motorStepDistance = 12000;
const int thresh = 100;
const int volume = 5;

int pressIndex = 0;
bool haveResetCount = false;
int pressTimes[numPresses];
int correctPressTimes[] = {100, 100, 100, 100, 100};

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(soundPin, OUTPUT);
  pinMode(pulsePin, OUTPUT);
  pinMode(directionPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {
  long loopStartTime = millis();
  
  while(!digitalRead(sensorPin)) {
    // Resets after 10 seconds
    if (millis() - loopStartTime > 10000 && !haveResetCount) {
      pressIndex = 0;
      haveResetCount = true;

      for (int i = 0; i < 3; i++) {
        analogWrite(soundPin, volume);
        delay(75);
        digitalWrite(soundPin, LOW);
        delay(75);
      }
    }
  }

  long pressStartTime = millis();
  
  while(digitalRead(sensorPin)) {
    analogWrite(soundPin, volume);
  }

  digitalWrite(soundPin, LOW);

  int pressTime = millis() - pressStartTime;
  pressTimes[pressIndex] = pressTime;
  haveResetCount = false;
  pressIndex++;

  if (pressIndex == numPresses) {
    bool stillCorrect = true;
    
    for (int i = 0; i < 4; i++) {
      if (abs(correctPressTimes[i]-pressTimes[i]) > thresh) {
        stillCorrect = false;
      }
    }

    if (!stillCorrect) {
      for (int i = 0; i < 20; i++) {
        analogWrite(soundPin, volume);
        delay(20);
        digitalWrite(soundPin, LOW);
        delay(20);
      }
    }

    else {
      for (int i = 0; i < 5; i++) {
        analogWrite(soundPin, volume);
        delay(100);
        digitalWrite(soundPin, LOW);
        delay(100);
      }

      unlock();
    }

    pressIndex = 0;
    haveResetCount = true;
  }
}

void unlock() {
  digitalWrite(directionPin,HIGH);
  digitalWrite(enablePin,HIGH);
    
  for (int i = 0; i < motorStepDistance; i++) {
    digitalWrite(pulsePin,HIGH);
    delayMicroseconds(50);
    digitalWrite(pulsePin,LOW);
    delayMicroseconds(50);
  }

  delay(5000);
  digitalWrite(directionPin,LOW);
  
  for (int i=0; i < motorStepDistance; i++) {
    digitalWrite(pulsePin,HIGH);
    delayMicroseconds(50);
    digitalWrite(pulsePin,LOW);
    delayMicroseconds(50);
  }
  
  digitalWrite(enablePin, LOW);
}

Credits

Alex Wulff

Alex Wulff

12 projects • 227 followers
I'm a maker and student at Harvard. I love Arduino, embedded systems, radio, 3D printing, and iOS development. www.AlexWulff.com

Comments