Electric Drop Bolt Lock Code Help

This is supposed to be a simple program that will lock the door if it is closed, (detected by a switch, HIGH when closed) and will unlock the door when the exit button is pushed.

The problem I am having is the arduino will only notice input from one of my two switches. Ex: The exit switch will detect a push but the door closed switch will not notice a HIGH input even if held down forever. What is interesting is that the “L” led on the nano turns green when my exit button is pressed. This does not happen with my door closed switch.

I feel like I am either missing something silly in my logic, or there is a hardware problem. :o

Here is the code:

#include <Bounce2.h>
int relayPin = 12;
int closedPin = 9; // pin the 'door closed' detector is on
int exitPin = 13; // pin for the exit button
int doorWaitTime = 10000; // wait before relocking
boolean doorLocked = false;
unsigned long now;
unsigned long lastTime;
  Bounce debouncer = Bounce();
 Bounce debouncer1 = Bounce(); 
void setup() {
  // put your setup code here, to run once:
  pinMode(exitPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(closedPin, INPUT);
  digitalWrite(relayPin, HIGH);
  Serial.begin(9600);
  debouncer.attach(exitPin);
  debouncer1.interval(50);
  debouncer1.attach(closedPin);
  debouncer.interval(50);
}

void loop() {
  if(doorLocked == false){
    lockDoor();
}
debouncer.update();
int value = debouncer.read();
if( value == HIGH){
    Serial.println("exit button pressed");
    openDoor();
}
}
void openDoor(){
  Serial.println("opening door");
  digitalWrite(relayPin, HIGH);
  doorLocked = false;
  while(1){
    debouncer1.update();
    int value = debouncer1.read();
    if( value == LOW){
      break;
    }
    Serial.println("waiting for door to open...");
    now = millis();
    if (now - lastTime >= doorWaitTime){
      Serial.println("door was not opened -- relocking.");
      digitalWrite(relayPin, LOW);
      doorLocked = true;
      lastTime = now;
      break;
    }    
  }
}
void lockDoor(){
  Serial.println("locking door");
  debouncer1.update();
  int value = debouncer1.read();
  if (value == HIGH && doorLocked == false){
    digitalWrite(relayPin, LOW);
    doorLocked = true;
    Serial.println("door locked");
  }
}

everything is powered on a 12v supply. the 12v supply powers the lock when the relay is closed. The arduino is powered on a 5v converter. The relay is on arduino 5v supply. Changing this to an isolated source does not fix my issue, nor does adding a diode.

nutcrapper:
I feel like I am either missing something silly in my logic, or there is a hardware problem. :o

Exactly, how can we say when we are missing your program code and circuit diagram ??

read the sticky on the first post of every forum about how to use this forum.

especially 6 and 7, about code tags.

then post your code. guessing one switch is hidden from another by an if() statement of some sort ?

ricky101:
Exactly, how can we say when we are missing your program code and circuit diagram ??

I posted this very early in the morning and was tired. It is added now. Thanks for the reply.

You have plenty of Serial prints in there , so all I would do is add some more serial prints after each of the switch debouncer. reads, so you see the switches status, and add some 3 second delays into each of the five main steps you have , then I think you will see when its going wrong as its running.

UPDATE: This program will work flawlessly ONLY if I have my voltage tester between ground and d9. Then it correctly recognizes all button presses. I also noticed that if I remove my tester from d9 and leave it connected to ground, then touch the not connected tester wire with my hand, it replicates random button presses on my door closed switch. Is this bad ground or something? I have never faced this random behavior.

You need to post a schematic including details of your power supply, pull up/down resistor values and length of cable between the nano and your door switches.

You say a voltage tester, do you mean a digital multimeter on the DC volts setting, which I would not have thought would have affected the pin in such a way.

Again, I would use the code to establish if the hardware is working ok, just use the more basic code like the Examples , Button to see if each switch works correctly.

If you suspect power rail problems, test things out on a battery.

It seems like all I needed was pull down resistors. I guess due the the length of the bundled cable I was using, interference was a big issue.

Especially if you have long wires from your Arduino (or any other processor), it is a good idea to not only have pull up/down as appropriate resistors, but have some sort of signal conditioning/protection on the input to the processor - even a simple 4r7k resistor in series with the input pin and a .1ufd cap from the input to ground will help eliminate stray spikes etc wandering around (depending on response time needed, I would probably even go with a larger cap - 1ufd or so). The trick is to try and make your signal as clean as possible into the processor as well as protecting the processor inputs from static, emp (from being near other wires with switched current in them etc.)