Blink an led during an If or else statement, Is it possible?

Below is the code I'm mashing together for a garage door indicator light system. So far I have the power status light fading, and 2 magnetic switch inputs that turn on an led when the circuit is broken (door open state) So far all is good but I have had no luck in getting the door indicator leds to blink instead of just staying on when the door is open. I would like to have the blink rate be adjustable as needed. I hope this is possible and Thank you in advance for any and all help.

// Door status indicaator
// This is to monitor the Garage door, back door and gate
//open or closed status

int Power_on_indicator_led_pin = 3; // Shows the system is on
int brightness = 0;
int fadeAmount = 5;
int Door_Led_Pin = 7; // choose the pin for the LED ///BIG garage door///
int Door_Sensor_Pin = 5; // choose the Door_Sensor_Pin
int val = 0; // variable for reading the Door_Sensor_Pin status

int Door2_Led_Pin = 8; // Choose the pin for the LED ///Small garage door//
int Door2_Sensor_Pin = 6; // Choose the Door_Sensor_Pin
int val2 = 0; // Variable for reading the Door_Sensor_Pin status

void setup() {
pinMode (Power_on_indicator_led_pin, OUTPUT); // Power status led pin set as output
pinMode(Door_Led_Pin, OUTPUT); // declare Door_Led_Pin as output ///BIG garage door/////
pinMode(Door_Sensor_Pin, INPUT); // declare Door_Sensor_Pin as input

pinMode(Door2_Led_Pin, OUTPUT); // declare Door_Led_Pin as output ///Small garage door////
pinMode(Door_Sensor_Pin, INPUT); // declare Door_Sensor_pin as input

}

void loop(){
analogWrite(Power_on_indicator_led_pin, brightness); // Power indicator fade code
brightness = brightness + fadeAmount;

if (brightness == 0 | brightness == 255) {
fadeAmount = -fadeAmount;
}
delay(30);

val = digitalRead(Door_Sensor_Pin); // read Door_Sensor_Pin ///BIG garage door////
if (val == HIGH) { // If Door_Sensor N.C. (no with magnet) -> HIGH : Door is open / LOW : Door is closed
// If Door_Sensor N.0. (nc with magnet) -> HIGH : Door is closed / LOW : Door is open
digitalWrite(Door_Led_Pin, LOW); //Set Door_Led low
} else {
digitalWrite(Door_Led_Pin, HIGH); //Set Door_Led high
}

val2 = digitalRead(Door2_Sensor_Pin); // read Door2_Sensor_Pin ///Small garage door/////
if (val2 == HIGH) { // If Door_Sensor N.C. (no with magnet) -> HIGH : Door is open / LOW : Door is closed
// If Door_Sensor N.0. (nc with magnet) -> HIGH : Door is closed / LOW : Door is open
digitalWrite(Door2_Led_Pin, LOW); // Set Door_Led low
} else {
digitalWrite(Door2_Led_Pin, HIGH); //set Door_Led high
}
}

Put { and } on separate lines

Use CTRL T to format your code

| is not the same as ||

Post your code with the </> icon in the Quick Reply menu

It's very confusing to read your code/comments when you cover both of the following possibilities. Which is true? :-

if (val == HIGH) {         // If Door_Sensor N.C. (no with magnet) -> HIGH : Door is open / LOW : Door is closed
                             // If Door_Sensor N.0. (nc with magnet) -> HIGH : Door is closed / LOW : Door is open

Is it normally closed (with magnet) or is it normally open (with magnet)? ie Is it HIGH when the door is open or when it's closed? Can't be both.

Same goes for this:-

if (val2 == HIGH) {       // If Door_Sensor N.C. (no with magnet) -> HIGH : Door is open / LOW : Door is closed
                            // If Door_Sensor N.0. (nc with magnet) -> HIGH : Door is closed / LOW : Door is open

And can we assume that your LEDs are active-high? The comment only mirrors the code. Perhaps it would be better if the comment said "// Turn large door LED on". :-

digitalWrite(Door_Led_Pin, HIGH);  //Set Door_Led high

To flash LEDs while either door is open, while still watching the reed switches, you'll need to learn how to do 'millis()'-based timing to do the flashing, then set/clear a state flag after reading each reed switch.
For example, you might create a flag called mainDoorOpen as follows:-

boolean mainDoorOpen;

Set/ clear it when you read the reed switch, then in your 'millis()'-based LED flashing routine, you only flash the LED if 'mainDoorOpen' is set. If 'mainDoorOpen' is clear, you turn off the LED.

There's an example of doing multiple things at once, including LED flashing, here:-
Demonstration code for several things at the same time


Also please edit your post and do as Larry suggests - place your code between code tags. Posting code inline is strictly verboten. :slight_smile:

You can do a search for 'blink without delay' and integrate that in your sketch. You can use the same principle for the fade function.
I suggest that you write some functions for fade and blink.

A note on below code

  if (brightness == 0 | brightness == 255) {
    fadeAmount = -fadeAmount;
  }

Although it works, the above if is not really correct. When two conditions are combined, one normally uses a logical OR ('||') and not a binary OR ('|'); same for AND etc.

sterretje:
A note on below code

  if (brightness == 0 | brightness == 255) {

fadeAmount = -fadeAmount;
  }



Although it works, the above if is not really correct. When two conditions are combined, one normally uses a logical OR ('||') and not a binary OR ('|'); same for AND etc.

Further to this, you can use "or" instead of '||' for a logical OR, and you can use "and" instead of '&&' for a logical AND.
e.g.

if (brightness == 0 or brightness == 255)
{
    fadeAmount = -fadeAmount;
}

I personally like using '||' and '&&' because I'm used to them, but just thought I'd mention it.
Many people find it easier to use/understand "or" and "and".

Have a look at Several Things at a Time for ideas about blinking while doing other things.

...R

Robin2:
Have a look at Several Things at a Time for ideas about blinking while doing other things.
...R

Beat you. :smiley:
(I suggested that he check out your "Several things...." thread in reply #2.)

OldSteve:
Beat you. :smiley:
(I suggested that he check out your "Several things...." thread in reply #2.)

Thank you very much, and apologies for missing your link.

...R

Missing a trick here guys. As he only wants to blink when the LED is on at the moment all he has to do is to replace the turn on the led statement with this.

if( millis() & 0x100 == 0 ) digitalWrite (Door_LED_Pin, HIGH); else digitalWrite (Door_LED_Pin, LOW);

Thank you all for the quick replies. I am adding a 3rd sensor and led to the mix. I'm also working on cleaning and modifying the code and implementing the suggestions given here. I will post more if i run into any issues and or having a working project that fits my needs.

Thanks again.