How to phrase a "Once a day" function.

Thread Starter

mbohuntr

Joined Apr 6, 2009
446
Hi all, I'm thinking of programming a Arduino to do this... After a photo resister "X" senses dark, set an output high for "Y" period, then repeat each night.

(watering my garden)
Read analog pin "X"
If "X" >= (value), Set pin "Y" High
count 20000
Set pin "Y" low
??????????
This is where the brain fart comes in...does anyone know how to tell it to reset, or loop after the photo resister goes low all day, then starts going high the next night? what about another read of "X" <= (value) then loop?

I haven't played with it for a couple of years, so I have to re-learn the syntax, but this part is simply conceptual...
Any help would be appreciated, Thanks!
 

noweare

Joined Jun 30, 2017
115
So when it gets dark the resistance increases and it makes a pin high (or low, up to you).
That pin is connected to an interrupt, like a pin change interrupt.

The software in the interrupt checks the value of the pin (high or low)
When the pin goes high (darkness) you set a some variable to one, lets say timeToWater. Your main program monitors timeToWater for a high and calls a function letsWaterNow() that turns on the water for some amount of time then shuts off AND also resets timeToWater to 0.

The interrupt only gets run when the pin changes state, since the interrupt only sets timeToWater on when the pin goes high this will only happen the next time the pin goes from low to high, which is the next night
 

Thread Starter

mbohuntr

Joined Apr 6, 2009
446
So when it gets dark the resistance increases and it makes a pin high (or low, up to you).
That pin is connected to an interrupt, like a pin change interrupt.

The software in the interrupt checks the value of the pin (high or low)
When the pin goes high (darkness) you set a some variable to one, lets say timeToWater. Your main program monitors timeToWater for a high and calls a function letsWaterNow() that turns on the water for some amount of time then shuts off AND also resets timeToWater to 0.

The interrupt only gets run when the pin changes state, since the interrupt only sets timeToWater on when the pin goes high this will only happen the next time the pin goes from low to high, which is the next night
I haven't seen the interrupt used, I'll have to look into it more.
Thank-you very much!
Mike
 

philba

Joined Aug 17, 2017
959
You don't need an interrupt if that is all the arduino does. Won't hurt if you do but it's not required.

More importantly, if you just use one "darkness" event, you may falsely trigger. I would read the sensor every second over some length of time - say 10 minutes or more. If it's dark in that entire period, then darkness is real. That way a bird sitting on the sensor, for example, won't trigger the sprinkler. Make the darktime minimum configurable for testing purposes.

A more reliable way to do this is to use a clock - either an RTC or some sort of network hookup to get the time. Then you can program for a specific time. More complex though.
 

witchdr

Joined Oct 27, 2010
3
Also, be careful when checking the light level. I would have a "gap" between brightness levels for dark and light otherwise you will find the it flicker between the two when you perform the reading at dusk and dawn.

As an example from an ADC pin it should be considered dark when the value is below, say 500 (on a scale of 0-1023) and light when above 550. Once you have decided it's dark (below 500) it won't be considered light again until it's above 550. The state won't change between the two for the middle 50.
 

philba

Joined Aug 17, 2017
959
Also, be careful when checking the light level. I would have a "gap" between brightness levels for dark and light otherwise you will find the it flicker between the two when you perform the reading at dusk and dawn.

As an example from an ADC pin it should be considered dark when the value is below, say 500 (on a scale of 0-1023) and light when above 550. Once you have decided it's dark (below 500) it won't be considered light again until it's above 550. The state won't change between the two for the middle 50.
Good point - in general you want to build in hysteresis any time you are using a level as a trigger for some operation. You can use the actual value or you can use a time delay between triggerings. The level approach is better but sometimes using time is easier. You see examples of hysteresis in control systems all over the place - your thermostat uses it, your kitchen over uses it.
 
Top