Using the Delay Function

I'm trying to use the "delay" function to wait for a specified time before activating a relay. Please see below:

if (digitalRead (Pressure) == LOW && digitalRead (Door) == HIGH) //when the pressure switch is active and the car door is open; turn on relay to sound horn.
{
delay(8000); // wait for for some specified time before truning on relay
digitalWrite(LED, HIGH);
digitalWrite(relay, HIGH); // turn the relay on(HIGH is the voltage level)

The delay does not seem to be working and the relay is activated the moment I press the button. How do I insert a delay before sounding my horn

Please post ALL your code.
Use code tags.

Sorry, I'm not sure what code tags are as I'm new to this.

int Pressure=8;     
int Door=9;     
int LED=13; 
int Relay=10; 
int Motion=11; 
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(Pressure, INPUT); 
  pinMode(Relay, OUTPUT); 
  pinMode(Door, INPUT); 
  pinMode(Motion, INPUT); 
}

void loop()
{  
 noInterrupts();
 if (digitalRead (Pressure) == LOW && digitalRead (Door) == HIGH || digitalRead (Motion) == HIGH)  
     
  {
   // delay(1000000);               // wait for for some specified time before truning on relay
    digitalWrite(LED, HIGH); 
    digitalWrite(Relay, HIGH);   
  }
  
  else if (digitalRead (Pressure) == HIGH || digitalRead (Door) == LOW || digitalRead (Motion) == LOW) 
  {
    digitalWrite(Relay, LOW);   
    digitalWrite(LED, LOW); 
  }

}

Sorry, I'm not sure what code tags are as I'm new to this

That's why we provide a handy "How to use this forum" post at the top of nearly every section of the forum.

The delay does not seem to be working

Because it isn't there.

if (digitalRead (Pressure) == LOW && digitalRead (Door) == HIGH || digitalRead (Motion) == HIGH) 
     
  {
   // delay(1000000);
noInterrupts();

That's going to be problematical.

Wow!! This was not the response I was hoping for. I'm simply attempting to help my 12 year old and thought this forum would be a helpful resource when he got stuck, which he is. Instead what I've received are snotty remarks and no real answers. Thanks.

AWOL gave you the answers.
Code Tags, read # 7 http://forum.arduino.cc/index.php/topic,148850.0.html
// delay(1000000); <<<<< You have this line commented out, remove the //
noInterrupts(); <<<<< With no interrupts there can be no delay

brownca:
Instead what I've received are snotty remarks and no real answers. Thanks.

As has been pointed out to you, you DID get answers. As for the "snotty remarks", there were none. Folks that help others on this forum do so without compensation. It is well within their right to ask that you follow some simple guidelines that are posted at the top of the forum. We would like you to do this because it is easier to see the programs when you post them correctly, and after all, the easier it is to read your code, the easier it is for us to see your problem.

But if you aren't even willing to post your code correctly, and moreover, are not willing to take the answers given as anything other than snotty remarks, you might want to consider the reaction of those who are, at least for now, willing to help.

In the OP's defence, the delay() wasn't commented out in the snippet in the first post.

Here's a tip though @brownca.....

Use Serial.begin(9600); in setup() and then Serial.println("here I am"); kind of messages strategically sprinkled into the code so you can see where the flow is going. It might be, for example, that the reason your delay() isn't working (assuming it's not commented out 8) ) is that the logic isn't taking you there, even though you think it is. A print to the monitor, which you turn on with ctrl/shift/m in the ide, will prove that it's going there or not.

(Although in your case it seems that's the only place the horn gets turned on.....)

Edit.... the code tags are turned on with the # button above the :wink: and :sweat_smile: smilies- select the code you type in, hit #, and it will appear

like this

We ask to see all the code because the fault may lie outside of the snippet you have posted (like, for instance, not setting the correct pinMode for a pin, or leaving an input floating, or powering a LED via a pullup resistor, or stepping outside the bounds of an array - common noob mistakes)

We ask that you post in code tags so that we can see all your code as the compiler sees it - the forum software has a nasty habit of turning C subscripts into text formatting, like italics, and simple punctuation into smilies, so it can be hard for others to try to replicate your problem.

We don't ask these things just to be picky; they make finding problems easier and quicker, and posts that don't adhere to the guidelines are more likely to be ignored, because they're just not worth the effort.

I should not have taken AWOL's comments as "snotty" but it's difficult to sense tone from posts and I read more into it than I should have. In fairness I should have read the instructions and I did not, lesson learned.

I understand all that's been said and thanks to all for the suggestions.

The issue I'm having with the delay is when is that it does delay the activation of the horn but it also keeps the horn sounding for an equal amount of time without the button being pressed.

Again, thanks to you all for the comments.

OK, so post your code, describe what it does and what you expect it to do.
If necessary post a simple schematic.

brownca:
The issue I'm having with the delay is when is that it does delay the activation of the horn but it also keeps the horn sounding for an equal amount of time without the button being pressed.

OK, first on the list is to get rid of noInterrupts();, Just comment it out.

Then, put the delay() back in, but make is a reasonable value to test, like say, 2000 (2 seconds).

Now, have a look at your conditions.

  if (digitalRead (Pressure) == LOW && digitalRead (Door) == HIGH || digitalRead (Motion) == HIGH)

I can never remember what the order of precedence for && and || are. is the above line parsed as

if (pressure == low && door == HIGH) || motion == HIGH) ?

or is it parsed as

if pressure == low && (door == HIGH || motion == HIGH) ?

If you use parentheses to clarify it to yourself (as I have done here), the compiler will not complain, and will compile for the meaning you want.

From the sense of the active states of the switches, I gather you have them wired from +5V to the appropriate pins. If this is the case, you need to pull down the pins with resistors (10K ohms is a reasonable value). The resistors should be connected from GND to the same appropriate pins. If you don't have resistors, the pins will 'float', and will be extremely susceptible to noise, causing random sensing of HIGH or LOW during digital reads.

You can do this without external resistors by simply connecting your switches between GND and the appropriate pins, then use INPUT_PULLUP in your pinMode statements. If you do this, you will have to then change the sense of the switch states. Every one that now reads HIGH should be changed to LOW, and every LOW should be changed to HIGH.

Look over the conditions carefully. Follow the logic of each condition. Ask yourself what happens if a condition changes during the execution of a block of code within the if.

Look over the conditions carefully. Follow the logic of each condition. Ask yourself what happens if a condition changes during the execution of a block of code within the if.

.... and use Serial.print to let you see where the flow actually took you 8)

I will try the modifications when my son gets home. Thanks for the suggestions.

Sorry, just now getting around to this. The delay works however, it also allows the siren to sound for the time of the delay (60s) even though the conditions for sounding the siren are no longer met.

You'll need to show your current code. But I suspect you have some sequence like this...

Check conditions
Turn on
Do a delay
Turn off

In that case, there's no way to test the conditions while the delay is running... it's known as "blocking" since it literally sits and does nothing during the delay. So if that was in loop() for example, it will only get back to the top to check conditions after the delay is over.

Look at BlinkWithoutDelay example in the IDE: FIle> Examples>....