Make four different inputs = value

Still new to programing. I have a code that works perfect but is long and would like make it simple and easier to change if need be.
I have four values, I would like to take those and make them equal to a word or even something like 0100. So i can go like this
if (0100 == true) {
digitalWrite(ccwPin, LOW);}

this is what I have now that works

if ((oneValue == 0) && (twoValue == 1) && (threeValue == 0) && (fourValue == 0)) { // readings from digital inputs
      digitalWrite(ccwPin, LOW);
}

Please post your entire sketch so we can grasp the context of the snippet you posted. The need is explained here:

I can tell you, in general what you propose is a bad idea, it makes anonymous the variables that are presently easily identifiable. Future maintenance is easier to perform on if statements like the one you posted, than on an obscure numerical code.

If there are too many of those 'if' statements, it's possible that your programming logic is inefficient or the approach is repetive and doesn't benefit from code factoring and indexed container features like arrays and structs that also can be used to avoid repetition.

maybe

{
    byte val;
    val  = oneValue;
    val |= twoValue << 1;
    val |= threeValue << 2;
    val |= fourValue << 3;

    digitalWrite(ccwPin, 0x4 == val);
}

So here is a loop that reads the inputs. I would like to assign a value(encoder) to each of the possible values.
I played with the byte shift << but I think that isn't what I'm looking for.
This code compiled but didn't work.

byte encoder;

void encoderLoop() {
  oneValue = digitalRead(codeOne);      // reads encoder 1
  twoValue = digitalRead(codeTwo);      // reads encoder 2
  threeValue = digitalRead(codeThree);  // reads encoder 3
  fourValue = digitalRead(codeFour);  // reads encoder 4
  
  
  if ((oneValue == 0) && (twoValue == 1) && (threeValue == 0) && (fourValue == 1)) {
    encoder = 0;
  }  //
  else if ((oneValue == 0) && (twoValue == 1) && (threeValue == 0) && (fourValue == 1)) {
    encoder = 1;
  }  //
  
  else if ((oneValue == 0) && (twoValue == 1) && (threeValue == 0) && (fourValue == 0)) {
    encoder = 2;
  }  //    
  else if ((oneValue == 0) && (twoValue == 0) && (threeValue == 0) && (fourValue == 0)) {
    encoder = 3;  
  } //
  else if ((oneValue == 0) && (twoValue == 0) && (threeValue == 0) && (fourValue == 1)) {
    encoder = 4;
  }  //
  else if ((oneValue == 1) && (twoValue == 0) && (threeValue == 0) && (fourValue == 1)) {
    encoder = 5;
  }  //
  else if ((oneValue == 1) && (twoValue == 0) && (threeValue == 0) && (fourValue == 0)) {
    encoder = 6;
  }  //
  else if ((oneValue == 1) && (twoValue == 0) && (threeValue == 1) && (fourValue == 0)) {
    encoder = 7;
  }  //
  else if ((oneValue == 0) && (twoValue == 0) && (threeValue == 1) && (fourValue == 0)) {
    encoder = 8;
  }  // 
}```

So here is my part of my main code to get a better idea of what I have going on

 encoderLoop();

  //Shift
  if (((modeSwitch == 1) && (oneValue == 0) && (twoValue == 0) && (threeValue == 0) && (fourValue == 0)) ||  //
      ((modeSwitch == 1) && (oneValue == 0) && (twoValue == 0) && (threeValue == 0) && (fourValue == 1)) ||  //
      ((modeSwitch == 1) && (oneValue == 1) && (twoValue == 0) && (threeValue == 0) && (fourValue == 1)) ||  //
      ((modeSwitch == 1) && (oneValue == 1) && (twoValue == 0) && (threeValue == 0) && (fourValue == 0)) ||  //
      ((modeSwitch == 1) && (oneValue == 1) && (twoValue == 0) && (threeValue == 1) && (fourValue == 0)) ||  //
      ((modeSwitch == 1) && (oneValue == 0) && (twoValue == 0) && (threeValue == 1) && (fourValue == 0)))    // This whole first part is still a working code
  {
    digitalWrite(ccwPin, HIGH);
    Serial.println("shift initated");
    while (ccwPin, HIGH) {
      Serial.println("shift in progress");
            encoderLoop(); 
    
    if (encoder == 2) { // 
      digitalWrite(ccwPin, LOW);
      Serial.println("shift complete");
      break;

See post #2.

consider

const byte codeOne   = A0;
const byte codeTwo   = A1;
const byte codeThree = A2;
const byte codeFour  = A3;

byte encoder;

void encoderLoop()
{
    byte val;
    val   = digitalRead(codeOne)   << 3;
    val  |= digitalRead(codeTwo)   << 2;
    val  |= digitalRead(codeThree) << 1;
    val  |= digitalRead(codeFour);

    if (5 == val)
        encoder = 0;
    else if (4 == val)
        encoder = 2;
    else if (0 == val)
        encoder = 3;
    else if (1 == val)
        encoder = 4;
    else if (9 == val)
        encoder = 5;
    else if (8 == val)
        encoder = 6;
    else if (0xA == val)
        encoder = 7;
    else if (2 == val)
        encoder = 8;
}


void
loop (void)
{
    encoderLoop();
    Serial.println (encoder);
    delay (1000);
}

void
setup (void)
{
    Serial.begin (9600);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.