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.
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.