program stops when using a timer function

Hi,
I made a small home control system.
One of the functions is that when I push one button, all the outputs are set LOW (all lights out), expect one output ( light in the hall) that goes HIGH during 5 minutes, so that I can leave the house on a safe way.
This function works good, except that during the time that the timer of 5 minutes is active, the arduino refuses all the other actions.
I use this subroutine for the timer function :

void tijdrelais()
// light HALL goes on for 5 minutes to lease the house
{
starttijd=millis();
while(millis() < starttijd+300000)
{ digitalWrite(Relay_9,RELAY_ON); state9 =1;
}
digitalWrite(Relay_9,RELAY_OFF); state9 =0;
}
I specially used the millis() function in case of the the delay function to avoid that the arduino should be blocked during the timer function.

All hands on the ouija board...

the arduino refuses all the other actions.

What other actions? What do you mean it refuses them?

  while(millis() < starttijd + 300000)
  { 
    digitalWrite(Relay_9,RELAY_ON); 
    state9 =1;
  }

You have told it to turn the relay on many, many, times for 5 minutes. Of course it doesn’t do anything else. What is RELAY_ON anyway?

The problem is that during the 5 minutes that the timer is active, I cannot put some light back ON again. When I push another button, the arduino gives no reaction anymore during that time.

Could that be because you're using "delay()"?

Have a look at the blink without delay example to see how to react in a more timely fashion.

Replace 300000 by 5L * 60 * 1000 so that the constant is
long (32 bits) and so its obvious its 5 minutes.

Replace

  while(millis() < starttijd + 300000)
  {
    ..
  }

with

  if (millis() - starttijd < 5L * 60 * 1000)
  {
    ..
  }

While loops will obvious jam up the system - loop is already looped, you don’t need
to do it again.

You should do a subtraction between the current time and the start time, and then
compare, or it won’t work when millis() wraps round. Even so your code must
then update starttijd for next time. Here you have a one-off event so you’ll really
need a boolean guard variable as well as a timeout.

Until you post all your code its hard to say more.

I specially used the millis() function in case of the the delay function to avoid that the arduino should be blocked during the timer function.

But doesn’t this block?

 while(millis() < starttijd + 300000)
  { 
    digitalWrite(Relay_9,RELAY_ON); 
    state9 =1;
  }

Read this:

http://www.gammon.com.au/blink

Thanks a lot. I wil modify my sketch with this information.

guidoc:
while(millis() < starttijd+300000)
{ digitalWrite(Relay_9,RELAY_ON); state9 =1;
}

Hi Guidoc.
You are instructing this (in Dutch):

Zolang (millis() < starttijd+300000)
Voer wat tussen { } staat uit (en niets anders).

So the while will not exit execution of what’s in the { } brackets, until the condition (millis() < starttijd = 300000) is met.
That is blocking any other instruction.

I modified my code I hope it would work.

void afsluiten()
// alle lichten worden gedoofd, uitgezonderd HALL
// All light OFF excepts the light in the hall that goes on for 5 minutes
{
starttijd=millis(); // aanpassing 23/11/2013
digitalWrite(Relay_1,RELAY_OFF); state1=0; state1b=0;
digitalWrite(Relay_2,RELAY_OFF); state2=0; state2b=0;
digitalWrite(Relay_3,RELAY_OFF); state3=0; state3b=0;
digitalWrite(Relay_4,RELAY_OFF); state4=0;
digitalWrite(Relay_5,RELAY_OFF); state5=0;
digitalWrite(Relay_6,RELAY_OFF); state6=0;
digitalWrite(Relay_7,RELAY_OFF); state7=0;
digitalWrite(Relay_8,RELAY_OFF); state8=0;
digitalWrite(Relay_9,RELAY_ON); state9=1;// Hall aanpassing 23/11/2013
digitalWrite(Relay_10,RELAY_OFF); state10=0;state9b=0;
digitalWrite(Relay_11,RELAY_OFF); state11=0;state11b=0;
digitalWrite(Relay_12,RELAY_OFF); state12=0;
digitalWrite(Relay_13,RELAY_OFF); state13=0;
digitalWrite(Relay_14,RELAY_OFF); state14=0;
digitalWrite(Relay_15,RELAY_OFF); state15=0;
//digitalWrite(Relay_16,RELAY_OFF); state16=0; //doorgang
digitalWrite(FET1,0);
digitalWrite(FET2,0);
digitalWrite(FET3,0);
digitalWrite(FET4,0);
digitalWrite(FET5,0);
digitalWrite(FET6,0);

for (int i=0;i<=7;i++)
{
ArduinoDmx0.TxBuffer = 0; // alle dmx-kanalen uitzetten

  • }*

}
void tijdrelais()
// verlichting blijft 5 minuten nabranden.
// light in the Hall goes OUT after 5 minutes
{

_ if (millis() - starttijd > 5L * 60 * 1000)_

  • { digitalWrite(Relay_9,RELAY_OFF); state9 =0;*
  • }*

}[/color]

I will upload the sketch tomorrow
MAS3, inderdaad mijn programma is in het nederlands geschreven. Waarschijnlijk niet zo slim van mij.

Please use code tags.

Read this before posting a programming question

This is the whole code.
Excuse me but I’m not a programmer, so I think the code can be optimize a lot.
Inputs are all pushbuttons.
Outputs are a mix between Relays for 230 Vac, Fet’s for 12 Vdc.
To have more outputs I used DMX controllers.
Here is still some work to do. The DMX controller blocks the serial monitor for the moment.

Final6_ino.ino (17.4 KB)