Alrighty... BRAND-SPANKING new at Arduino.... I do understand the simple logic in the programming... It's been many years since, but i used to program PLCC's and work with digital electronics all the time in the military... but I'm older and RUSTY! lol
I am positive this has been answered within these forums... but after going thru thousands of lines of code examples posted here I am now need of new glasses, a few beers, and possibly a lobotomy! What I need(using the following code for examples) is to have 4 different events happening at different intervals, with different timings between each interval. I grow Hungarian Peppers indoors, I have a homemade dome to grow cuttings and seedlings in. On this dome I have a heating pad underneath, vent fans, one light atop, a water pump, and a pond mister to keep humidity high.
Using the following basic code, I need these conditions:
- Ability to reduce the "on time" of the mister later(on 24 hours in the beginning) to start acclimating the seedlings to lower humidity. Looped.
- Turn vent fans on (always two fans together, one pushing air in, the other pulling out) for 20 seconds on, 2 hours off. Looped.
- Water pump turns on for 10 minutes, then off for 2 hours. Looped.
- Light on for 16 hours, off for 8 hours. Looped.
I have a 4-relay shield atop my Arduino Uno.
Code thus far:
// Which pins are connected to which LED
const byte Light = 4;
const byte WaterPump = 5;
const byte Fans = 6;
const byte Mister = 7;
// Time periods of blinks in milliseconds (1000 to a second).
//60000 milliseconds in 1 minute, 3600000 ms in 1 hour, 57600000/16hrs
const unsigned long Lightinterval = 5000;//57600000=16 hours
const unsigned long LightOFFinterval = 10000;//57600000=16 hours
const unsigned long WaterPumpinterval = 600000;//600000=10 minutes
const unsigned long Fansinterval = 7200000;//7200000=2 hours
const unsigned long Misterinterval = 86400000;//86400000=1 day
// Variable holding the timer value so far. One for each "Timer"
unsigned long Lighttimer;
[color=red]unsigned long LightOffTimer;[/color]
unsigned long WaterPumptimer;
unsigned long Fanstimer;
unsigned long Mistertimer;
void setup ()
{
pinMode (Light, OUTPUT);
pinMode (WaterPump, OUTPUT);
pinMode (Fans, OUTPUT);
pinMode (Mister, OUTPUT);
Lighttimer = millis ();
[color=red]LightOffTimer = 5000;[/color]
WaterPumptimer = millis ();
Fanstimer = millis ();
Mistertimer = millis ();
} // end of setup
void toggleLight ()
{
if (digitalRead (Light) == LOW)
digitalWrite (Light, HIGH);
else
digitalWrite (Light, LOW);
// remember when we toggled it
Lighttimer = millis ();
} // end of toggleLight
void toggleWaterPump ()
{
if (digitalRead (WaterPump) == LOW)
digitalWrite (WaterPump, HIGH);
else
digitalWrite (WaterPump, LOW);
// remember when we toggled it
WaterPumptimer = millis ();
} // end of toggleWaterPump
void toggleFans ()
{
if (digitalRead (Fans) == LOW)
digitalWrite (Fans, HIGH);
else
digitalWrite (Fans, LOW);
// remember when we toggled it
Fanstimer = millis ();
} // end of toggleFans
void toggleMister ()
{
if (digitalRead (Mister) == LOW)
digitalWrite (Mister, HIGH);
else
digitalWrite (Mister, LOW);
// remember when last toggled
Mistertimer = millis ();
} // end of toggleMister
void loop ()
{
// Handling the intervals.
if ( (millis () - Lighttimer) >= Lightinterval)
toggleLight ();
if ( (millis () - WaterPumptimer) >= WaterPumpinterval)
toggleWaterPump ();
if ( (millis () - Fanstimer) >= Fansinterval)
toggleFans ();
if ( (millis () - Mistertimer) >= Misterinterval)
toggleMister ();
} // end of loop
As we can see "interval" works great for turning on (HIGH) an item, however, this code uses the same amount of time (interval) to time the "LOW" (off) time as well... can't have that as I do not want the identical HIGH and LOW (on and off) times.
I started to write in "LightOffTimer" variable but am not sure how to perform the logical decisions in the final interval (Void Loop) control code to accomplish the math to make my times different for each toggle event.
Any help would be appreciated! Even just pointing me to the forum posts of similar threads with solutions as I do not want to go the lobotomy route!
Thanks in advanced,
Scott