arduino as timer and light sensor!

Hi guys, i am new here ;)

Let me get to the point:

1) I am connecting 10 bulbs to a relay so that they go ON/OFF in intervals of 5mins all together. (so period is 10min and duty cycle is 50%)

2) i inted to do this via a transistor and an SPDT relay (i saw a diagram on how to do it on this forum)

3)in parallel with it (running at the same time) i need to connect some light sensors (LDR's) to see which bulb stopped working and after how many cycles.

now my problem is here.

void loop()
  digitalWrite(ledPin, HIGH);  
  delay(300000);                  // waits for 5min
  digitalWrite(ledPin, LOW);    
  delay(300000);                  // waits for 5min

i guess that using this code i cannot read the analogue pins right? also i am not sure if it is possible to have a 5min delay.?

so can someone point me in the right direction on how i can run the board as a timer (output high after 5mins and output low after 5min and repeating) and at the same time i read some other analogue inputs.


i guess that using this code i cannot read the analogue pins right?
also i am not sure if it is possible to have a 5min delay.?

Using the long delay function does limit what you can do. Delay is a 'blocking' function, which means nothing else in the main loop can function until the the delay times out.

Load the BlinkWithoutDelay sketch in the example/digital folder in your Arduino IDE. Study it to see how control can be done without using delays by time stamping a variable and then comparing it each time the main loop repeats. That lets you add many other functions in the main loop but still control other things on a time basis.

/* Blink without Delay

 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

  The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2005
 by David A. Mellis
 modified 17 Jun 2009
 by Tom Igoe

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
      ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);


thanks alot lefty. i will tell you if i did manage it or not ;)

Another way to do this is to use a RTC clock IC with an alarm output. You set the alarm to interrupt the uC every 5 minutes. During the interrupt routine you change the state of the relay. I have a RTC alarm example at (see the Application Hints section).

(* jcl *)

www: twitter: blog:

There's a really simple answer, if I understand your problem properly. but, as ever, not so simple to explain.

But first... do you have enough inputs to monitor whether the bulb has burned out? Just ignore this question if answer is yes.

So... on-off-on-off, with a change of state every 5 minutes, but without blocking the Arduino during the wait-for-next change-of-state.

The following may not be exactly syntactically correct and could be made more elegant, but should give you the idea....

void setup()
int SecondsPassed=0;//create and init variable
StateOfBulbs=1;//create and init variable
                       // "1" means "on", use "0" for "off"
TurnLightsOn;//You'll have to write a subroutine to do this

void loop()
delay (1000);
inc SecondsPassed;
CheckBulbs;//You'll have to write a subroutine to do this, deal with answers
if SecondsPassed==60*5

void ChangeBulbsOnOff()
if StateOfBulbs==0

yes i have enough inputs since i am using the Mega board (received it yesterday ;))

basically i started with the program lefty gave and at first i connected the relay to pin 13 just to see the led going ON and the relay switching and vice versa and it worked but with a problem or a bug:

it so happened that once i press Tools (release 0018) the relay will go ON/OFF which sounds weird since the program was uploaded :S
==>you know why this happens?

than i moved to pin12 and it worked w/o problems

only problem now is that when i press tools…serial monitor…it will restart losing the time were it was before.
(and yes i added serial.begin and serial.println in the program to see the number of milliseconds that passed from each ON/OFF)

ok guys i found the bug ;D i put 120ohm resistor between 5v and reset