HELP - Use 8 buttons to drive 8 relays using a 4bit (binary mode)

I'm a novice with no experience in Arduino and new in this forum, sorry if I made a mistake. I would like, use an Arduino UNO to drive with 8 buttons, 8 relays connected with 6 cables. Two cables for power supply 12v (for relays and IC) and 4 cables for the signals (0-5V). This board have one CD4028 (with a pull down resistance included) to pilot two ULN2004 driver for relays. I'm trying to use these 8 buttons to pilot relays card, using 4bit in binary, but it is so hard for me. I wish the press of a button, enable the corresponding relay and it remain active until you press another button, which deactivates the previous relay and activates the new one, concerning the button pressed. I would like to reserve also the ability to turn off all the relays in case of one particular event from program. I tried with one with two and 4 buttons and little sketch test run, but with 8 relays no !?? Anyone can help me in this. Following my program, maybe filled of errors ...

/* Binary Relays ON, 4 bit binary method using 4 pin for output and 8 pin for input Accensione del relè in modalità binaria a 4bit, usati 4 pin per l'input e 8 pin per l'output output pin 10,11,12,13 (relays 1,2,3,4) input pin 0,1,2,3,4,5,6,7 (buttons 1,2,3,4,5,6,7,8) NO button pressed = 0000 => pin 10,11,12,13 button1,2,3,4,5,6,7,8 pressed => pin 10,12,13,14 => 1000,0100,0010,0001,1100,1010,1001,1110 */ int relay1=10; // assegno n alla variabile intera relay int relay2=11; int relay3=12; int relay4=13;

int button1=0; // assegno n alla variabile intera button int button2=1; int button3=2; int button4=3; int button5=4; int button6=5; int button7=6; int button8=7;

int button1_status=0; int button2_status=0; int button3_status=0; int button4_status=0; int button5_status=0; int button6_status=0; int button7_status=0; int button8_status=0;

void setup() { pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); /*

Resistenze di pull up 10K resistenza verso button 330 Ohm R1 = 10 K R2 = 330 Ohm

Int GND / _R1___ +5V | R2 | Pin (button-INPUT)

*/ pinMode(button1 , INPUT); pinMode(button2 , INPUT); pinMode(button3 , INPUT); pinMode(button4 , INPUT); pinMode(button5 , INPUT); pinMode(button6 , INPUT); pinMode(button7 , INPUT); pinMode(button8 , INPUT);

// imposta i pulsanti in LOW digitalWrite(button1, button1_status); digitalWrite(button2, button2_status); digitalWrite(button3, button3_status); digitalWrite(button4, button4_status); digitalWrite(button5, button5_status); digitalWrite(button6, button6_status); digitalWrite(button7, button7_status); digitalWrite(button8, button8_status);

}

void loop() { button1_status = digitalRead(button1); // lettura dell'ingresso if (button1_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, HIGH); // accende il RELE 1 digitalWrite(relay2, LOW ); digitalWrite(relay3, LOW ); digitalWrite(relay4, LOW ); } // else if (button1_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button2_status = digitalRead(button2); // lettura dell'ingresso if (button2_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, LOW ); // accende il RELE 2 digitalWrite(relay2, HIGH); digitalWrite(relay3, LOW ); digitalWrite(relay4, LOW ); } // else if (button2_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button3_status = digitalRead(button3); // lettura dell'ingresso if (button3_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, LOW ); // accende il RELE 3 digitalWrite(relay2, LOW ); digitalWrite(relay3, HIGH); digitalWrite(relay4, LOW ); } // else if (button3_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button4_status = digitalRead(button4); // lettura dell'ingresso if (button4_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, LOW ); // spegne tutti i RELE digitalWrite(relay2, LOW ); digitalWrite(relay3, LOW ); digitalWrite(relay4, HIGH); } // else if (button4_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button5_status = digitalRead(button5); // lettura dell'ingresso if (button5_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, HIGH); // spegne tutti i RELE digitalWrite(relay2, HIGH); digitalWrite(relay3, LOW ); digitalWrite(relay4, LOW ); } // else if (button4_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button6_status = digitalRead(button4); // lettura dell'ingresso if (button6_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, HIGH); // spegne tutti i RELE digitalWrite(relay2, LOW ); digitalWrite(relay3, HIGH); digitalWrite(relay4, LOW ); } // else if (button6_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button7_status = digitalRead(button4); // lettura dell'ingresso if (button7_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, HIGH); // spegne tutti i RELE digitalWrite(relay2, LOW ); digitalWrite(relay3, LOW ); digitalWrite(relay4, HIGH); } // else if (button7_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

button8_status = digitalRead(button4); // lettura dell'ingresso if (button8_status == HIGH) { // controlla se il pulsante è premuto digitalWrite(relay1, HIGH); // spegne tutti i RELE digitalWrite(relay2, HIGH); digitalWrite(relay3, HIGH); digitalWrite(relay4, LOW ); } // else if (button8_status == LOW) { // controlla se il pulsante viene rilasciato // digitalWrite(relay1, LOW); // spegne tutti i RELE // digitalWrite(relay2, LOW); // digitalWrite(relay3, LOW); // digitalWrite(relay4, LOW); // }

if (button1_status == LOW || button2_status == LOW || button3_status == LOW || button4_status == LOW || button5_status == LOW || button6_status == LOW || button7_status == LOW || button8_status == LOW ) { digitalWrite(relay1, LOW); // spegne tutti i RELE digitalWrite(relay2, LOW); digitalWrite(relay3, LOW); digitalWrite(relay4, LOW);

}

}

For the buttons, just the bounce library, it’s easy to use. Then the loop becomes:

Bounce buttons[8];
byte buttonPins[] = {0, 1, 2, 3, 4, 5, 6, 7};
byte outputs[] = {10, 11, 12, 13};

void setup(){
  //buttons
  for(byte i = 0; i < 8; i++){
    pinMode(buttonPins[i], INPUT_PULLUS);
    buttons[i].attach(buttonPins[i]);
  }
  
  //outputs
  for(byte i = 0; i < 4; i++){
    pinMode(outputs[i], OUTPUT);
  }
}

void loop(){
  //scan all buttons
  for(byte i = 0; i < 8; i++){
    //if a button is pressed
    if(buttons.read() == LOW){
      driveOutput(i);
      break;
    }
  }
}

void driveOutput(byte in){
  //spead the BCD over the outputs
  for(byte i = 0; i < 4; i++){
    digitalWrite(outputs[i], in && (1 << i));
  }
}

Do not use pins 0 and 1 as it may affect your ability to upload your program.

This is wrong. GND / _R1___ +5V | R2 | Pin (button-INPUT)

It should be: GND | R1 = 10K |_______/ __ +5V | Pin

Ciao Septillion and Hery_best,
many thanks for your help about.
Sorry for delay in answering but I take some time to understand the library and also the new programming method.

Septillion, you tell me to include a new library called Bounce, I found it in internet but now it is called Bounce2 and inside i think there is the old one called Bounce1.

I tryed to use the new one Bounce2 and also the old but i receive in both, lot of errors during verify.
Really i don’t understand the reasons.

At my age I have a German gentleman who chases me (It is called… Altzh… but now I do not remember the right name) and it is easy to make mistakes, but many years ago I programmed a lot.

Thanks also to Henry_Best that tell me to not use pin 0 and 1 to avoid cut the communication to the serail port for programming Arduino and also to give me more info about the connection circuit.
This forum is full of nice guys, this is very good !!

So, returning to my sketch I understand that Septillion charge an array “buttonPins” with these values { 0, 1, 2, 3, 4, 5, 6, 7} first question: if I don’t want to use the pin 0 and 1 as suggested from Henry, I need to change it in:
“byte buttonPins = {2, 3, 4, 5, 6, 7, 8, 9};” right ?

Then I have modified the connection as Henry suggest me.

To avoid some errors I modifyed the sketch in this way and I hope it is correct !?
Now I tryed to use the Septillion sketch but dont run at all :

In attach the sketch i don’t understand way the “square parenthesis” don’t appear !!

#include <Bounce2.h>

Bounce buttons[8] = Bounce();

byte buttonPins = {2, 3, 4, 5, 6, 7, 8, 9};
byte outputs = {10, 11, 12, 13};

void setup(){
//buttons
for(byte i = 0; i < 8; i++){
pinMode(buttonPins*, INPUT);*
buttons.attach(buttonPins_); // buttons SP i SP.attach(buttonPins*)
}*_

* //outputs*
* for(byte i = 0; i < 4; i++){*
_ pinMode(outputs*, OUTPUT);
}
}
void loop(){
//scan all buttons*

* for(byte i = 0; i < 8; i++){
//if a button is pressed*

if(buttons*.read() == LOW){ // I write (buttons SP i SP.read() == LOW) but don’t change !
driveOutput(i);
break;
}
}
}
void driveOutput(byte in){
//spead the BCD over the outputs*

* for(byte i = 0; i < 4; i++){
digitalWrite(outputs, in && (1 << i));
}
}_

rele_pulsanti.ino (696 Bytes)
rele_pulsanti.ino (696 Bytes)*