Button with binary encoder

Do you have another easy method? :roll_eyes:

Do you have another easy method?

To do what? Why would anyone number 2 out of three variables, instead of all 3?

Something like this, will do what you want:

#define DIM 3
const int pin[DIM] = {2, 4, 6};


void setup(void) {
  
  Serial.begin(9600);
  
  for (int i=0; i< DIM; i++) {
    pinMode(pin[i], INPUT);
  }
}

void loop(void) {
int decimal=0;

  for (int i; i<DIM; i++) {
    decimal = decimal*2 + !digitalRead(pin[DIM-i-1]);
  }
  
  Serial.print("0x");
  Serial.println(decimal, HEX);
  
  delay(1000);
}

Could be a load of rubbish…but this?
Could maybe use an array to hold the byte values and reference them from a for loop with i=pin number as a slightly more “even cleaner” code.

void setup() {
  // put your setup code here, to run once:

 Serial.begin(9600);

//INPUT mode for pins 2 4 and 6.
for (int i=2; i<7;i=+2){
  pinMode(i,INPUT);
}


}

void loop() {
 
  
//some bytes that are the value of each pin.
byte pin2 = 0b00000001;
byte pin4 = 0b00000010;
byte pin6 = 0b00000100;

//a byte to hold the total.  
byte total = 0b00000000;
  
  if (digitalRead(2)!=0){
    total =+ pin2;
  }
  
  if (digitalRead(4)!=0){
    total =+ pin4;
  }
  
    if (digitalRead(6)!=0){
    total =+ pin6;
  }
  
  Serial.println(total,DEC);
  delay(1000);
  

}

Johnny010:
Could be a load of rubbish…but this?
Could maybe use an array to hold the byte values and reference them from a for loop with i=pin number as a slightly more “even cleaner” code.

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

//INPUT mode for pins 2 4 and 6.
for (int i=2; i<7;i=+2){
  pinMode(i,INPUT);
}

}

void loop() {

//some bytes that are the value of each pin.
byte pin2 = 0b00000001;
byte pin4 = 0b00000010;
byte pin6 = 0b00000100;

//a byte to hold the total. 
byte total = 0b00000000;
 
  if (digitalRead(2)!=0){
    total =+ pin2;
  }
 
  if (digitalRead(4)!=0){
    total =+ pin4;
  }
 
    if (digitalRead(6)!=0){
    total =+ pin6;
  }
 
  Serial.println(total,DEC);
  delay(1000);

}

Do your code compile? What it’s supose to do:

    total =+ pin6;

No idea if this will work…I am a noob.

void setup() {
  // put your setup code here, to run once:

 Serial.begin(9600);

//INPUT mode for pins 2 4 and 6.
for (int i=2; i<7;i=+2){
  pinMode(i,INPUT);
}



}

void loop() {
 

//some bytes that are the value of each pin.
char pin_values[3]={0,2,4};

//a byte to hold the total.  
byte total = 0b00000000;
  
  for (int i=1; i<4 ; i++){

 // Will read pins 2,4,6. If digital HIGH, add the byte value to the byte total.
 
   if (digitalRead((i*2) > 0) 
   total = total + pin_values[i];
  }
  
  
  
  Serial.println(total,DEC);
  delay(1000);
  

}

luisilva: Do your code compile? What it's supose to do:

    total =+ pin6;

It complies in my Arduino enviroment?

I meant total += pin6 but it seems to compile without errors :S.

Johnny010: It complies in my Arduino enviroment?

I meant total += pin6 but it seems to compile without errors :S.

But there are different things '+=' and '=+'.

Why are people using plus rather than bitwise or?

int v = 
    (digitalRead(2) ? 1 : 0)
  | (digitalRead(4) ? 2 : 0)
  | (digitalRead(6) ? 4 : 0);

If HIGH is 1 (and not -1 or something else)

int v = 
    (digitalRead(2) )
  | (digitalRead(4) << 1)
  | (digitalRead(6) << 2);

BTW, please don’t call your result ‘decimal’. It’s a mistake to think in terms of numbers inside the arduino being decimal or binary: they are all binary (unless they are BCD, whatever). Decimal is an artefact of the way they are printed out by Stream.print, and of how the compiler interprets the way your code is written. The number itself is a number.

Paul's second code it most likely the fastest. It requires no jumping or program flow words. It is the way I've done it in the past. Dwight