Hi everybody!
I've been working on a project to open and close a drawer's lock, using a solenoid lock and an arduino nano with a bluetooth module via a simple android app. The drawer also has a push button that gets pressed when the door is closed, so that (the app) wont let you close the lock if the door is open.
So far so good, but i haven't yet managed to solve the following issue:
I need the circuit to automatically close the lock (by-passing the app orders) after "X" seconds that it got opened. So i've been playing with Timer.h library (with t.pulse() for example) and millis(), but i can't get them to work inside the SWITCH. The ideal scenario would be like: if "case 1" happens (lock gets open), then close the lock after "X" seconds, unless the lock gets closed before from the app.
Any thoughts?
Cheers!
Iggy
The code:
// Solenoid Lock + Bluetooth door proyect
// Open a door using an app and Bluetooth module
// Detect when the door is either open or closed as a condition for closing the lock
// Automatic lock down after “X” seconds, using Timer library
#include "Timer.h"
Timer t;
const int lock = 2; //solenoid lock
const int openLED = 3; //green LED
const int closedLED = 4; //red LED
const int state = 5; // button that reads if the door is closed (high) of open (low)
int sensorValue = 0; // int for the app to know if door is open or closed given pin5
byte serialA;
void setup() // red LED on, green LED off, Lock closed (deactivated)
{
Serial.begin(115200);
pinMode(lock, OUTPUT);
pinMode(openLED, OUTPUT);
pinMode(closedLED, OUTPUT);
pinMode(state, INPUT);
digitalWrite(closedLED, HIGH);
}
void loop() // 2 methods running in loop, the door state verification and the lock operator
{
verification();
operator();
}
void verification()
{
sensorValue = digitalRead(state);
Serial.println(sensorValue);
delay(1000);
}
void operator() //2 main states: opened or closed door.
{
if (Serial.available() > 0)
{
serialA = Serial.read();Serial.println(serialA);
}
switch (serialA) {
//STATE 1: DOOR GETS OPEN
case 1:
digitalWrite(lock, HIGH); //lock activated (opened)
break;
case 3:
digitalWrite(openLED, HIGH); //green LED on
break;
case 6:
digitalWrite(closedLED, LOW); // red LED off
break;
//STATE 2: DOOR GETS CLOSED
case 2:
digitalWrite(lock, LOW); //lock closed
break;
case 4:
digitalWrite(openLED, LOW); //green LED off
break;
case 5:
digitalWrite(closedLED, HIGH); //red LED on
break;
}
}