Irrigation code with millis

When i run the code it wont pass the 1 event
any suggestions ?

const unsigned long eventInterval_1=500;
const unsigned long eventInterval_2=5000;
const unsigned long eventInterval_3=20000;
const unsigned long eventInterval_4=20000;
const unsigned long eventInterval_5=20000;
const unsigned long eventInterval_6=20000;
const unsigned long eventInterval_7=20000;
const unsigned long eventInterval_8=700000;
unsigned long previousTime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(22,OUTPUT);
pinMode(24,OUTPUT);
pinMode(26,OUTPUT);
pinMode(28,OUTPUT);
pinMode(30,OUTPUT);
pinMode(32,OUTPUT);
pinMode(34,OUTPUT);
pinMode(36,OUTPUT);
pinMode(38,OUTPUT);
pinMode(40,OUTPUT);
pinMode(42,OUTPUT);
pinMode(44,OUTPUT);
pinMode(46,OUTPUT);
pinMode(48,OUTPUT);
pinMode(50,OUTPUT);
pinMode(52,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned long currentTime = millis();
if (currentTime - previousTime >=eventInterval_1){
digitalWrite(34,HIGH);
digitalWrite(44,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_2){
digitalWrite(22,HIGH);
digitalWrite(24,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_3){
digitalWrite(34,LOW);
digitalWrite(44,LOW);
digitalWrite(36,HIGH);
digitalWrite(46,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_4){
digitalWrite(36,LOW);
digitalWrite(46,LOW);
digitalWrite(38,HIGH);
digitalWrite(48,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_5){
digitalWrite(38,LOW);
digitalWrite(48,LOW);
digitalWrite(40,HIGH);
digitalWrite(50,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_6){
digitalWrite(40,LOW);
digitalWrite(50,LOW);
digitalWrite(42,HIGH);
digitalWrite(52,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_7){
digitalWrite(22,LOW);
digitalWrite(24,LOW);
digitalWrite(26,HIGH);
digitalWrite(28,HIGH);
digitalWrite(30,HIGH);
digitalWrite(32,HIGH);
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_8){
digitalWrite(26,LOW);
digitalWrite(28,LOW);
digitalWrite(30,LOW);
digitalWrite(32,LOW);
previousTime=currentTime;
}
}
}

}
}
}
}
}
}

How to use this forum.

Suggestions:

  1. Edit your post to add code tags, as described in "How to use this forum".

  2. Work through what happens in the code for event 1, carefully. Then you will understand the problem.

your event1 sets previousTime equal to currentTime so there is no way any of the following elapsed time interval tests will succeed.

when i run the code it doesn't pass.the first event any suggestions?

const unsigned long eventInterval_1=500;
const unsigned long eventInterval_2=5000;
const unsigned long eventInterval_3=20000;
const unsigned long eventInterval_4=20000;  
const unsigned long eventInterval_5=20000;
const unsigned long eventInterval_6=20000;
const unsigned long eventInterval_7=20000;
const unsigned long eventInterval_8=700000;
unsigned long  previousTime = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(22,OUTPUT);
pinMode(24,OUTPUT); 
pinMode(26,OUTPUT);
pinMode(28,OUTPUT); 
pinMode(30,OUTPUT);
pinMode(32,OUTPUT); 
pinMode(34,OUTPUT); 
pinMode(36,OUTPUT);
pinMode(38,OUTPUT); 
pinMode(40,OUTPUT);
pinMode(42,OUTPUT); 
pinMode(44,OUTPUT);
pinMode(46,OUTPUT); 
pinMode(48,OUTPUT); 
pinMode(50,OUTPUT); 
pinMode(52,OUTPUT); 
}

void loop() {
  // put your main code here, to run repeatedly:
unsigned long currentTime = millis();
if (currentTime - previousTime >=eventInterval_1){
digitalWrite(34,HIGH);
digitalWrite(44,HIGH);
previousTime=currentTime;
        

if (currentTime - previousTime >=eventInterval_2){
digitalWrite(22,HIGH);
digitalWrite(24,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_3){
digitalWrite(34,LOW);
digitalWrite(44,LOW);
digitalWrite(36,HIGH);
digitalWrite(46,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_4){
digitalWrite(36,LOW);
digitalWrite(46,LOW);
digitalWrite(38,HIGH);
digitalWrite(48,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_5){
digitalWrite(38,LOW);
digitalWrite(48,LOW);
digitalWrite(40,HIGH);
digitalWrite(50,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_6){
digitalWrite(40,LOW);
digitalWrite(50,LOW);
digitalWrite(42,HIGH);
digitalWrite(52,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_7){
digitalWrite(22,LOW);
digitalWrite(24,LOW);
digitalWrite(26,HIGH);
digitalWrite(28,HIGH); 
digitalWrite(30,HIGH);
digitalWrite(32,HIGH); 
previousTime=currentTime;

if (currentTime - previousTime >=eventInterval_8){
digitalWrite(26,LOW);
digitalWrite(28,LOW);
digitalWrite(30,LOW);
digitalWrite(32,LOW);
previousTime=currentTime;
}
}
}

}
}  
}
}   
}
}

Cross posting, makes us cross.


What are you trying to do with this sketch ?


No idea what you are trying to do but this might stir the pot:

const unsigned long eventInterval_1 = 500;
const unsigned long eventInterval_2 = 5000;
const unsigned long eventInterval_3 = 20000;
const unsigned long eventInterval_4 = 20000;
const unsigned long eventInterval_5 = 20000;
const unsigned long eventInterval_6 = 20000;
const unsigned long eventInterval_7 = 20000;
const unsigned long eventInterval_8 = 700000;

unsigned long       previousTime    = 0;

byte timingFlag                     = 0;

const byte outputPins[]             = {22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52};

void setup()
{
  for (byte x = 0; x <= sizeof(outputPins); x++)
  {
    pinMode(x, OUTPUT);
  }

}

void loop()
{
  unsigned long currentTime = millis();

  if (timingFlag == 0 && currentTime - previousTime >= eventInterval_1)
  {
    digitalWrite(34, HIGH);
    digitalWrite(44, HIGH);
    previousTime = currentTime;
    timingFlag = 1;
  }


  if (timingFlag == 1 && currentTime - previousTime >= eventInterval_2)
  {
    digitalWrite(22, HIGH);
    digitalWrite(24, HIGH);
    previousTime = currentTime;
    timingFlag = 2;
  }


  if (timingFlag == 2 && currentTime - previousTime >= eventInterval_3)
  {
    digitalWrite(34, LOW);
    digitalWrite(44, LOW);
    digitalWrite(36, HIGH);
    digitalWrite(46, HIGH);
    previousTime = currentTime;
    timingFlag = 3;
  }

  if (timingFlag == 3 && currentTime - previousTime >= eventInterval_4)
  {
    digitalWrite(36, LOW);
    digitalWrite(46, LOW);
    digitalWrite(38, HIGH);
    digitalWrite(48, HIGH);
    previousTime = currentTime;
    timingFlag = 4;
  }

  if (timingFlag == 4 && currentTime - previousTime >= eventInterval_5)
  {
    digitalWrite(38, LOW);
    digitalWrite(48, LOW);
    digitalWrite(40, HIGH);
    digitalWrite(50, HIGH);
    previousTime = currentTime;
    timingFlag = 5;
  }

  if (timingFlag == 5 && currentTime - previousTime >= eventInterval_6)
  {
    digitalWrite(40, LOW);
    digitalWrite(50, LOW);
    digitalWrite(42, HIGH);
    digitalWrite(52, HIGH);
    previousTime = currentTime;
    timingFlag = 6;
  }

  if (timingFlag == 6 && currentTime - previousTime >= eventInterval_7)
  {
    digitalWrite(22, LOW);
    digitalWrite(24, LOW);
    digitalWrite(26, HIGH);
    digitalWrite(28, HIGH);
    digitalWrite(30, HIGH);
    digitalWrite(32, HIGH);
    previousTime = currentTime;
    timingFlag = 7;
  }

  if (timingFlag == 7 && currentTime - previousTime >= eventInterval_8)
  {
    digitalWrite(26, LOW);
    digitalWrite(28, LOW);
    digitalWrite(30, LOW);
    digitalWrite(32, LOW);
    previousTime = currentTime;
    timingFlag = 8;                   //   <-----<<<<<  what should the flag go to ?
  }

}

The code controls 6 pumps and 10 valves, event 1 opens valves , event 2 turns on water pump , events 3 to 7 open and close valves while water pumps are on. And event 8 turns on 4 recirculating pumps that work symotanisly.

I haven't had a proper look at the code, but don't you need a previousTime for each event?

unsigned long  previousTime_1 = 0;
unsigned long  previousTime_2 = 0;
..
unsigned long  previousTime_3 = 0;

(Although arrays are nicer.)

“ The code controls 6 pumps and 10 valves, event 1 opens valves , event 2 turns on water pump , events 3 to 7 open and close valves while water pumps are on. And event 8 turns on 4 recirculating pumps that work symotanisly.”

Please try to explain the the above again using more precise language.

example:

  1. 33 and 34 operate for ‘X’ amount of time, after ‘X’ time do they stay operated ?
  2. After #1, 22 and 24 operate for ‘Y’ amount of time, after ‘Y’ time do they stay operated ?
    etc.

Do you know what a ‘State Machine’ is ?


Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

'the problem is all your if() statemetns.

if (currentTime - previousTime >=eventInterval_4){
digitalWrite(36,LOW);
digitalWrite(46,LOW);
digitalWrite(38,HIGH);
digitalWrite(48,HIGH); 
previousTime=currentTime;

see that " { " after the end of the line ?
you need to close that after you do that last line
you just piiled them all up at the end.
they way you have it, it is like using AND
if 1 = 1 AND 1=2 and 1 =3 and 1=4 and 1=5

@mxcoprez

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.