Redo code concept so that multiple functions/operations can run at once

Hello-
I know you cannot run two functions at once with one CPU. However, I am stuck for a way to do this.
I'm using an Arduino Mega 2560 to control solenoids in pairs. For example, pin6 flow in, pin9 flow out.
There are 25 pairs.
I would like each pair (opening and closing for a random time) to be running simultaneously. I've had quite a bit of help with the running several things at once already from How to make delays "parallel", not serial - Programming Questions - Arduino Forum and have read the sticky post at the top of the other forum. Demonstration code for several things at the same time - Project Guidance - Arduino Forum

The code that I have posted obviously runs each function one at a time. I believe using functions is not the way to approach this, but I don't have an idea of what to do.
Thanks in advance.

// 25 solenoids opening have 25 partner solenoids that close. The reason for having separate solenoids is because of different flow rates.
// The solenoids should open and close randomly
const byte pinArray[] = {6, 7, 8, 9, 10, 11};
int depthArrayOn[] = {600,500,400,800,500,400};
int depthArrayOff[] = {700,800,700,600,500,600};
int count = 0;
int on, off;
   int pin6 = pinArray[0];
   int pin7 = pinArray[1];
   int pin8 = pinArray[2];
   int pin9 = pinArray[3];
   int pin10 = pinArray[4];
   int pin11 = pinArray[5];
int random_num_0_to_7 = random ();  

int depthOn (int timeOn){
on = depthArrayOn [random (0,5)];  // on = timeOn
return on;
}

int depthOff (int timeOff){
off = depthArrayOff [random (0,5)];
return off;
}

void pin7pin10(){
   int balloonOn, balloonOff;
   int countOn = depthOn(balloonOn);  
   int countOff = depthOff(balloonOff);   
     for (count=0;count <= 5;count++) {
     digitalWrite(pin7, HIGH);
     digitalWrite(pin10, LOW);
     delay (countOn);  
     digitalWrite(pin7, LOW);
     digitalWrite(pin10, HIGH);
     delay (countOff);
     }
}
void pin8pin11(){
   int balloonOn, balloonOff;
   int countOn = depthOn(balloonOn);  
   int countOff = depthOff(balloonOff);   
     for (count=0;count <= 5;count++) {
     digitalWrite(pin8, HIGH);
     digitalWrite(pin11, LOW);
     delay (countOn);  
     digitalWrite(pin8, LOW);
     digitalWrite(pin11, HIGH);
     delay (countOff);
     }
}
void pin6pin9(){
   int balloonOn, balloonOff;
   int countOn = depthOn(balloonOn);  
   int countOff = depthOff(balloonOff);   
     for (count=0;count <= 5;count++) {
     digitalWrite(pin6, HIGH);
     digitalWrite(pin9, LOW);
     delay (countOn);   
     digitalWrite(pin6, LOW);
     digitalWrite(pin9, HIGH);
     delay (countOff);
     }
}
void setup() {
    for (count=0; count <=5; count++){
    pinMode (pinArray[count], OUTPUT);
    }
}

void loop() {
  pin7pin10();
  delay(25);
  pin6pin9();
  delay(25);
  pin8pin11();
  delay(25);
  }

I would use a modified version of BlinkWithoutDelay example sketch. Get away from the delays and use the millis() function instead. You will need a separate "previousMillis" and "interval" variable for each pin.

const byte pinArray[] = {6, 7, 8, 9, 10, 11};
...
...
   int pin6 = pinArray[0];
   int pin7 = pinArray[1];
   int pin8 = pinArray[2];

Err, why?

AWOL: not sure how else to access pin numbers?
SurferTim: Thanks, will try that.

unsigned long previousMillis6;        // will store last time LED was updated
unsigned long previousMillis7;        // will store last time LED was updated

unsigned long interval6 = 1000;           // interval at which to blink (milliseconds)
unsigned long interval7 = 800;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);      

  unsigned long thisMilli = millis();
  previousMillis6 = thisMilli;
  previousMillis7 = thisMilli;
}

void loop()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis6 > interval6) {
    // save the last time you blinked the LED 
    previousMillis6 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (digitalRead(6)  == LOW)
      digitalWrite(6, HIGH);
    else
      digitalWrite(6, LOW);
  }

  if(currentMillis - previousMillis7 > interval7) {
    // save the last time you blinked the LED 
    previousMillis7 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (digitalRead(7)  == LOW)
      digitalWrite(7, HIGH);
    else
      digitalWrite(7, LOW);
  }
}

Sophi:
AWOL: not sure how else to access pin numbers?

Via an index into the array "pin array"

SurferTim: the problem is that when pin6 is ON, pin7 must be OFF. They are paired, if air is flowing in, it cannot also be flowing out. Thanks!

Be creative. :slight_smile: If all pins have a counterpart like that, then use this.

unsigned long previousMillis6;        // will store last time LED was updated
unsigned long previousMillis8;        // will store last time LED was updated

unsigned long interval6 = 1000;           // interval at which to blink (milliseconds)
unsigned long interval8 = 800;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);      
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  unsigned long thisMilli = millis();
  previousMillis6 = thisMilli;
  previousMillis8 = thisMilli;
}

void loop()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis6 > interval6) {
    // save the last time you blinked the LED 
    previousMillis6 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (digitalRead(6)  == LOW) {
      digitalWrite(7, LOW);
      digitalWrite(6, HIGH);
    }
    else {
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);
    }
  }

  if(currentMillis - previousMillis8 > interval8) {
    // save the last time you blinked the LED 
    previousMillis8 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (digitalRead(8)  == LOW) {
      digitalWrite(9, LOW);
      digitalWrite(8, HIGH);
    }
    else {
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
    }
  }
}

edit: This is using arrays.

#define FANCOUNT 4
unsigned long previousMillis[FANCOUNT];        // will store last time LED was updated
unsigned long interval[FANCOUNT] = {1000,900,800,700}; // interval for each fan set
int fanIn[FANCOUNT] = {6,8,10,12}; // intake fan pins
int fanOut[FANCOUNT] = {7,9,11,13}; // exhaust fan pins

void setup() {
  unsigned long thisMilli = millis();

  // set the digital pin as output and initialize previousMillis
  for(int i = 0; i < FANCOUNT; i++) {
    pinMode(fanIn[i], OUTPUT);
    pinMode(fanOut[i], OUTPUT);      
    previousMillis[i] = thisMilli;
  }
}

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

  for(int i = 0; i < FANCOUNT; i++) {
    if(currentMillis - previousMillis[i] > interval[i]) {
    // save the last time you blinked the LED 
      previousMillis[i] = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (digitalRead(fanIn[i])  == LOW) {
        digitalWrite(fanOut[i], LOW);
        digitalWrite(fanIn[i], HIGH);
      }
      else {
        digitalWrite(fanIn[i], LOW);
        digitalWrite(fanOut[i], HIGH);
      }
    }
  }
}

My apology for any typos.

SurferTim: Thank you! Learned much :slight_smile: