Problems running an 8 rrelay modual with an Arduino mega 2650

I am trying to run multiple combinations of relays at the same time to run hydraulics. For example 5 buttons 5 relays ( button 1 runs two relays at the same time, one for the pump solenoid and one valve)
Button 2 runs solenoid and a different valve. same for button 3. Button 4 runs solenoid and two valves. Button 5 runs 4 valves. Mind you all valves are controlled by a relay on the relay module. All controls have to be momentary contact non latching.

My problem is that some relays activate with brite red led's and others have a dim led and not activate the relay. its acting like a pulldown resister is being used but Im not calling for any.

Any help would be grateful

Button_relay2.ino (3.53 KB)

How are your buttons wired? In your code is the comment

// check if the pushbutton is pressed. If it is, the buttonState is LOW:

but your input modes are:

  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(button5Pin, INPUT);

Unless you have an external pullup resistor, you'll want to use INPUT_PULLUP for a SPST-NO momentary switch that grounds the pin when pressed.

You may also want to look into changing relay states only when a button is pressed/released and not when it is in the high or low state.

You have several relays that are controlled by more than one Button input, such as relay1 in the following segment of your code. If you press Button1, that will turn relay1 on, but since Button2 is not pressed, it will then be immediately turned back off. The same for any other relay that is controlled by multiple buttons, the pressed button will turn it on, and any other button that is not pressed will turn it back off.

  if (button1State == HIGH) {
    // turn RELAY on:
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay2Pin, HIGH);
  } else {
    // turn RELAY off:
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
  }
  if (button2State == HIGH) {
    // turn RELAY on:
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay3Pin, HIGH);
  } else {
    // turn RELAY off:
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay3Pin, LOW);
  }
  if (button3State == HIGH) {
    // turn RELAY on:
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
  } else {
    // turn RELAY off:
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay4Pin, LOW);
  }
  if (button4State == HIGH) {
    // turn RELAY on:
    digitalWrite(relay1Pin, HIGH);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
  } else {
    // turn RELAY off:
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(relay4Pin, LOW);
  }
  if (button5State == HIGH) {
    // turn RELAY on:
    digitalWrite(relay5Pin, HIGH);
    digitalWrite(relay2Pin, HIGH);
    digitalWrite(relay3Pin, HIGH);
    digitalWrite(relay4Pin, HIGH);
  } else {
    // turn RELAY off:
    digitalWrite(relay5Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(relay4Pin, LOW);
  }

The state of a relay output needs to depend on the state of multiple inputs, such as:

  if ((button1State == HIGH) || (button2State == HIGH) || (button3State == HIGH) || (button4State == HIGH)) {
    digitalWrite(relay1Pin, HIGH);
  } else {
    digitalWrite(relay1Pin, LOW);
  }

  if ((button1State == HIGH) || (button5State == HIGH)) {
    digitalWrite(relay2Pin, HIGH);
  } else {
    digitalWrite(relay2Pin, LOW);
  }

  if ((button2State == HIGH) || (button4State == HIGH) || (button5State == HIGH)) {
    digitalWrite(relay3Pin, HIGH);
  } else {
    digitalWrite(relay3Pin, LOW);
  }

  if ((button3State == HIGH) || (button4State == HIGH) || (button5State == HIGH)) {
    digitalWrite(relay4Pin, HIGH);
  } else {
    digitalWrite(relay4Pin, LOW);
  }

  if ((button5State == HIGH)) {
    digitalWrite(relay5Pin, HIGH);
  } else {
    digitalWrite(relay5Pin, LOW);
  }

(edit)
That code will most likely not work, since your inputs are active low, and I don't know if the relay is active high or low, but it should give a general idea of the logic needed.

Thank you I am so new to this Arduino world. I will give it a shot. the relay is a low active set up. the buttons have an led light in them which I want active at the same time the relay is on.