sensor 4 sec delay then turn on relays

I am trying to write a program to do the following functions....

  1. when beam between two sensors is broken for 4 secs, I want to:
    a) turn on relay to turn on light that remains on until reset
    b) turn on relay to turn on transmitter for 25 seconds then turn off for 5 seconds then cycle until reset with same process

this is what i have. this is a first time project and I am still very green! any help and suggestions gladly accepted!

//Pin Numbers will not change:
const int sensor = 8;
const int MERS = 3;
const int LED = 13;

// Variables that will change:
int sensorState = LOW;
int lastsensorState = HIGH;
int MERSState = LOW;
int LEDState = LOW;

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {

pinMode(sensor,INPUT);
pinMode(MERS,OUTPUT);
pinMode(LED, OUTPUT);

// initial LED & MERS State
digitalWrite(MERS, MERSState);
digitalWrite(LED, LEDState);

}

void loop() {
int reading = digitalRead(sensor);

if (reading != lastsensorState) {
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the state has changed:
if (reading != sensorState) {
sensorState = reading;

// only toggle the LED if the new button state is HIGH
if (sensorState == HIGH) {
MERSState = !MERSState;
if (sensorState == HIGH) {
LEDState = !LEDState;
}
}
}

// set MERS & LED
digitalWrite(LED,LEDState);

digitalWrite(MERS,MERSState);
delay(25000);
digitalWrite(MERS,MERSState);
delay(5000);}
}
}

Take a look at the Blink Without Delay Example in the Arduino directory.

Basically, when you want to do something a certain time later, you get the current millis() value and put it into a variable, then each time through loop(), you check to see if that the current millis() value minus the value in the start time variable is equal to, or greater than, the desired duration.

Any variables you use for these values should be of type unsigned long

I found this code and it seems to kind of do what I want , but I have not figured out how to make one stay on and the other cycle.

Also I am trying to figure out how to only start the process if the beam is broken for "x" time

/*
January 28, 2014
[author] Mark Graziano
mark.graziano.13@gmail.com
[instructables profile] http://www.instructables.com/member/GraziCNU/
*/

int LEDArray[] = {13, 10}; // LED array
int numOfLEDs = 2; // LEDs of index 0-3
int pirState = LOW;
int pirVal = 0;
int pirPin = 7;

void setup() {

pinMode(pirPin, INPUT);

int i;
for(i = 0; i < numOfLEDs; i++) {
pinMode(LEDArray*, OUTPUT);*

  • }*
    }
    void loop() {
  • pirVal = digitalRead(pirPin);*
  • int i;*
  • //if motion is captured, light the LEDs in sequence*
  • if (pirVal == HIGH) {*
  • for (i= 0; i < numOfLEDs; i++) {*
    _ analogWrite(LEDArray*, 255);_
    _
    delay (10000);_
    _
    }_
    _
    delay (1000);_
    _
    for (i= 0; i < numOfLEDs; i++) {_
    _ analogWrite(LEDArray, 0);
    delay (1000);
    }
    if (pirState == LOW) {
    pirState = HIGH;
    }
    }
    //else, set all LED values to zero*
    * else {
    analogWrite(LEDArray[0], 0);
    analogWrite(LEDArray[1], 0);
    if (pirState == HIGH) {
    pirState = LOW;
    }
    }
    }*_

I was sure I asked you to "Take a look at the Blink Without Delay Example in the Arduino directory.". Perhaps I should have been more clear.

Take a look at the Blink Without Delay Example in the Arduino directory. Play with it. Change things in it, and see what it does. When you understand what Blink Without Delay does, you problem will be solved.

OP almost has it. Well, not really.

First is to change all those ints that will never be outside 0 to 255 to byte instead of int. RAM is limited.

Second is to get rid of the code-blocking delays, NOTHING happens during a delay.

Can you tell what is wrong with this?

     lastDebounceTime = millis();
  if ((millis() - lastDebounceTime) > debounceDelay) {

Are you throwing other people's code that you don't understand up for fixing? Or did you write that?
Because if that's so then nothing below will make any sense to you. Learn BlinkWithoutDelay.

But if you're not a noob who treats code like incantations...
When you write code to handle events, tasks break down to pieces that run in loop() when triggered.

One kind of trigger is TIME through millis() or micros(), both return unsigned long values.

tNow = millis();
if ( tNow - tStart >= tWait ) { code for when timed event is triggered }

Another kind of trigger is STATE which is simply a value in a variable that reflects the current stage that events have reached. The total code for the task is split into pieces wherever there is a delay and the new piece triggers on time set by the part split from. That way when loop() comes to the part that is waiting, the next piece of code gets a chance to trigger, and so on.
A simple state machine can handle serial input on a per-char basis, match keywords, evaluate numbers, things like that even while it blinks led 13 on time as a status light and still be able to add tasks without making spaghetti code.

Other triggers are interrupts and pin reads.
My last priority, bottom of the list trigger is serial available, but I always run a fast loop by never running long code in one piece. Break tasks to sub-tasks and let other tasks run a slice in between. 1 ms is 16000 CPU cycles. If a task in loop() is cycle-heavy, do a return at the end of that task to make loop() run again and check the critical triggers first, etc. Small, very fast code steps in loop() let your code be immensely responsive in Real Time.
Hey, microcontrollers were invented to handle Real Time. That's what setup() and loop() are geared to support.