Else or not else

(NB. in the first version I used dome "for" that where not needed for this issue so I delete that part of code that would just confuse.)
Hi, I am programming an arduino to make it communicate with a Unity game, but that is not the point, the problem is on Arduino:

  • I am using a "rotary encoder" to make detect when it is rotating and in which direction it is going, I connected the A and B of the encoder on two different digital pins of Arduino and the C has been connected to the GND pin.

this is the code:

int reading = 0;
int degree = 0;
int delayy = 20;

// Pin definitions
const int pinA = 6;
const int pinB = 7;

// Storing the readings

boolean A;
boolean B;

void setup() {
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
Serial.begin(9600);
}

void loop()
{
A = digitalRead(pinA);
B = digitalRead(pinB);
currentTime = millis();

if((A == 1) && (B == 0))
{
Serial.print(4);
Serial.flush();
delay(delayy);
}

else
{
if((A == 0) && (B == 1))
{
Serial.print(5);
}
else
{
Serial.print(6);
Serial.flush();
delay(delayy);
}
}
}

when the encoder goes in one direction it gives me A == 1 and B == 0 and viceversa when on the other direction. before I added the final else with output of "6" I got the following problem: turning in one direction I receive an output like " 444444444444444444555" or "55555555555555555444" I can even understand why this happen, it can be an hardware issue from the moment at the end of the movement the encoder will change his state. the strange fact is that when i use the "else" to indicate the output in case I get no input this problem doesn't occur.. I get "66666666466666656666666". It seems like Arduino need an else to control the number of information about the input. Have I been clear? I have other problems on the Unity side now, but I was curious about this issue with arduino, that I have solved for some reason with the else. Thank you!

This is your loop() function formatted more sensibly by using Auto Format in the IDE

void loop()
{
  A = digitalRead(pinA);
  B = digitalRead(pinB);
  currentTime = millis();
  if ((A == 1) && (B == 0))
  {
    for (i = 0; i < 100; i++)
    {
      Serial.print(4);
    }
    Serial.flush();
    delay(delayy);
  }
  else
  {
    if ((A == 0) && (B == 1))
    {
      for (i = 0; i < 100; i++)
      {
        Serial.print(5);
      }
    }
    else
    {
      Serial.print(6);
      Serial.flush();
      delay(delayy);
    }
  }
}

You can see more clearly from the indentation which if the final else clause is attached to, ie A == 0 and B == 1. Is that where it is meant to be ?

Incidentally, why are you printing the number 100 times ? Isn’t just once enough ?

That's not the usual way to test an encoder for direction: a "snapshot" of the A and B states can take on any combination regardless of direction. In either of the 2 figures below you can, for example, see cases of A=1 and B=0, for either direction.

Source

The normal way is to look for a change in A, ie compare newA with previousA, and iff A has changed:

  • if B is the same as newA that's one direction, or
  • if B is different from newA, that's the other direction.

Thank you for the help, I still had some doubts and every time I was writing them here, i found the answer by myself while I was checking that everything was written clear, I really tried before to understand properly the working but till now I only found that method to work.
I show here the code that is working for me, I think the only not intuitive point that I want to highlight is that the delay in the two “if” must be “1” to work.

/*
* put a deley beteween the imput to avoid the repetition of the wrong number btween the direction detections
*/

int delayy = 1;

const int pinA = 6;
const int pinB = 7;

boolean A;
boolean B;
boolean PreA;
unsigned long lastTime;

void setup() 
{
 pinMode(pinA, INPUT_PULLUP);
 pinMode(pinB, INPUT_PULLUP);
 Serial.begin(9600);
} 

void loop()
{
 A = digitalRead(pinA);
 B = digitalRead(pinB);

 if(A != PreA)
 {
   /* this part can be useful to understand what is going on in arduino
   Serial.print("PreA");
   Serial.print(PreA);
   Serial.print("\n");
   Serial.print("A");
   Serial.print(A);
   Serial.print("\n");
   Serial.print("B");
   Serial.print(B);
   Serial.print("\n");
   Serial.print("\n");
   */

   if((A == B) && lastTime < millis()+delayy)
   {
     Serial.print(4);
     //Serial.print("\n");
     Serial.flush();
     delay(delayy);
     lastTime = millis();
   }
 
   if((A != B) && lastTime < millis()+delayy)
   {
     Serial.print(5);
     //Serial.print("\n");
     Serial.flush();
     delay(delayy); 
     lastTime = millis();
    } 

 PreA = A;
 
}
}

I check if I can give points or something like that to the people that helped, thank you again!