Hello, for sometime I've been trying to understand how to use millis. I've been to many sites, read forums, and gone in circles trying to figure out how to use millis in a way that one part of my code runs and finishes before another begins. I have a garden project that uses 10 hygrometers, 10 solenoids, 3 DHT22 sensors, and a handful of other types of sensors connected to a Mega 2560. I have succeeded in using millis to call certain functions at different times.Right now I'm just wanting to remove the delay from my Solenoid function and replace it with millis. I am using Serialprint just to be able to view the steps of my code as they run. I would like the code and the output to run/look like....
Now Testing Solenoids
Watering Section 1 // turns solenoid 1 on for 5 seconds
Section 1 is OFF // turns solenoid 1 off
wait before moving to next solenoid //Waits 2 seconds before going to the next solenoid
Watering Section 2 // turns solenoid 2 on for 5 seconds
Section 2 is OFF // turns solenoid 2 off
wait before moving to next solenoid //Waits 2 seconds before going to the next solenoid
Watering Section 3 // turns solenoid 3 on for 5 seconds
Section 3 is OFF // turns solenoid 3 off
wait before moving to next solenoid //Waits 2 seconds before going to the next solenoid
.....and so on.....
void Solenoid()
{
Serial.println ("Now Testing Solenoids");
unsigned long previousMillis10 = 0; // millis() returns an unsigned long.( )
unsigned long interval10 = 5000 ; // = 5 sec ( )
int period = 2000;
int period2 = 5000;
unsigned long time_now = 0;
unsigned long currentMillis = millis(); // grab current time
if (Value1 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay1, LOW); // turns on
Serial.println ("Watering Section 1");
}
if (millis() > period2) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay1, HIGH); // turns off
Serial.println (" Section 1 is OFF");
}
if (millis() > period2 && digitalWrite == HIGH) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value2 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay2, LOW); // turns on
Serial.println ("Watering Section 2");
}
if (millis() > time_now + period2) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay2, HIGH); // turns off
Serial.println (" Section 2 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value3 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay3, LOW); // turns on
Serial.println ("Watering Section 3");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay3, HIGH); // turns off
Serial.println (" Section 3 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value4 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay4, LOW); // turns on
Serial.println ("Watering Section 4");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay4, HIGH); // turns off
Serial.println (" Section 4 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value5 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay5, LOW); // turns on
Serial.println ("Watering Section 5");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
//if ((unsigned long)(currentMillis - time_now) >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay5, HIGH); // turns off
Serial.println (" Section 5 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value6 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay6, LOW); // turns on
Serial.println ("Watering Section 6");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay6, HIGH); // turns off
Serial.println (" Section 6 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value7 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay7, LOW); // turns on
Serial.println ("Watering Section 7");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay7, HIGH); // turns off
Serial.println (" Section 7 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value8 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay8, LOW); // turns on
Serial.println ("Watering Section 8");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay8, HIGH); // turns off
Serial.println (" Section 8 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value9 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay9, LOW); // turns on
Serial.println ("Watering Section 9");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay9, HIGH); // turns off
Serial.println (" Section 9 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
if (Value10 > 300)
{
if (millis() > time_now + period)
{
digitalWrite(solenoidRelay10, LOW); // turns on
Serial.println ("Watering Section 10");
}
if (millis() > time_now >= interval10) // check if "interval10" time has passed ( 5 sec )
//if ((unsigned long)(currentMillis - time_now) >= interval10) // check if "interval10" time has passed ( 5 sec )
{
digitalWrite(solenoidRelay10, HIGH); // turns off
Serial.println (" Section 10 is OFF");
}
if (millis() > time_now + period) {
Serial.println("wait before moving to next solenoid");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
}
Help would be greatly appreciated. Thank you in advance.
Also, I hope I have all of the necessary information for someone to be able to help.

