Yellow led should blink (alarm system)

I suddenly got to work on my Alarm system.

The only then yet i installed is a doormagnet for my door, so when the door opens, the sirene goes on for like 10 secs, and then if the door is closed again, the alarm turns off.

I got 3 leds.

Red for when alarm is on.
Green for when Door is closed and everything is OK.
And a Yellow. And for the yellow i have a little question.

Yellow:
My idea is, when the alarm is on, and the door still i open, the yellow led should just be LOW. But when the door is closed and the sirene is still on (delay(10000):wink: the yellow LED should start to blink with a delay of 100 mil until the sirene is off (as it is after 10 secounds, when the door is closed), then it should be LOW- but i dont know how i can do that - i tryed for hours last day, but notin dident work :frowning:
Look at my code:

int ledPin = 13; // choose the pin for the LED
int inPin = 2;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == LOW) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);
    
  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn LED ON    
    
    delay(10000);
    
  }
   
    digitalWrite(yellowPin, HIGH);
    delay(200);
    digitalWrite(yellowPin, LOW);
    delay(200);
}

Can anyone tell me how to do that?.

And then i have another question :slight_smile:

  • the Arduino board is only 5v - and it wont work if i put in a 9v battery to my solderless breadboard instead of the power from the arduino board. Like the sirene needs atleast 12v (a better one i want for ths project) and some moton detectors needs more power. So how am i supposed to add like 12v power supply to the breadboard? :slight_smile:

Thank you all, and MERRY CHRISTMAS!

Once you enter delay() all bets are off. That is the arduino effectively hangs until the delay is over.
Therefore to do something in the delay time you have to write a function that does the stuff and exits when the delay time is up.
Use millis() + delayTime to get the end time, then hold in a while loop until millis() > delayTime
While you are in that loop you can do stuff with your yellow LED.

The most arduino boards have a power regulator on them so if you connect your 12V source to Vin pin you can run everything off one supply.

Hello :slight_smile:

Thank you so much for your answer, but cause im a (somehow) stupid human, i dident really understand, i have to say.

Can you try write the code, so understand better, and can fix that, in a other time? :)..

And about the 9v supply. Sould i add - or + to the VIN, and then plugin the adapter, or what? :slight_smile:

while(delayTime != "0" && val == "LOW") {
digitalWrite(yellowPin, High);
delay(100);
digitalWrite(yellowPin, Low);
}

or something like that? :slight_smile:

and oh, dont look at the code description, use this one:

int ledPin = 13; // choose the pin for the SIRENE
int inPin = 2;   // choose the input for the door magnet
int val = 0;     // variable for reading the sirene status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare sirene as output
  pinMode(inPin, INPUT);    // declare doormagnet as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read sirene value
  if (val == LOW) {         // check if the door magnet is LOW (door closed)
    digitalWrite(ledPin, LOW);  // turn sirene OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);  
    
  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn sirene ON  
    
    delay(10000);
    
  }
  
    digitalWrite(yellowPin, HIGH);
    delay(200);
    digitalWrite(yellowPin, LOW);
    delay(200);
}

:slight_smile:

That is close but you need an other delay after the yellow LED turns off.
Try this:-

int ledPin = 13; // choose the pin for the SIRENE
int inPin = 2;   // choose the input for the door magnet
int val = 0;     // variable for reading the sirene status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare sirene as output
  pinMode(inPin, INPUT);    // declare doormagnet as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read sirene value
  if (val == LOW) {         // check if the door magnet is LOW (door closed)
    digitalWrite(ledPin, LOW);  // turn sirene OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);

  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn sirene ON

    flashDelay(10000);

  }

    digitalWrite(yellowPin, HIGH);
    delay(200);
    digitalWrite(yellowPin, LOW);
    delay(200);
}
 
 void flashDelay(long del){
   long endTime = millis() + del;
   while(endTime > millis()) {
     digitalWrite(yellowPin, HIGH);
     delay(100);
     digitalWrite(yellowPin, LOW);
     delay(100);
   }
 }

And about the 9v supply. Sould i add - or + to the VIN, and then plugin the adapter, or what?

Put the - of the 9V supply to the ground of the Arduio.
Put the + of the 9V supply to the Vin pin of the arduino
OR
Plug the 9V supply into the adapter input socket of the arduino, with the +ve to the center pin and the -ve to the outer connection.

Oh- it allmost work corretly :)..

Now the yellow LED start to blink whenever the sirene is on, even the door is closed or open.

I only want it to blink, when the door is closed, and the alarm sirene is on :)..

Merry christmas, thank you Grumpy_Mike - you the best ;D

int ledPin = 13; // choose the pin for the SIRENE
int inPin = 2;   // choose the input for the door magnet
int val = 0;     // variable for reading the sirene status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare sirene as output
  pinMode(inPin, INPUT);    // declare doormagnet as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read sirene value
  if (val == LOW) {         // check if the door magnet is LOW (door closed)
    digitalWrite(ledPin, LOW);  // turn sirene OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);

  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn sirene ON

    flashDelay(10000);

  }

}

 void flashDelay(long del){
   long endTime = millis() + del;
   while(endTime > millis()) {
     digitalWrite(yellowPin, HIGH);
     delay(100);
     digitalWrite(yellowPin, LOW);
     delay(100);
   }
 }

THANK YOU SO MUCH! :slight_smile:

I got it to work now, thank you so much! :slight_smile:

With this code:

int ledPin = 13; // choose the pin for the SIRENE
int inPin = 2;   // choose the input for the door magnet
int val = 0;     // variable for reading the sirene status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare sirene as output
  pinMode(inPin, INPUT);    // declare doormagnet as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read sirene value
  if (val == LOW) {         // check if the door magnet is LOW (door closed)
    digitalWrite(ledPin, LOW);  // turn sirene OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);

  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn sirene ON

    flashDelay(10000);

  }

}

 void flashDelay(long del){
   long endTime = millis() + del;
   val = digitalRead(inPin);
   if(val == LOW) {
   while(endTime > millis()) {
     digitalWrite(yellowPin, HIGH);
     delay(100);
     digitalWrite(yellowPin, LOW);
     delay(100);
   }
   }
 }

Have a merry christmas!

And oh. About the 9V power supply issue.

When i insert the + to the VIN and the - to the GND, it did exaly the same, as before, when i plug'ed in the adapter (with the head on) into the arduino board (like when you charge a pc or a moblile phone).

ALL three leds are HIGH, and the L led (intregated on the arduino board) is blinkin like this:

HIGH
100 millis
LOW
1000 millis
HIGH
100 millis
LOW
1000 millis

Why that? The adapter i connected is:

AC-DC Charger
Input: 230v~50hz 7W
OUTPUT: 9v --- 300mA

Thank you;)

  • and then i have another question :D- sorry if i ask too much.
    I have a button (pin 4) - and i want to use that button for, when you click on it (LOW) green and red ligh should turn of, and the yellow led should be on ONLY and then hole the system waits for like 10 secs (delay), and then it actiave again. Used for, when you have to go out of the door, you dont want the alarm to go on, then you clikc the button, then you go, and close the door after you :)..

But i dont know.

With the code above, the yellow light is just on and red and green led is off, and nothing happens. When i open the door, the alarm siren don't activate before about 0-10 secs (due to the delay). That is exaly what the system should do, when i click the button. But ONLY when i click it, not automatically :)..

int ledPin = 13; // choose the pin for the SIRENE
int inPin = 2;   // choose the input for the door magnet
int val = 0;     // variable for reading the sirene status
int dor = 0;
int alarmPin = 6;
int greenPin = 7;
int yellowPin = 4;
int butPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);  // declare sirene as output
  pinMode(inPin, INPUT);    // declare doormagnet as input
  pinMode(alarmPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(butPin, INPUT);
}

void loop(){
  val = digitalRead(inPin);  // read sirene value
  if (val == LOW) {         // check if the door magnet is LOW (door closed)
    digitalWrite(ledPin, LOW);  // turn sirene OFF
    digitalWrite(alarmPin, LOW);
    digitalWrite(greenPin, HIGH);
    
   val = digitalRead(butPin);
   if(val == LOW) {
     digitalWrite(ledPin, LOW);
     digitalWrite(alarmPin, LOW);
     digitalWrite(greenPin, LOW);
     digitalWrite(yellowPin, HIGH);
     delay(10000);
     digitalWrite(yellowPin, LOW);
   }

  } else {
    digitalWrite(yellowPin, LOW);
    digitalWrite(alarmPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  // turn sirene ON

    flashDelay(10000);

  }

}

 void flashDelay(long del){
   long endTime = millis() + del;
   val = digitalRead(inPin);
   if(val == LOW) {
   while(endTime > millis()) {
     digitalWrite(yellowPin, HIGH);
     delay(100);
     digitalWrite(yellowPin, LOW);
     delay(100);
   }
   }
 }

What is wrong? :slight_smile:

Thank you all ;-).

Hi,
I am not sure about your external supply it would be best if you drew a schematic so I could see what you are trying to do.
All voltages attached to the input / outputs of the Arduino must be 5V or less. If you want more then you have to use transistors or other such devices to match the Arduino to what you want. So no doubt you are not understanding something,

As to the code problems sorry but I haven't time at the moment. I am going away today and I wont be back until the first Monday of the new year. If anyone else wants to help on this then feel free but if not it will have to wait until then. Sorry.