I am using a solenoid for a lock, and need it to start a timer when opened, so that it will close when that timer expires. This is the code I am using, without any sort of timer.
#define lock 12
#define sound 3
boolean trig = true;
boolean state = true;
void setup() {
// put your setup code here, to run once:
pinMode(lock, OUTPUT);
pinMode(sound, INPUT);
}
void loop() {
// Checks if the button state has change
trig = digitalRead(sound);
if (trig == false && state == false)
{
// Turns the Lock on
digitalWrite(lock, HIGH);
state = true;
delay(900);
}
else if (trig == false && state == true)
{
// Turns the Lock off
digitalWrite(lock, LOW);
state = false;
delay(900);
}
}
If you want to do nothing for a certain amount of time, delay() is the easiest solution.
What's wrong with that?
I feel the it has become a tradition in this forum to advise against using delay() for no good reason.
couka:
If you want to do nothing for a certain amount of time, delay() is the easiest solution.
What's wrong with that?
I feel the it has become a tradition in this forum to advise against using delay() for no good reason.
Advising against use of delay is just shorthand for a much longer lecture that ends with:
"So as you can see, delay is best avoided until you've got more experience"
Of course there are appropriate uses for it, but once you've seen fifty threads where someone is struggling with a non-responsive program because their sketch has tens of seconds of delays in it, that shorthand comes in handy.
Also, unless you're in the unusual position of having rock solid requirements, use of delay can mean a rewrite when you decide that the system would be better if it responded to a couple of buttons as well as what it does now.