Go Down

Topic: multiple pushbutton control of relays (Read 255 times) previous topic - next topic

richardlong

Nov 12, 2019, 03:33 pm Last Edit: Nov 12, 2019, 11:38 pm by richardlong Reason: Corrected logical statement of relay 3. Changed from opens to closes.
Hi everyone,

could anyone provide an example code for the following?

I wish to use a number of Normally-Open pushbuttons to operate a number of Normally-Closed relays.  The logic is as follows:

Inputs: N pushbuttons
Outputs: N relays

each pushbutton corresponds to a single relay (pb1-->relay1,  pb2-->relay2 etc)

Pushing the nth pushbutton will Open all other relays and keep the nth relay closed.

Simultaneous operation of pushbuttons and relays is allowed.

e.g. pb2 and 4 pushed --> relay2 and 4 held closed, all other relays opened.  If pb 3 is pushed then relay 3 closes without affecting the stae of the other open relays.

Thanks for any help/advice

IoT_hobbyist

You can refer to this code: 5 push button and 5 relay
Code: [Select]

int buttonPins[] = {2, 3, 4, 5, 6};
int relayPins[] = {7, 8, 9, 10, 11};

void setup() {
  for(int i = 0; i < 5; i++)
    pinMode(buttonPins[i], INPUT_PULLUP);

  for(int i = 0; i < 5; i++)
    pinMode(relayPins[i], OUTPUT);
}

void loop() {
  for(int i = 0; i < 5; i++) {
    int buttonState = digitalRead(buttonPins[i]);
    digitalWrite(relayPins[i], buttonState);
  }
}


For relay, you can refer to this tutorial
For push-button, you can refer to this tutorial

sayHovis

Pushing the nth pushbutton will Open all other relays and keep the nth relay closed.

If pb 3 is pushed then relay 3 opens
Isn't that contradictory? I thought pb[n] pushed => relay[n] closed


Something like this (code is untested):


const byte nChannels = 5;
byte buttonPins[nChannels] = {1, 2, 3, 4, 5};
byte relayPins[nChannels] = {6, 7, 8, 9, 10};
boolean pressed[nChannels];

void setup() {

  for (byte i = 0; i < nChannels; i++) {
    pinMode(buttonPins, INPUT_PULLUP);
    pinMode(relayPins, OUTPUT);
  }

}

// Set relays for pressed buttons
void setRelays() {
  for (byte i = 0; i < nChannels; i++) {
    digitalWrite(relayPins, pressed);
  }
}

void loop() {
  // Assumen no buttons pressed
  for (byte i = 0; i < nChannels; i++) {
    pressed[nChannels] = false;
  }


  // Scan for pressed buttons, this will repeat while buttons are pressed to catch multiple button presses
  boolean updateRelays = false;
  boolean buttonPressed;
  do {
    buttonPressed = false;
    for (byte i = 0; i < nChannels; i++) {
      if (digitalRead(i) == HIGH) {
        buttonPressed = true;
        pressed = true;
        updateRelays = true;
        // Call setRelays() here for immediatly response
      }
    }
    delay(100); // A small delay to work with imperfect user timing when pressing multiple buttons.
  } while (buttonPressed);

  if (updateRelays) setRelays(); // Will first set relays when all buttons are released again

}

Paul__B

Something like this (code is untested):
And not posted in a useful fashion.

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window.  Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window.  But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read.  If you do not post it as "code" it can as you now see, be somewhat garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper.  People can usually see the mistakes directly and do not want to have to actually load it in their own IDE.  And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space.  Do use blank lines, but only single blanks between complete functional blocks.


OK, we have got over that one?

Here we go for "Radio Buttons".  Note that buttons always connect between pins and ground.
Code: [Select]
// Radio Buttons!
const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5;
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    // move on ready for next interval
    return true;
  }
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
    case 0: // Button up so far,
      if (button == HIGH) return false; // Nothing happening!
      else {
        *butnstate = 2;                 // record that is now pressed
        *marker = millis();             // note when was pressed
        return false;                   // and move on
      }

    case 1: // Button down so far,
      if (button == LOW) return false; // Nothing happening!
      else {
        *butnstate = 3;                 // record that is now released
        *marker = millis();             // note when was released
        return false;                   // and move on
      }

    case 2: // Button was up, now down.
      if (button == HIGH) {
        *butnstate = 0;                 // no, not debounced; revert the state
        return false;                   // False alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *butnstate = 1;               // jackpot!  update the state
          return true;                  // because we have the desired event!
        }
        else
          return false;                 // not done yet; just move on
      }

    case 3: // Button was down, now up.
      if (button == LOW) {
        *butnstate = 1;                 // no, not debounced; revert the state
        return false;                   // False alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *butnstate = 0;               // Debounced; update the state
          return false;                 // but it is not the event we want
        }
        else
          return false;                 // not done yet; just move on
      }
    default:                            // Error; recover anyway
      {
        *butnstate = 0;
        return false;                   // Definitely false!
      }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(button4, INPUT);
  digitalWrite (led1Pin, LOW);
  digitalWrite (led2Pin, LOW);
  digitalWrite (led3Pin, LOW);
  digitalWrite (led4Pin, LOW);
}

void loop() {
  // Select LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    digitalWrite (led1Pin, HIGH);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, HIGH);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, HIGH);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, HIGH);
  }
}



Go Up