Using Timer.h inside a SWITCH

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;

}
}

Please use code tags </> to present code. You can Edit your post accordingly.

Read about doing "several things at a time". All you need is millis(), for writing time-based code.
Later on understanding a "state machine" (automaton) may be helpful.

Try to get your logic and commands right. If you use the Serial Monitor to steer the Arduino, remember that you send characters (e.g. '1'), not integer values.

  1. You need to edit your post and place the code [code]within code tags[/code].

  2. You say "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.", yet the code you've shown doesn't attempt to use the "Timer" library functions.

  3. As mentioned by DrDiettrich, you will be receiving ASCII characters, not integers.
    ie When a '1' is sent, the value you receive will be 0x31, not 0x1, so in your case statements you would need either:-

switch(serialA)
{
    case 0x31:
.
.
.
}

or:-

switch(serialA)
{
    case '1':
.
.
.
}

but not:-

switch(serialA)
{
    case 1:
.
.
.
}