Beer Brewing System!! Serial relay control. Output is LOW, vs HIGH at reboot.

SUMMARY: Trying to toggle ON/OFF (HIGH/LOW) states for a relay using serial commands, but MUST have the initial state be HIGH (off).

I am using an arduino 2560 Mega to control a 16 channel relay board, which controls brewing heating elements & pumps. (I am going straight from the Arduino, to the board. Using a 12V 1A power supply for the board. All relay channels work.)

When I use this script, all the Digital Pins send LOW output (on). I am trying to make the system send HIGH(off), while allowing me to toggle on/off relays via serial commands. Reasoning is, I need all equipment to unpowered when I turn on my system.

Any help, I've been working at it for days now.

(I can define in a basic sketch digitalWrite(RELAY1, HIGH), but need to know how to toggle the state off/on. I can insert a delay, but turning the relay back off via serial is an unknown process to me)

PLEASE, PLEASE, THANK YOU, THANK YOU!

#define RELAY1 22
#define RELAY2 23
#define RELAY3 24
#define RELAY4 25
#define RELAY5 26
#define RELAY6 27
#define RELAY7 28
#define RELAY8 29
#define RELAY9 30
#define RELAY10 31
#define RELAY11 32
#define RELAY12 33
#define RELAY13 34
#define RELAY14 35
#define RELAY15 36
#define RELAY16 37

void setup() {

pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
pinMode(RELAY9, OUTPUT);
pinMode(RELAY10, OUTPUT);
pinMode(RELAY11, OUTPUT);
pinMode(RELAY12, OUTPUT);
pinMode(RELAY13, OUTPUT);
pinMode(RELAY14, OUTPUT);
pinMode(RELAY15, OUTPUT);
pinMode(RELAY16, OUTPUT);

Serial.begin(9600); // open serial
}

void loop() {
{
static int relayVal = 0;
int cmd;

while (Serial.available() > 0)
{
cmd = Serial.read();

switch (cmd)
{
case '1':
{
relayVal ^= 1; // xor current value with 1 (causes value to toggle)
if (relayVal)
Serial.println("Relay 1 on");
else
Serial.println("Relay 2 off");
break;
}
default:
{
Serial.println("Press enter just the relay number/letter to turn relays on/off");
}
}

if (relayVal)
digitalWrite(RELAY1, LOW);
else
digitalWrite(RELAY1, HIGH);
}
}
}

 digitalWrite(RELAY1, HIGH);
 pinMode(RELAY1, OUTPUT);
 digitalWrite(RELAY2, HIGH);
 pinMode(RELAY2, OUTPUT);
 digitalWrite(RELAY3, HIGH);
 pinMode(RELAY3, OUTPUT);
 digitalWrite(RELAY4, HIGH);
 pinMode(RELAY4, OUTPUT);
 digitalWrite(RELAY5, HIGH);
 pinMode(RELAY5, OUTPUT);
 digitalWrite(RELAY6, HIGH);
 pinMode(RELAY6, OUTPUT);
 digitalWrite(RELAY7, HIGH);
 pinMode(RELAY7, OUTPUT);
 digitalWrite(RELAY8, HIGH);
 pinMode(RELAY8, OUTPUT);
 digitalWrite(RELAY9, HIGH);
 pinMode(RELAY9, OUTPUT);
 digitalWrite(RELAY10, HIGH);
 pinMode(RELAY10, OUTPUT);
 digitalWrite(RELAY11, HIGH);
 pinMode(RELAY11, OUTPUT);
 digitalWrite(RELAY12, HIGH);
 pinMode(RELAY12, OUTPUT);
 digitalWrite(RELAY13, HIGH);
 pinMode(RELAY13, OUTPUT);
 digitalWrite(RELAY14, HIGH);
 pinMode(RELAY14, OUTPUT);
 digitalWrite(RELAY15, HIGH);
 pinMode(RELAY15, OUTPUT);
 digitalWrite(RELAY16, HIGH);
 pinMode(RELAY16, OUTPUT);

I can define in a basic sketch digitalWrite(RELAY1, HIGH), but need to know how to toggle the state off/on

You seem to already be doing that.

What you are NOT doing that would make life far easier is using arrays. Writing code one time makes more sense than writing it 16 times.

An array of 16 pin numbers and an array of 16 states would be useful. Set all the pin modes and states (to HIGH) in a for loop.

Toggling the nth element of a state array, and writing the state of the nth element of the state array to nth element of the pin arrray, are easy tasks.

byte relayArray[] = {22,23,24,25,26,27,28,29,29,30,31,32,33,34,35,36,};

In setup():

for (byte x=0; x<16; x=x+1){
pinMode (relayArray[x], OUTPUT);
digitalWrite (relayArray[x], HIGH);
}

To use in loop():

digitalWrite (relay[0], LOW); // turn on relay at pin D22
digitalWrite (relay[0], HIGH); // turn off relay at pin D22

Unless all of your relays do exactly the same thing you will find it easier to write and understand you code if you label your relays not by their position on the relay board but by what they actually do:

const unsigned char IntakePump=22;
const unsigned char DrainPump=23;
const unsigned char WaterHeater=24;
const unsigned char TransferPump=25;
const unsigned char relayPins[] = {IntakePump, DrainPump, WaterHeater, TransferPump,...};
const unsigned char relayCount = sizeof relayPins / sizeof relayPins[0];

void setup() {
  for (byte x=0; x< relayCount; x++) {
    pinMode (relayPins[x], OUTPUT);
    digitalWrite (relayPins[x], HIGH);
  }
}

void loop() {
...
    digitalWrite (IntakePump, LOW);
...
}

That way you don't need to always refer to your design document to see what "RELAYx" controls to know what the code is doing.