Hi All i have a project compose of 7 button each button will on separately except 7 he is the all control of 6 button but even you

#define BUTTON_1 2
#define BUTTON_2 3
#define BUTTON_3 4
#define BUTTON_4 5
#define BUTTON_5 13
#define BUTTON_6 7
#define BUTTON_7 22

#define RELAY_1 8
#define RELAY_2 9
#define RELAY_3 10
#define RELAY_4 11
#define RELAY_5 12
#define RELAY_6 6
#define RELAY_7 23

unsigned long lastActionTime = 0;
const unsigned long timeout = 120000;

void setup() {

pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
pinMode(BUTTON_4, INPUT_PULLUP);
pinMode(BUTTON_5, INPUT_PULLUP);
pinMode(BUTTON_6, INPUT_PULLUP);
pinMode(BUTTON_7, INPUT_PULLUP); 


pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
pinMode(RELAY_5, OUTPUT);
pinMode(RELAY_6, OUTPUT);
pinMode(RELAY_7, OUTPUT); 


digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
digitalWrite(RELAY_3, LOW);
digitalWrite(RELAY_4, LOW);
digitalWrite(RELAY_5, LOW);
digitalWrite(RELAY_6, LOW);
digitalWrite(RELAY_7, LOW); 

}

void loop() {
// Read buttons (active LOW)
bool button1State = !digitalRead(BUTTON_1);
bool button2State = !digitalRead(BUTTON_2);
bool button3State = !digitalRead(BUTTON_3);
bool button4State = !digitalRead(BUTTON_4);
bool button5State = !digitalRead(BUTTON_5);
bool button6State = !digitalRead(BUTTON_6);
bool button7State = !digitalRead(BUTTON_7);

if (button1State) {
  activateRelay(1);
} else if (button2State) {
  activateRelay(2);
} else if (button3State) {
  activateRelay(3);
} else if (button4State) {
  activateRelay(4);
} else if (button5State) {
  activateRelay(5);
} else if (button6State) {
  activateRelay(0);
} else if (button7State) {
  activateRelay(7); // <-- New
}

if (millis() - lastActionTime > timeout) {
  activateRelay(0); // All ON
}

}

void activateRelay(int relayNumber) {

digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
digitalWrite(RELAY_3, HIGH);
digitalWrite(RELAY_4, HIGH);
digitalWrite(RELAY_5, HIGH);
digitalWrite(RELAY_6, HIGH);
digitalWrite(RELAY_7, HIGH); 


if (relayNumber == 1) {
  digitalWrite(RELAY_1, LOW);
} else if (relayNumber == 2) {
  digitalWrite(RELAY_2, LOW);
} else if (relayNumber == 3) {
  digitalWrite(RELAY_3, LOW);
} else if (relayNumber == 4) {
  digitalWrite(RELAY_4, LOW);
} else if (relayNumber == 5) {
  digitalWrite(RELAY_5, LOW);
}  else if (relayNumber == 7) {
  digitalWrite(RELAY_7, LOW);
} else if (relayNumber == 0) {
  digitalWrite(RELAY_1, LOW);
  digitalWrite(RELAY_2, LOW);
  digitalWrite(RELAY_3, LOW);
  digitalWrite(RELAY_4, LOW);
  digitalWrite(RELAY_5, LOW);
  digitalWrite(RELAY_6, LOW);
  digitalWrite(RELAY_7, LOW); 
}

lastActionTime = millis();  

}programming

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

Replace with code tags so we can read it properly, also what are you looking for with this post??

i want to know whats wrong on my code because its not working im not sure what wrong

Please read How to get the best out of this forum before posting

What do you mean 'not working'? Is it a syntax error? A functionality error? What is it supposed to do?

Try this:

const int BUTTONS[] = {2, 3, 4, 5, 13, 7, 22};
const int RELAYS[]  = {8, 9, 10, 11, 12, 6, 23};
const int NUM_RELAYS = sizeof(RELAYS) / sizeof(RELAYS[0]);

unsigned long lastActionTime = 0;
const unsigned long timeout = 120000;

void setup() {
  for (int i = 0; i < NUM_RELAYS; i++) {
    pinMode(BUTTONS[i], INPUT_PULLUP);
    pinMode(RELAYS[i], OUTPUT);
    digitalWrite(RELAYS[i], LOW); // All relays start ON (active LOW)
  }
}

void loop() {
  bool buttonPressed = false;

  for (int i = 0; i < NUM_RELAYS; i++) {
    if (!digitalRead(BUTTONS[i])) { // Button active LOW
      activateRelay(i);
      buttonPressed = true;
      break; // Only one relay active at a time
    }
  }

  if (!buttonPressed && millis() - lastActionTime > timeout) {
    activateRelay(-1); // Turn all relays ON after timeout
  }
}

void activateRelay(int relayNumber) {
  // Turn all relays OFF (inactive = HIGH)
  for (int i = 0; i < NUM_RELAYS; i++) {
    digitalWrite(RELAYS[i], HIGH);
  }

  // If valid relayNumber, turn it ON (active = LOW)
  if (relayNumber >= 0 && relayNumber < NUM_RELAYS) {
    digitalWrite(RELAYS[relayNumber], LOW);
  }

  lastActionTime = millis();
}


This fixes the issue of you not actually controlling relay 6, the stray 'programming' comment at the bottom, and uses loops and arrays instead of repetitive code

You wrote:

} else if (button7State) {
  activateRelay(7); // <-- New
}

But in your activateRelay function, relayNumber == 7 is not handled. You only handle:

else if (relayNumber == 7) {
  digitalWrite(RELAY_7, LOW);
}

But this just activates relay 7. You probably meant that button 7 should turn all relays ON.

THANKS IT WORK BUT when i start click the main button all relays are on which is correct but when i start clicking the 2nd button relay is turning off same on the other button it supposedly 2nd button will on

what do you need the code to do? What relay is turning off?? What other button??? Try this:

const int BUTTONS[] = {2, 3, 4, 5, 13, 7, 22};
const int RELAYS[]  = {8, 9, 10, 11, 12, 6, 23};
const int NUM_RELAYS = sizeof(RELAYS) / sizeof(RELAYS[0]);

unsigned long lastActionTime = 0;
const unsigned long timeout = 120000;

void setup() {
  for (int i = 0; i < NUM_RELAYS; i++) {
    pinMode(BUTTONS[i], INPUT_PULLUP);
    pinMode(RELAYS[i], OUTPUT);
    digitalWrite(RELAYS[i], HIGH); // Start with all relays OFF (inactive = HIGH)
  }
}

void loop() {
  bool anyButtonPressed = false;

  for (int i = 0; i < NUM_RELAYS; i++) {
    if (!digitalRead(BUTTONS[i])) { // Button is pressed (active LOW)
      digitalWrite(RELAYS[i], LOW); // Turn relay ON (active LOW)
      lastActionTime = millis();    // Update last action time
      anyButtonPressed = true;
    }
  }

  // Timeout: turn all relays ON (i.e., set them all to LOW)
  if (!anyButtonPressed && (millis() - lastActionTime > timeout)) {
    for (int i = 0; i < NUM_RELAYS; i++) {
      digitalWrite(RELAYS[i], LOW); // Turn relay ON (active LOW)
    }
  }
}

Whatever else y'all do, this

    if (!digitalRead(BUTTONS[i])) { // Button is pressed (active LOW)

is better (any correctly)

    if (digitalRead(BUTTONS[i]) == LOW) { // Button is pressed (active LOW)

and even better

// up top

# define PRESSED LOW

// eveywhere else

    if (digitalRead(BUTTONS[i]) == PRESSED) { // Button is pressed (active LOW)

which provides comment free clarity and one stop shopping for changing how switches need to be wired. Works for LEDs and relays and stuff too

# define ON LOW
# define OFF HIGH
//...

    digitalWrite(doorRelay, ON);

These days the advice will be to step away from # define, so

const auto ON = LOW;
const auto OFF = HIGH;

would be the way to go. I'm stuck in the 20th century in so many ways, so using define isn't a big deal for me.

a7

Hello wire12

Welcome to the best Aruino forum ever :slight_smile:

Ckeck this "Button Manager Simple" sketch.

//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Button Manager Simple"
#define NotesOnRelease "Arduino Mega tested"

// make names
enum TimerEvent {NotExpired, Expired};
enum ButtonState {Released, Pressed};
enum OnOff {Off, On};

//--------------------------------------------------
// make variables
constexpr uint8_t Buttons[] {A0, A1, A2, A3};
constexpr uint8_t Relays[] {9, 10, 11, 12};
//--------------------------------------------------

// make structures
struct TIMER
{
  uint32_t interval;
  uint32_t now;
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval;
    if (timerEvent == Expired) now = currentMillis;
    return timerEvent;
  }
};
struct BUTTON2RELAY
{
  uint8_t relay;
  uint8_t button;
  uint8_t stateOld;
  TIMER debounce;
  void make(uint8_t relay_, uint8_t button_)
  {
    relay = relay_;
    pinMode (relay, OUTPUT);
    button = button_;
    pinMode (button, INPUT_PULLUP);
    stateOld = digitalRead(button) ? Off : On;
    debounce = {20, 0};
  }
  void exec ( uint32_t currentMillis)
  {
    if (debounce.expired(currentMillis) == Expired)
    {
      uint8_t stateNew = digitalRead(button) ? Off : On;
      if (stateOld != stateNew)
      {
        stateOld = stateNew;
        if (stateOld == Pressed) digitalWrite(relay, digitalRead(relay) ? Off : On);
      }
    }
  }
};
//----------------------------------
BUTTON2RELAY button2relays[sizeof(Buttons)];
//----------------------------------
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application

void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  uint8_t index=0;
  for (auto &button2relay:button2relays)
  {
    button2relay.make(Relays[index],Buttons[index]);
    index++;
  }
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);
  for (auto &button2relay:button2relays) button2relay.exec(currentMillis); 
}

I interpret the title as: Buttons 1 through 6 are independent with button 7 the master.

did you forget 6?

look this over

const byte PinBut [] = { 2, 3,  4,  5, 13, 7, 22 };
const byte PinRly [] = { 8, 9, 10, 11, 12, 6, 23 }; 

const int  Npin      = sizeof(PinBut);
const int  AllOff    = -1;

enum { Off = LOW, On = HIGH };

const unsigned long Timeout        = 5000;      // 120000;
      unsigned long lastActionTime;

// -----------------------------------------------------------------------------
void activateRelay (
    int relayNumber )
{
    if (AllOff == relayNumber)
        for (int n = 0; n < Npin; n++)
            digitalWrite (PinRly [n], Off);

    else  {
        for (int n = 0; n < Npin; n++)
            digitalWrite (PinRly [n], On);

        digitalWrite (PinRly [relayNumber], Off);
    }

    lastActionTime = millis();
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (millis() - lastActionTime > Timeout)
        activateRelay (AllOff);

    for (int n = 0; n < Npin; n++)  {
        if (LOW == digitalRead (PinBut [n]))
            activateRelay (n);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    for (int n = 0; n < Npin; n++)  {
        pinMode (PinBut [n], INPUT_PULLUP);

        pinMode      (PinRly [n], OUTPUT);
        digitalWrite (PinRly [n], Off);
    }
}i

Hi, @wire12
Welcome to the forum.

Can you please tell us in point form;

What you want Button 1 to do?
What you want Button 2 to do?
What you want Button 3 to do?
What you want Button 4 to do?
What you want Button 5 to do?
What you want Button 6 to do?
What you want Button 7 to do?

If you can tell us the basically what you want your code to do, we may be able to help you better.

Can you please tell us what your native langauge is?

Tom.... :smiley: :+1: :coffee: :australia:

1 Like

We can tell it is not English because of the none sensical title of this thread.

@wire12

Normally we will require a schematic of your hardware. Hand drawn and photographed is fine, but physical layout diagrams like fritzing, or pictures connected lines is not.

This is because when using an embedded processor like the Arduino, the software is only ever half the story.

thanks man in advance

What you want Button 1 to do? On relay 1 and 2,3 then 4,5,6,7 is off
What you want Button 2 to do? On relay 2 and 3 while 1,4,5,6,7 off
What you want Button 3 to do? On relay 2,3 and 4 while 1,,4,5,6,7 off
What you want Button 4 to do? On relay 2,3 and 5 while 1,,4,6,7 off
What you want Button 5 to do? On relay 2,3 and 6 while 1,4,5,7 off
What you want Button 6 to do? On relay 2,3 and 7 while 1,4,5,6 off
What you want Button 7 to do? On relay 2,3 and 8 while 1,4,5,6,7 off

Hi, @wire12

Have you looked at Switch (case) Statement.
https://docs.arduino.cc/language-reference/en/structure/control-structure/switchCase/

https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase/

Each "case" being a button press and in the "case" function you have the "digitalWrite" statements for each combination of outputs.

Tom.... :smiley: :+1: :coffee: :australia:

yeah bro im still learning so maybe if you can do some examples for me

Hi, @wire12
When you release the button, do you want the relays to stay how they were set by that button, or all return to OFF?

What Arduino controller are you using?

Tom.... :smiley: :+1: :coffee: :australia:

yes bro stay how they set
im using arduino mega

Hello wire12o

What is the task of the programme in real life?