Reading multiple inputs and a switch


#1

Hi guys

I’m trying to do a switch(software) with the readings of multiples inputs, so that depending on the digital reading of this inputs a specific case of the switch must execute.
when trying to compile it, error appears:
intentomanu.ino:100:19: switch quantity not an integer

Could someone lead me into the right way to do it?

int E3 = D7;
int E2 = D5;
int E1 = D4;
int E0 = D3;

int Inputs[4] = {D7,D5,D4,D3};

switch (Inputs)
    {
        case 1: //d7=0, d5=0, d4=0, d3=1
        message();
        break;
        
        case 2: //d=0, d5=0, d4=1, d3=0
        message2();
        break;
//and so on
``

#2

The switch statement needs the (integer) variable in brackets, to be matched to the value in each case statement. In this code snippet the value (Input) is an array and therefore will not match to 1: or 2:

So one way of doing this could be: (NB - not tested on a device)

enum inputs_enum {E0, E1, E2, E3, numOfInputs}; // create a list of aliases for 0, 1, 2, 3 & 4
int Inputs[numOfInputs] = {D3,D4,D5,D7}; // pins corresponding to inputs

void message(int messageNum)
{
  switch(messageNum) 
  {
    case E0: // matches to 0 from its position in the enum above
      Serial.println("E0 is High");
      break;
    case E1: // matches to 1 from its position in the enum above
      Serial.println("E1 is High");
      break;
    //etc
  }
}

void setup() {
    Serial.begin();

    // get values
    for (int n = 0; n < numOfInputs; n++) 
    {
      if (digitalRead(Inputs[n]) > 0) // if the input is detected
      {
        message(n); // send message for that input 
      }
    }
}

void loop() {
    Particle.process();
}

#3

Thank you, I did understand the part of matching the switch statement to the value, notice my error. But, with the inputs E3,E2,E1,E0 ; I want it to be as a binary number from 0(0000) to 15(1111), depending on the digitalRead of the inputs it will be the switch case
for example:
If E3=0, E2=0, E1=1, E0=1 it will be case 3 of the switch.

I hope I make clearer my question.


#4

You can use

int inputPinsValue = (digitalRead(E3) << 3) + (digitalRead(E2) << 2) + (digitalRead( E1) << 1) + digitalRead(E0);

Then switch (inputPinsValue)
{
  case 0:
    break;
  case 1: // E0 = 1
    break; 
    //...
 case 15: // all inputs are 1
   break;

}

#7

You cannot have cases like this (x, y, z). A construct like this will evaluate to the last item in the list hence you got the duplicate case of 0 (as of 0,1,0 -> 0 vs. 0,0,0 -> 0).
You can write your binary values 0b0010 tho`.

In your previous attempt you need to (re)read your pins over and over again in loop() not only once during initialisation (which I’d not do at all but init to -1 to indicate a disallowed value).

BTW, when using buttons or switches you need to add pull-resisotrs (internal or external) to ensure a discrete level while the switch is not closed.


#8

Thank you!!
Made a couple of changes! with your suggestion and it worked!


#9

Thank you ScruffR.
Re tried the suggestion given by shanevanj and saw my mistake in the coding.