intelligent light timer with reset and rollover

I am trying to set up an intelligent timer using 5V 16Mhz mini pro together with an RCWL-0516 and a 5V relay.

I was thinking about using 15 seconds for the initial timer. If the timer is counting down and a new event is being detected, I would like it to be reset to the original value and start again.
If there are more than 3 events triggered within 1 minutes I would like to extend the timer to 1 minute.

Again, if there are more than 3 events in 1.5 minutes detected then extend timer to 3 minutes.

I am also trying to save power since there will be a few of these. what is the best approach to put them into sleep?

that I have been using right now. not working very great:

int ip = 10;

int val = 0;

int led = 11;

void setup() {


  pinMode (ip, INPUT);

  pinMode (led, OUTPUT);


void loop() {

  val = digitalRead(ip);

  Serial.println(val, DEC);

  if (val >0){
    digitalWrite(led, LOW);
  else {
    digitalWrite(led, HIGH);

I am pretty sure that I didn't come up with this as the first person. where can I find good examples that I would just be able to re-design for my needs?

I am trying a few things but don't really know how to do it exactly.


int Sensor = 10; //Input Pin
int Relay01 = 11; // Relay Pin 01
int Relay02 = 12; // Relay Pin 02

int flg = 0; //Change detection flag
void setup() {
pinMode (Sensor, INPUT); //Define Pin as input
pinMode (Relay01, OUTPUT); //Relay Pin 01 as OUTPUT
Serial.println("Waiting for motion");

void loop() {
int val = digitalRead(Sensor); //Read Pin as input

if((val > 0) && (flg==0))
digitalWrite(Relay01, HIGH);
Serial.println("Motion Detected");
flg = 1;

if(val == 0)
digitalWrite(Relay01, LOW);
flg = 0;

The main Arduino site has loads of examples and lessons at the links that so many people don't follow.

Arduino has time functions millis() and micros() that return unsigned long values.

unsigned long markTime; // save the time that something happened, mark the time down


markTime = millis();

When you subtract unsigned integers you get the difference between them regardless of rollover or not. The subtraction being unsigned will always be right as long as the time is less than what your time variable can hold. Unsigned long millis() can time up to 49.71-some DAYS intervals, again whether millis() rolls over between start and end times makes no difference when usigned subtraction is used so just deep-six any insistence on "rollover code" as that's a problem for those with their heads stuck in signed math.

elapsed_time = end_time - start_time; // every time

Your IDE has Examples that the Arduino site has a page for each with explanations.