 # Independent and random on and off times without delays

Hello-
I have a bank of intake solenoids which all have corresponding outtake solenoids.
For example if inTake is on, outTake must be off. If outTake  is high, inTake  must be low.

The high times (inWhoosh) and high times (outWhoosh) are slightly different.
The high-in times are longer than the high-out times. The high-in times should take random values from the inWhoosh array and the high-out times should take random values from the outWhoosh array.

I’ve tried to work out the logic and came up with psuedocode. Not sure that this is correct, or how to go about it. Thanks in advance.

My psuedocode is

``````if inTake state is high inTake(random [i])
set corresponding outTake state low (series of if statements?)
if inTake state is high and it's been longer than the ON interval
grab the currentMillis and set to previousMillis
set inTake state to low
go to beginning

if outTake state is high outTake(random [i]) and it's been longer than the OFF interval
grab the currentMillis and set to previousMillis
set outTake state to high
``````

My actual code is

``````#include <Streaming.h>                                                  // library for formatting printf
#define SOLENOIDCOUNT 8
unsigned long previousMillis;                                        // will store last time solenoid was toggled
unsigned long interval[] = {1700,2000,1500,700};                          // interval time for each solenoid on/off
int outTake[SOLENOIDCOUNT] = {1,2,3,4,5,6,7,8};                                  // intake pins
int inTake[SOLENOIDCOUNT] = {9,10,11,12,14,15,16,17};                              // exhaust pins
unsigned long inWhoosh[] = {4000,4900,4200,4800,4100,4200,4900,4400};
unsigned long outWhoosh[] = {2000,3000,1800,3700,2000,1100,3000,2000};
long int count = 0;
int led = 13;
void setup() {                                                          // set the digital pin as output and initialize previousMillis
Serial.begin(9600);
unsigned long thisMilli = millis();
for(int i = 0; i < 8; i++) {
pinMode(inTake[i], OUTPUT);
pinMode(outTake[i], OUTPUT);
previousMillis[i] = thisMilli;
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
}

void loop() {
unsigned long currentMillis = millis();
for(int i = 0; i < 8; i++) {
if(currentMillis - previousMillis[i] > inWhoosh[i]) {         // only switch states if current - previous > than array value
//if current millis( the ones that have been counting since Arduino was turned on) - previous millis(points to value in the array) > interval [points to value in the array]
previousMillis[i] = currentMillis;                                    // save the last time you flipped the solenoid
digitalWrite(outTake[i], LOW);
digitalWrite(inTake[i], HIGH);
delay(200);
}
else{
digitalWrite(inTake[i], LOW);
digitalWrite(outTake[i], HIGH);
delay(200);
}
}
}
}
``````

Change the interval array value at each pin state change.

``````#include <Streaming.h>                                                  // library for formatting printf
#define SOLENOIDCOUNT 8
unsigned long previousMillis;                                        // will store last time solenoid was toggled
unsigned long interval[] = {1700,2000,1500,700};                          // interval time for each solenoid on/off
int outTake[SOLENOIDCOUNT] = {1,2,3,4,5,6,7,8};                                  // intake pins
int inTake[SOLENOIDCOUNT] = {9,10,11,12,14,15,16,17};                              // exhaust pins
unsigned long inWhoosh[] = {4000,4900,4200,4800,4100,4200,4900,4400};
unsigned long outWhoosh[] = {2000,3000,1800,3700,2000,1100,3000,2000};
long int count = 0;
int led = 13;
void setup() {                                                          // set the digital pin as output and initialize previousMillis
Serial.begin(9600);
unsigned long thisMilli = millis();
for(int i = 0; i < 8; i++) {
pinMode(inTake[i], OUTPUT);
pinMode(outTake[i], OUTPUT);
previousMillis[i] = thisMilli;
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
}

void loop() {
unsigned long currentMillis = millis();
for(int i = 0; i < 8; i++) {
if(currentMillis - previousMillis[i] > inWhoosh[i]) {         // only switch states if current - previous > than array value
//if current millis( the ones that have been counting since Arduino was turned on) - previous millis(points to value in the array) > interval [points to value in the array]
previousMillis[i] = currentMillis;                                    // save the last time you flipped the solenoid
digitalWrite(outTake[i], LOW);
digitalWrite(inTake[i], HIGH);
interval[i] = inWhoosh[i];
}
else{
digitalWrite(inTake[i], LOW);
digitalWrite(outTake[i], HIGH);