Delayed start

Hi everone,
I would like some help on how to write a code that has a delayed start when the sensor trips, but after that the arduino would run a small program that dose not run that delay again. The reason for this code is that I own a green house and an arduino is run a timer program for the fans and mister. The arduino is powered off a solar panel and the program starts after it's IR sensor detect the sun. However I don't need the fan and mister going off a daybreak. I would like a 4 hour delay from daybreak to the first time the mister come on, but the 4 hour delay should not happen after that unless the sun sets and a new day comes. Any clues on how to wright a program like this. I'm including the code I use right below, but I need to know how to add the 4 hours delay.

const int Fan = 8;
const int Water = 9;
const int IR = 6;

void setup() {
pinMode(Fan, OUTPUT);
pinMode( Water, OUTPUT);
pinMode(IR, INPUT);
}

void loop() {
 if(digitalRead(IR) == 1)
 {
   digitalWrite(Fan, HIGH);
   digitalWrite(Water, LOW);
   delay(120000);
   digitalWrite(Water, HIGH);
   digitalWrite(Fan, LOW);
   delay(480000);
   digitalWrite(Fan, HIGH);
   delay(600000);
 }
 else
 {
   digitalWrite(Fan, HIGH);
   digitalWrite(Water, HIGH);
 }
}

See the 'blink without delay' example in the learning > examples page of this site, or in the IDE.
Also, set a flag to show that the delay has occurred and doesn't need to be used again.

thank you for the help. I came up with this program, but I have not tested it yet. Do you think it will work?

const int Fan = 8;
const int Water = 9;
const int IR = 6;
int x;

void setup() {
pinMode(Fan, OUTPUT);
pinMode( Water, OUTPUT);
pinMode(IR, INPUT);
x = 0;
}

void loop() {
 if(digitalRead(IR) == 1)
 {
   delay(24000000);
   x + 1;
   flag:
   if((digitalRead(IR) == 1) && (x == 1))
   {
     digitalWrite(Fan, HIGH);
     digitalWrite(Water, LOW);
     delay(120000);  
     digitalWrite(Water, HIGH);
     digitalWrite(Fan, LOW);
     delay(480000);
     digitalWrite(Fan, HIGH);
     delay(600000);
     goto flag;
   }
   else
   {
     x -1;
   }
 }
 else
 {
   digitalWrite(Fan, HIGH);
   digitalWrite(Water, HIGH);
 }
}

never use delay for more than about 100

first, unsigned long does 4,294,967,295

your delay is 24,000,000

you can declarer a timer as :

unsigned long timer ;
unsigned long  [color=#222222]duration ;[/color]


//then under voidloop()

start = [color=#222222]digitalRead(IR);[/color]

[color=#222222]if(start=HIGH && last_start=LOW){[/color]
[color=#222222]start_timing=HIGH;[/color]
 timer [color=#222222]= millis();[/color]
[color=#222222]}[/color]

[color=#222222]last_start = start;[/color]

[color=#222222]if (millis()-timer >= [/color]24000000){
   system_go=HIGH;
}

this will give you a point, 'system_go' that only starts after ir went and stayed high for your 24million ms.

you could add

[color=#222222]

duration = (millis()-timer)

if(duration >= 24120000 && duration<= 24130000){[/color]
[color=#222222]  d[/color][color=#222222]igitalWrite(Fan, HIGH);[/color]
[color=#222222]  digitalWrite(Water, LOW);[/color]
}
//  then, run for your time, then turn them off.

[color=#222222]if(duration >= 24130001 && duration<= 241300010){[/color][color=#222222] //10 ms to shut them off)[/color]
  digitalWrite(Fan, LOW);
  digitalWrite(Water, LOW);
}

a note here is that this is sloppy, it give you a 10ms window to shut things off after they ran for some time.
and it allows you to turn them back on. but, if you use delay anywhere and miss that 10ms window, they do not get shut off.

@dave-in-nj, you can't put coloured text into a code box.

@blade1112345, have a look at how millis() is used to manage timing in several things at a time.

...R

Robin2:
@dave-in-nj, you can't put coloured text into a code box.

@blade1112345, have a look at how millis() is used to manage timing in several things at a time.

...R

yeah... i wish i could figure that out... spaces, fonts, and colors are my nemisis, second only to stairs and spelling.

yeah... i wish i could figure that out... spaces, fonts, and colors are my nemisis, second only to stairs and spelling.

Bears, bears are the worst nemesis (just as Stephen Colbert says).

LarryD:
Bears, bears are the worst nemesis (just as Stephen Colbert says).

TED .... whew... gives me the shakes....

"TED"
Right next to Clowns. :astonished:

.

Just to get a better idea of your problem.

You run the Arduino on solar power.

Does this mean that it will be completely without power overnight?

Or do you have a backup battery keeping your Arduino functions alive.
(This would make it much more easier to develop code that does what you want)