Timed LED based on input received.

Hi Guys.

This might be something I missed in the other posts but I'm struggling to get my head around this..

I'm busy building an alarm system.

It will be based on an IR Beam that when broken, an alarm should sound. I should also be able to reset via remote but that's another issue I'll battle out as soon as i get there in this project.

For now though, I'm able to pick up when the beam is broken and able to light an LED (alarm will be added via relay as final step.)

My problem is that I want to be able to have the Siren (LED for now) active for about 1 min even after the beam has been closed again.

Using "Delay" is not an option as that will halt all other logic and I need it to run so that I can still remotely reset the system by changing the outputs.

What I have now is that when the beam is broken, the LED lights up as expected, but the moment the beam is closed again, the LED switches off.

PS. Any positive criticism welcome!

#define LEDPIN 13
#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(LEDPIN, OUTPUT);      
 // initialize the sensor pin as an input:
 pinMode(SENSORPIN, INPUT);     
 digitalWrite(SENSORPIN, HIGH); // turn on the pullup
}

void loop(){
 // read the state of the pushbutton value:
 sensorState = digitalRead(SENSORPIN);

 // check if the sensor beam is broken
 // if it is, the sensorState is LOW:
 if (sensorState == LOW) {     
   // turn LED on:
   digitalWrite(LEDPIN, HIGH);  
 } 
 else {
   // turn LED off:
   digitalWrite(LEDPIN, LOW); 
 }
}

I have to apologize though, I have no clue how to upload a code sni[et so I’ll just paste what I have.

1-Start by learning how to use the forum
2-specifically the toolbar buttons
3- Put your mouse cursor over each one and read the popup label
4-The FIRST button with the “</>” label is the CODE BUTTON.
5- HIGHLIGHT YOUR CODE
6- click the CODE button.
7- click SAVE.

My problem is that I want to be able to have the Siren (LED for now) active for about 1 min even after the beam has been closed again.

Using “Delay” is not an option as that will halt all other logic and I need it to run so that I can still remotely reset the system by changing the outputs.

Ever heard of millis() ?

My problem is that I want to be able to have the Siren (LED for now) active for about 1 min even after the beam has been closed again.

That looks quite simple.

#define LEDPIN 13
#define SENSORPIN 4
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);      
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
}

boolean alarmActive=false; 
unsigned long alarmTime;

void loop(){
  // if no alarm active, read the state of the pushbutton value:
  if (!alarmActive && digitalRead(SENSORPIN)==LOW)
  {
    alarmActive=true;   // activate alarm
    alarmTime=millis(); // remember time
  }
  if (alarmActive && millis()-alarmTime>60000L) // timeout after one minute
  {
    alarmActive=false; // deactivate alarm after one minute
  }
  digitalWrite(LEDPIN, alarmActive);  // set alarm LED accordingly
}

You have a "Finite State Machine" which is in one of two states:

  • no alarm active
  • alarm active

If it gets more complicated than that, you could use a library to handle the different states of your state machine.

Morning.

Thank you very much.
I am still a beginner at this so will try and understand how you incorporated Millis() and the logic around it.

Thank you for the assistance!!