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);
}