binary code for relay control

Hello i am new to arnoido programming. I have the following situation: 1. remotely (with start, stop, up, down, left, right buttons). Each button generates a binary radio signal 2. a receiver converting the input radio signal into an output digital binary code (8 outputs with high / low signal = 00001001 (for example)). 3. Arduino inputs are connected to the outputs of the converter. 4. After the arduino, I have a board with 16 relays that I want to control with the generated binary code at the arduino input.

In practice, I have a wireless remote and relays. When I press a button on the remote control I want to activate a relay. + single press of the start button hold the "start" relay on. When the stop button is pressed, the start relay switches off.

It sounded very simple to me BUT programming turned out to be a difficult task for me.

My program so far is this one:

const int D1pin = 1;
const int D2pin = 2;
const int D3pin = 3;
const int D4pin = 4;
const int D5pin = 5;
const int D6pin = 6;
const int D7pin = 7;
const int D8pin = 8;
const int D21pinR1 = 21;
const int D22pinR2 = 22;
const int D23pinR3 = 23;
const int D24pinR4 = 24;
const int D25pinR5 = 25;
const int D26pinR6 = 26;
const int D27pinR7 = 27;
const int D28pinR8 = 28;
const int D29pinR9 = 29;
const int D30pinR10 = 30;
const int D31pinR11 = 31;
const int D32pinR12 = 32;
const int D33pinR13 = 33;
const int D34pinR14 = 34;
const int D35pinR15 = 35;
const int D36pinR16 = 36;



int D1pinState = 0;
int D2pinState = 0;
int D3pinState = 0;
int D4pinState = 0;
int D5pinState = 0;
int D6pinState = 0;
int D7pinState = 0;
int D8pinState = 0;

void setup() {
// put your setup code here, to run once:
//
pinMode(D1pin, INPUT);
pinMode(D2pin, INPUT);
pinMode(D3pin, INPUT);
pinMode(D4pin, INPUT);
pinMode(D5pin, INPUT);
pinMode(D6pin, INPUT);
pinMode(D7pin, INPUT);
pinMode(D8pin, INPUT); 
pinMode(D21pinR1, OUTPUT); //R1
pinMode(D22pinR2, OUTPUT); //R2
pinMode(D23pinR3, OUTPUT); //R3
pinMode(D24pinR4, OUTPUT); //R4
pinMode(D25pinR5, OUTPUT); //R5
pinMode(D26pinR6, OUTPUT); //R6
pinMode(D27pinR7, OUTPUT); //R7
pinMode(D28pinR8, OUTPUT); //R8
pinMode(D29pinR9, OUTPUT); //R9
pinMode(D30pinR10, OUTPUT); //R10
pinMode(D31pinR11, OUTPUT); //R11
pinMode(D32pinR12, OUTPUT); //R12
pinMode(D33pinR13, OUTPUT); //R13
pinMode(D34pinR14, OUTPUT); //R14
pinMode(D35pinR15, OUTPUT); //R15
pinMode(D36pinR16, OUTPUT); //R16

}

void loop() {

D1pinState=digitalRead(D1pin);
D2pinState=digitalRead(D2pin);
D3pinState=digitalRead(D3pin);
D4pinState=digitalRead(D4pin);
D5pinState=digitalRead(D5pin);
D6pinState=digitalRead(D6pin);
D7pinState=digitalRead(D7pin);
D8pinState=digitalRead(D8pin);

digitalWrite(D1pin,HIGH);
digitalWrite(D2pin,HIGH);
digitalWrite(D3pin,HIGH);
digitalWrite(D4pin,HIGH);
digitalWrite(D5pin,HIGH);
digitalWrite(D6pin,HIGH);
digitalWrite(D7pin,HIGH);
digitalWrite(D8pin,HIGH);


//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==LOW||D7pinState==LOW||D8pinState==LOW){

digitalWrite(D21pinR1,HIGH);

} else {
digitalWrite(D21pinR1,LOW);
}


//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==LOW||D6pinState==HIGH||D7pinState==HIGH||D8pinState==HIGH) {

digitalWrite(D22pinR2,HIGH);
} else {
digitalWrite(D22pinR2,LOW);
}

//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==LOW||D7pinState==HIGH||D8pinState==LOW) {

digitalWrite(D23pinR3,HIGH);
} else {
digitalWrite(D23pinR3,LOW);
}
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==LOW||D7pinState==LOW||D8pinState==HIGH) {

digitalWrite(D24pinR4,HIGH);
} else {
digitalWrite(D24pinR4,LOW);
}
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==LOW||D6pinState==HIGH||D7pinState==LOW||D8pinState==LOW) {

digitalWrite(D25pinR5,HIGH);
} else {
digitalWrite(D25pinR5,LOW);
} 
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==HIGH||D7pinState==HIGH||D8pinState==LOW) {

digitalWrite(D26pinR6,HIGH);
} else {
digitalWrite(D26pinR6,LOW);
} 
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==HIGH||D7pinState==LOW||D8pinState==HIGH) {

digitalWrite(D27pinR7,HIGH);
} else {
digitalWrite(D27pinR7,LOW);
} 
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==HIGH||D7pinState==LOW||D8pinState==LOW) {

digitalWrite(D28pinR8,HIGH);
} else {
digitalWrite(D28pinR8,LOW);
} 


//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==LOW||D6pinState==HIGH||D7pinState==HIGH||D8pinState==LOW) {

digitalWrite(D29pinR9,HIGH);
} else {
digitalWrite(D29pinR9,LOW);
} 
//////////////////////////////////////////////

if (D1pinState==LOW||D2pinState==LOW||D3pinState==LOW||D4pinState==LOW||D5pinState==HIGH||D6pinState==LOW||D7pinState==HIGH||D8pinState==HIGH) {

digitalWrite(D30pinR10,HIGH);
} else {
digitalWrite(D3

Hello Rady,

Welcome to the forum. Please read 'how to use this forum - please read' (there's a clue in the title), the go back and modify your post in line with the instructions, especially item #7.

Thank you.

Does each button control ONE input or does each button control a pattern of EIGHT inputs?

johnwasser:
Does each button control ONE input or does each button control a pattern of EIGHT inputs?

Does each button control a pattern of EIGHT inputs.

This is a table of the codes generated by the remote control.

One thing to keep in mind, is that when you decode a binary message in parallel as noted, you need to specify a settling time so that ‘late’ bits don’t upset your decoder.

For this reason, if you don’t allow for this, you have to supply a strobe to latch the data.

It may help to describe the whole project, and parts you have - which may suggest an alternative to tossing binary control words around in parallel. There are likely to be straightforward methods (e.g. shift registers) that can make your job easier.

Rady:
Does each button control a pattern of EIGHT inputs.
This is a table of the codes generated by the remote control.

OK. So I’d put all of the inputs in a byte:

void loop()
{
  byte command =
    ((digitalRead(D1pin) != LOW) ? 128 : 0) +
    ((digitalRead(D2pin) != LOW) ? 64 : 0) +
    ((digitalRead(D3pin) != LOW) ? 32 : 0) +
    ((digitalRead(D4pin) != LOW) ? 16 : 0) +
    ((digitalRead(D5pin) != LOW) ? 8 : 0) +
    ((digitalRead(D6pin) != LOW) ? 4 : 0) +
    ((digitalRead(D7pin) != LOW) ? 2 : 0) +
    ((digitalRead(D8pin) != LOW) ? 1 : 0);


  switch (comand)
  {
    case B00001000: // Speed up
      Serial.println("Speed up");
      break;


    case B00000111: // Speed down
      Serial.println("Speed down");
      break;


    default:
      Serial.print(F("Unrecognized comand: B"));
      Serial.println(command, BIN);
  }
}

I will try to add a multiplexer 74HC4051 . Will this work for me? What do you think?

If you are using the 74HC4051 as in input multiplexer then, yes, it will allow you you to read the 8 inputs, one at a time, with four I/O pins.

If you are trying to save pins, a CD4021B shift register would be much better. It will read the same 8 inputs as a single byte with three I/O pins and one function call. See the shiftIn() tutorial:

https://www.arduino.cc/en/tutorial/ShiftIn

johnwasser: OK. So I'd put all of the inputs in a byte:

void loop()
{
  byte command =
    ((digitalRead(D1pin) != LOW) ? 128 : 0) +
    ((digitalRead(D2pin) != LOW) ? 64 : 0) +
    ((digitalRead(D3pin) != LOW) ? 32 : 0) +
    ((digitalRead(D4pin) != LOW) ? 16 : 0) +
    ((digitalRead(D5pin) != LOW) ? 8 : 0) +
    ((digitalRead(D6pin) != LOW) ? 4 : 0) +
    ((digitalRead(D7pin) != LOW) ? 2 : 0) +
    ((digitalRead(D8pin) != LOW) ? 1 : 0);

  switch (comand)   {     case B00001000: // Speed up       Serial.println("Speed up");       break;

    case B00000111: // Speed down       Serial.println("Speed down");       break;

    default:       Serial.print(F("Unrecognized comand: B"));       Serial.println(command, BIN);   } }

With your help, I got to logging each command remotely as a bit code. :)

How can I configure each command to activate a relay?

Rady: How can I configure each command to activate a relay?

You can turn relays on and off in the switch/case statement cases.

johnwasser:
You can turn relays on and off in the switch/case statement cases.

 #include <SPI.h>
#include <Arduino.h> 
 
 
 
 int D1pin = 2;
 int D2pin = 3;
 int D3pin = 4;
 int D4pin = 5;
 int D5pin = 6;
 int D6pin = 7;
 int D7pin = 8;
 int D8pin = 9;
 int D21pinR1 = 21; // Speed up   (impuls)_B00001000
 int D22pinR2 = 22; // Speed down (impuls)_B00000111
 int D23pinR3 = 23; // Up arrow   (impuls)_B00001010
 int D24pinR4 = 24; // Down arrow (impuls)_B00001001
 int D25pinR5 = 25; // 
 int D26pinR6 = 26;
 int D27pinR7 = 27;
 int D28pinR8 = 28;
 int D29pinR9 = 29;
 int D30pinR10 = 30;
 int D31pinR11 = 31;
 int D32pinR12 = 32;
 int D33pinR13 = 33;
 int D34pinR14 = 34;
 int D35pinR15 = 35;
 int D36pinR16 = 36;

  
 int D1pinState = 0;
 int D2pinState = 0;
 int D3pinState = 0;
 int D4pinState = 0;
 int D5pinState = 0;
 int D6pinState = 0;
 int D7pinState = 0;
 int D8pinState = 0;
 
 
 
void setup() {
  // put your setup code here, to run once:
  
  
  pinMode(D1pin, INPUT);
  pinMode(D2pin, INPUT);
  pinMode(D3pin, INPUT);
  pinMode(D4pin, INPUT);
  pinMode(D5pin, INPUT);
  pinMode(D6pin, INPUT);
  pinMode(D7pin, INPUT);
  pinMode(D8pin, INPUT);     
  pinMode(D21pinR1, OUTPUT);      //R1
  pinMode(D22pinR2, OUTPUT);     //R2
  pinMode(D23pinR3, OUTPUT);     //R3
  pinMode(D24pinR4, OUTPUT);     //R4
  pinMode(D25pinR5, OUTPUT);      //R5
  pinMode(D26pinR6, OUTPUT);      //R6
  pinMode(D27pinR7, OUTPUT);      //R7
  pinMode(D28pinR8, OUTPUT);      //R8
  pinMode(D29pinR9, OUTPUT);      //R9
  pinMode(D30pinR10, OUTPUT);      //R10
  pinMode(D31pinR11, OUTPUT);      //R11
  pinMode(D32pinR12, OUTPUT);      //R12
  pinMode(D33pinR13, OUTPUT);      //R13
  pinMode(D34pinR14, OUTPUT);      //R14
  pinMode(D35pinR15, OUTPUT);      //R15
  pinMode(D36pinR16, OUTPUT);      //R16
  digitalWrite(D21pinR1,HIGH);
  digitalWrite(D22pinR2,HIGH);
  digitalWrite(D23pinR3,HIGH);
  digitalWrite(D24pinR4,HIGH);
  digitalWrite(D25pinR5,HIGH);
  digitalWrite(D26pinR6,HIGH);
  digitalWrite(D27pinR7,HIGH);
  digitalWrite(D28pinR8,HIGH);
  digitalWrite(D29pinR9,HIGH);
  digitalWrite(D30pinR10,HIGH);
  digitalWrite(D31pinR11,HIGH);
  digitalWrite(D32pinR12,HIGH);
  digitalWrite(D33pinR13,HIGH);
  digitalWrite(D34pinR14,HIGH);
  digitalWrite(D35pinR15,HIGH);
  digitalWrite(D36pinR16,HIGH);
  
  
  
  
  Serial.begin(9600);

}

void loop() 
 

{

  byte command =
    ((digitalRead(D1pin) != HIGH) ? 128 : 0) +
    ((digitalRead(D2pin) != HIGH) ? 64 : 0) +
    ((digitalRead(D3pin) != HIGH) ? 32 : 0) +
    ((digitalRead(D4pin) != HIGH) ? 16 : 0) +
    ((digitalRead(D5pin) != HIGH) ? 8 : 0) +
    ((digitalRead(D6pin) != HIGH) ? 4 : 0) +
    ((digitalRead(D7pin) != HIGH) ? 2 : 0) +
    ((digitalRead(D8pin) != HIGH) ? 1 : 0);
 

  switch (command)
  {
    
    case B00001111:                 ///// Manual once
      Serial.println("Manual once");
      
      break;
      
    case B00011111:               ///// Manual twich
      Serial.println("Manual twich");
      
      break;

    case B00101111:               ///// Manual three
      Serial.println("Manual three");
      
      break;
                
    case B00001000: // Speed up
      Serial.println("Speed up");
      digitalWrite(D21pinR1,LOW);
      delay(100);
      digitalWrite(D21pinR1,HIGH);

      break;
         
   case B00000111: // Speed down
      Serial.println("Speed down");
      digitalWrite(D22pinR2,LOW);
      delay(100);
      digitalWrite(D22pinR2,HIGH);
      break;

    case B00001010: // UP arrow
      Serial.println("UP arrow");
      digitalWrite(D23pinR3,LOW);
      delay(100);
      digitalWrite(D23pinR3,HIGH);
      break;

    case B00001001: // Down arrow
    Serial.println("Down arrow");
    digitalWrite(D24pinR4,LOW);
    delay(100);
    digitalWrite(D24pinR4,HIGH);
    break;

    case B00000100: // Left arrow
    Serial.println("Left arrow");
    digitalWrite(D25pinR5,LOW);
    delay(100);
    digitalWrite(D25pinR5,HIGH);
    break;

    case B00001110: // Right arrow
    Serial.println("Right arrow");
    digitalWrite(D26pinR6,LOW);
    delay(100);
    digitalWrite(D26pinR6,HIGH);
    break;

    case B00001101: // Torch UP
    Serial.println("Torch UP");
    digitalWrite(D27pinR7,LOW);
    delay(100);
    digitalWrite(D27pinR7,HIGH);
    break;    
    
    case B00001100: // Torch DOWN
    Serial.println("Torch DOWN");
    digitalWrite(D28pinR8,LOW);
    delay(100);
    digitalWrite(D28pinR8,HIGH);
    break;

    case B00000110: // Start
    Serial.println("Start");
    digitalWrite(D31pinR11,LOW);
    digitalWrite(D32pinR12,LOW);
    delay(5000);
    digitalWrite(D29pinR9,LOW);
    delay(1000);
    digitalWrite(D33pinR13,LOW);
    break;

    case B00001011: // Stop
    Serial.println("Stop");
    digitalWrite(D33pinR13,HIGH);
    delay(1000);
    digitalWrite(D29pinR9,HIGH);
    delay(5000);
    digitalWrite(D31pinR11,HIGH);
    digitalWrite(D32pinR12,HIGH);   
    break;

    case B00000010: // Back
    Serial.println("Back");
    digitalWrite(D30pinR10,LOW);
    delay(100);
    digitalWrite(D30pinR10,HIGH);
    break;

        
    case B00000011: // Forward
    Serial.println("Forward");    
    digitalWrite(D29pinR9,LOW);
    delay(100);
    digitalWrite(D29pinR9,HIGH);    
    break;

    case B00000101: // Ignition
    Serial.println("Ignition");
    break;
    
    case B00000001: // Cutting
    Serial.println("Cutting");
    break;  

    case B00110000: // Preheat
    Serial.println("Preheat");
    break;
     
    case B01000000: // Plasma arc
    Serial.println("Plasma arc");
    break;
    
    case B11110000: // Close
    Serial.println("Close");
    break;    
        
    default:
      Serial.print(F("Unrecognized comand: B"));
      Serial.println(command, BIN);


  
  
  }
  
}

This result is satisfying for me to produce properly built code.

Thanks for the correct intervention!