intermittent flow controller

hi,
i’m a newbie with arduino coding, i’m working on a scientific project (repirometry chambers) and to make it work i need to control 4 pumps and 6 solenoid valves to obtain an infinite intermittent flow through an arduino mega controller
.
i got stuck with the code:
i’m having an infinite loop and this is what i wanted, relay 2-6-7-8 are working, 3-4 are always on wich is what i want, but 1,5,9,10 are not working.
before using lastMillis1 = millis(); in each relay all relay was working but not 1 and i hadn’t an infinite loop.

i’m surely doing something wrong but i don’t get what, maybe you can help me.

this is the code:

/*
when relay is turned on a corresponding red light will light up

these pins link to 4 relay board pins:
pin 4 to IN1 - RELAY1(flush pump chamber 1)
pin 5 to IN2 - RELAY2(flush pump chamber 2)
pin 6 to IN3 - RELAY3(recirculating pump chamber 1)
pin 7 to IN4 - RELAY4(recirculated pump chamber 2)

these pins link to 8 relay board pins:
pin 8 to IN1 - RELAY5(flush solenoid chamber 1)
pin 9 to IN2 - RELAY6(flush solenoid chamber 2)
pin 10 to IN3 - RELAY7(recirculating solenoid chamber 1)
pin 11 to IN4 - RELAY8(recirculating solenoid chamber 1)
pin 12 to IN5 - RELAY9(recirculating solenoid chamber 2)
pin 13 to IN6 - RELAY10(recirculating solenoid chamber 2)
*/

#define RELAY1_ON_TIME 1 // this define the time in milliseconds when relay 1 turn on
#define RELAY1_OFF_TIME 3000 // this define the time in milliseconds when relay 1 turn off
#define RELAY2_ON_TIME 3000 // this define the time in milliseconds when relay 2 turn on
#define RELAY2_OFF_TIME 6000 // this define the time in milliseconds when relay 2 turn off
#define RELAY3_ON_TIME 0 // relay 3 is always on during the cycle
#define RELAY3_OFF_TIME 0
#define RELAY4_ON_TIME 0 // relay 4 is always on during the cycle
#define RELAY4_OFF_TIME 0
#define RELAY5_ON_TIME 0 // this define the time in milliseconds when relay 5 turn on
#define RELAY5_OFF_TIME 3000 // this define the time in milliseconds when relay 5 turn off
#define RELAY6_ON_TIME 3000 // this define the time in milliseconds when relay 6 turn on
#define RELAY6_OFF_TIME 6000 // this define the time in milliseconds when relay 6 turn off
#define RELAY7_ON_TIME 3000 // this define the time in milliseconds when relay 7 turn on
#define RELAY7_OFF_TIME 6000 // this define the time in milliseconds when relay 7 turn off
#define RELAY8_ON_TIME 3000 // this define the time in milliseconds when relay 8 turn on
#define RELAY8_OFF_TIME 6000 // this define the time in milliseconds when relay 8 turn off
#define RELAY9_ON_TIME 1 // this define the time in milliseconds when relay 9 turn on
#define RELAY9_OFF_TIME 3000 // this define the time in milliseconds when relay 9 turn off
#define RELAY10_ON_TIME 1 // this define the time in milliseconds when relay 10 turn on
#define RELAY10_OFF_TIME 3000 // this define the time in milliseconds when relay 2 turn off

byte relay1_state = LOW;
byte relay2_state = LOW;
byte relay3_state = LOW;
byte relay4_state = LOW;
byte relay5_state = LOW;
byte relay6_state = LOW;
byte relay7_state = LOW;
byte relay8_state = LOW;
byte relay9_state = LOW;
byte relay10_state = LOW;
unsigned long lastMillis1 = 0;
unsigned long lastMillis2 = 0;
unsigned long lastMillis3 = 0;
unsigned long lastMillis4 = 0;
unsigned long lastMillis5 = 0;
unsigned long lastMillis6 = 0;
unsigned long lastMillis7 = 0;
unsigned long lastMillis8 = 0;
unsigned long lastMillis9 = 0;
unsigned long lastMillis10 = 0;
byte relay1_pin = 4;
byte relay2_pin = 5;
byte relay3_pin = 6;
byte relay4_pin = 7;
byte relay5_pin = 8;
byte relay6_pin = 9;
byte relay7_pin = 10;
byte relay8_pin = 11;
byte relay9_pin = 12;
byte relay10_pin = 13;

void setup()
{
pinMode(relay1_pin, OUTPUT);
pinMode(relay2_pin, OUTPUT);
pinMode(relay3_pin, OUTPUT);
pinMode(relay4_pin, OUTPUT);
pinMode(relay5_pin, OUTPUT);
pinMode(relay6_pin, OUTPUT);
pinMode(relay7_pin, OUTPUT);
pinMode(relay8_pin, OUTPUT);
pinMode(relay9_pin, OUTPUT);
pinMode(relay10_pin, OUTPUT);
digitalWrite(relay1_pin, LOW);
digitalWrite(relay2_pin, LOW);
digitalWrite(relay3_pin, LOW);
digitalWrite(relay4_pin, LOW);
digitalWrite(relay5_pin, LOW);
digitalWrite(relay6_pin, LOW);
digitalWrite(relay7_pin, LOW);
digitalWrite(relay8_pin, LOW);
digitalWrite(relay9_pin, LOW);
digitalWrite(relay10_pin, LOW);
}

void loop()

{
if (millis() - lastMillis1 >= ((relay1_state == HIGH) ? RELAY1_OFF_TIME : RELAY1_ON_TIME)) {
lastMillis1 = millis();
relay1_state = !relay1_state;
digitalWrite(relay1_pin, relay1_state);}
if (millis() - lastMillis2 >= ((relay2_state == HIGH) ? RELAY2_OFF_TIME : RELAY2_ON_TIME)) {
lastMillis2 = millis();
relay2_state = !relay2_state;
digitalWrite(relay2_pin, relay2_state);}
if (millis() - lastMillis3 >= ((relay3_state == HIGH) ? RELAY3_OFF_TIME : RELAY3_ON_TIME)) {
lastMillis3 = millis();
relay3_state = !relay3_state;
digitalWrite(relay3_pin, relay3_state);}
if (millis() - lastMillis4 >= ((relay4_state == HIGH) ? RELAY4_OFF_TIME : RELAY4_ON_TIME)) {
lastMillis4 = millis();
relay4_state = !relay4_state;
digitalWrite(relay4_pin, relay4_state);}
if (millis() - lastMillis5 >= ((relay5_state == HIGH) ? RELAY5_OFF_TIME : RELAY5_ON_TIME)) {
lastMillis5 = millis();
relay5_state = !relay5_state;
digitalWrite(relay5_pin, relay5_state);}
if (millis() - lastMillis6 >= ((relay6_state == HIGH) ? RELAY6_OFF_TIME : RELAY6_ON_TIME)) {
lastMillis6 = millis();
relay6_state = !relay6_state;
digitalWrite(relay6_pin, relay6_state);}
if (millis() - lastMillis7 >= ((relay7_state == HIGH) ? RELAY7_OFF_TIME : RELAY7_ON_TIME)) {
lastMillis7 = millis();
relay7_state = !relay7_state;
digitalWrite(relay7_pin, relay7_state);}
if (millis() - lastMillis8 >= ((relay8_state == HIGH) ? RELAY8_OFF_TIME : RELAY8_ON_TIME)) {
lastMillis8 = millis();
relay8_state = !relay8_state;
digitalWrite(relay8_pin, relay8_state);}
if (millis() - lastMillis9 >= ((relay9_state == HIGH) ? RELAY9_OFF_TIME : RELAY9_ON_TIME)) {
lastMillis9 = millis();
relay9_state = !relay9_state;
digitalWrite(relay9_pin, relay9_state);}
if (millis() - lastMillis10 >= ((relay10_state == HIGH) ? RELAY10_OFF_TIME : RELAY10_ON_TIME)) {
lastMillis10 = millis();
relay10_state = !relay10_state;
digitalWrite(relay10_pin, relay10_state);}
}

Your code would be a lot shorter and easier to debug if you used arrays. It would be a lot easier for us to read if you used code tags.

Definitely move to using arrays for this, so so much easier.

but 1,5,9,10 are not working.

``````#define RELAY1_ON_TIME 1 // this define the time in milliseconds when relay 1 turn on
#define RELAY1_OFF_TIME 3000 // this define the time in milliseconds when relay 1 turn off
#define RELAY5_ON_TIME 0 // this define the time in milliseconds when relay 5 turn on
#define RELAY5_OFF_TIME 3000 // this define the time in milliseconds when relay 5 turn off
#define RELAY9_ON_TIME 1 // this define the time in milliseconds when relay 9 turn on
#define RELAY9_OFF_TIME 3000 // this define the time in milliseconds when relay 9 turn off
#define RELAY10_ON_TIME 1 // this define the time in milliseconds when relay 10 turn on
#define RELAY10_OFF_TIME 3000 // this define the time in milliseconds when relay 2 turn off
``````

How can you tell? All four of the relays which are not working have very short on times compared to the ones which are performing as expected.

Now that you are looping very fast, how to you detect the very short time period for the state before it changes.

What is the point of operating the relays in this fashion? The comments do not match what the code actually does. The times define a period of time when the relay is off, and a period of time when the relay is on.

Relay5 with a 0 time for the on period can not possibly work correctly.