How to read a rotary selector?

Hi, Im doing a project in witch the fan on and off time is controling by the use of a rotary selector.

A the idea is the user configures a fan time, in witch case the new configuration will be displayed on a LCD and show the on and Off time, the problem Im having rigth Know is to read and act accordingly the selector;

Here is the code (Of the selector reading)

void rotarySelectorRead(){
 /*
 This soubrotine will read and debounce the ratary selector and 
 aconvert the current selection to a variable, to handle the 
 ventilation, times on and off.
 
 Variables needed;

int RSpin1 = 31; //the pin 1 of the retary selector 
int RSpin2 = 33; 
int RSpin3 = 35; 
int RSpin4 = 37; 

int RS1 = 0;          //var to hold the status of each pin
int RS2 = 0;
int RS3 = 0; 
int RS4 = 0;

int RSpins  [4] = { RSpin1, RSpin2, RSpin3, RSpin4};
int RSstates[4] = { RS1, RS2, RS3, RS4 };
 */
  

for(int A= 0; A<4  ; A++){
 RSstates[A] = digitalRead(RSpins[A]);
  }
delay(50); 

if(RS1 == HIGH && RS2 == LOW )  fanCase = 1; 
if(RS1 == LOW && RS2 == HIGH )  fanCase = 2; 
if(RS2 == LOW && RS3 == HIGH )  fanCase = 3; 
if(RS1 == LOW && RS4 == HIGH )  fanCase = 4; 

//else fanCase = 1; 

for(int B = 0; B < 4; B++){
 Serial.print(" "); Serial.print(RSstates[B]); 
                      }
 Serial.print("  FanCase  "); Serial.println(fanCase);

//Serial.print("-----------------");
}

I know this is perhaps a wrong point of view and that’s why I came to the professionals.

The code read correctly each pin state, the serial output;
0 0 0 0 FanCase 0
1 0 0 0 FanCase 0
0 1 0 0 FanCase 0
0 0 1 0 FanCase 0
0 0 0 1 FanCase 0

As you can see, the digital read is good, but is not updating the variable.

At first I try;

if(RS1 == HIGH && RS2 == LOW && RS3 == LOW && RS4  == LOW)  fanCase = 1; 
if(RS1 == LOW && RS2 == HIGH && RS3 == LOW && RS4  == LOW)  fanCase = 2; 
if(RS1 == LOW && RS2 == LOW  && RS3 == HIGH && RS4 == LOW)  fanCase = 2; 
if(RS1 == LOW && RS4 == HIGH && RS3 == LOW  && RS4 == HIGH)  fanCase = 4;

Also I try to changue the LOW and HIGh states for the 1 and 0 according to the int variable is in use.
Not luck.

So thats why I choose to compare only two values, but neither works.

In the begining, I was planing to read the pins, and see if there was change and with this variable, uptdate the screen, and also make a second reading, like the debounce function, ideally also I can chek if for any reason there are two pins on 1 like; 1001 this means must be re- read (the rotary selector has only 4 position) and in case of this continues… pre-select fanCase =1.

But, currently this is an area of code, I have no experience.

is there a way to compare the reading array with some pre-defined states? and in case the reading don fit in one case… pre-select other ?

Pseudocode;
If (readingArray[0,0,0,1] == case1[0,0,01]) fanCase = 1

;???
Thanks.
-Alex

if(RS1 == HIGH && RS2 == LOW )  fanCase = 1;

Nothing in your function changes the values of RS1, RS2, etc. after initialization, so they are always 0. Either use the values in the array RSstates[], or directly input the values to RS1, 2, ...

Well well… this is one of thath situation like Sheldon Cooper would say;

“I cant belive I make a bubu and show it to Stephen Hawking”
-FAINTS :o :o :o

In deed was an bad error, I found some similar stuff here; (will post a link after I found it)

And I changue the code like so;

void rotarySelectorRead(){
 /*
 This soubrotine will read and debounce the ratary selector and 
 aconvert the current selection to a variable, to handle the 
 ventilation, times on and off.
 
 Variables needed;

int RSpin1 = 31; //the pin 1 of the rotary selector 
int RSpin2 = 33; 
int RSpin3 = 35; 
int RSpin4 = 37; 

int RS1 = 0;          //var to hold the status of each pin
int RS2 = 0;
int RS3 = 0; 
int RS4 = 0;

byte fanCase = 0;
int rotarySelector = 0;    //to hold the state of switch in a bitstate
int RSpins  [4] = { RSpin1, RSpin2, RSpin3, RSpin4};
int RSstates[4] = { RS1, RS2, RS3, RS4 };
 */
  
 

for(int A= 0; A<4  ; A++){
  int Read = digitalRead(RSpins[A]); 
  bitWrite ( rotarySelector, A, Read ); 
  }
delay(100); 



switch (rotarySelector)
   {
      // test for some combinations of switches we care about
     case  B0001: fanCase = 1; Serial.print(fanCase);  Serial.println("Rotary knob on 1"); break;
     case  B0010: fanCase = 2; Serial.print(fanCase);  Serial.println("Rotary knob on 2"); break;
     case  B0100: fanCase = 3; Serial.print(fanCase);  Serial.println("Rotary knob on 3"); break;      
     case  B1000: fanCase = 4; Serial.print(fanCase);  Serial.println("Rotary knob on 4"); break;      
   } 
   
   delay(100); 

//Serial.print(" ");
Serial.println( rotarySelector , BIN  ); 

}

It a nice trick to read the status of the pin and convert in to a binary number, then you can make a
switch case with the number itself…

Well…
Here are some Outputs;

1Rotary knob on 1
2Rotary knob on 2
3Rotary knob on 3
4Rotary knob on 4

Its seem like Know is reading the state of the pin and changing the variable according to it.

Thanks !
-Alex.

PS.- Why the serial print at the end; “Serial.println( rotarySelector , BIN );”
dont print the 0000 number? instead only 1, 10, 100, 1000?
or why not 1000, 0100, 0010, 0001??