Digital input missing values from the encoder

Hello, I am working on a rotary encoder connected with Arduino MEGA 2560. I am taking digital inputs from the two channels of the encoder, which are randomly connected to the pin 5 and pin 9 of Arduino respectively. the channel A which is connected to the pin 5 seems to go to zero all of a sudden even when i am high state for that channel and doesn't even respond when I move my encoder disk.

Is there anything else I need to incorporate while I am reading multiple digital inputs to read the value simultaneously?

How about posting your code here ? How are the encoders wired to the Arduino ?

For all we know you have delay()s in the code that might prevent the inputs being read when required and/or the inputs are floating at an unknown voltage hence in an unknown state.

I used the following code:

int n=2;
int m=3;
int righti=LOW;
int lefti=LOW;
int left=LOW;
int right=LOW;
float counter=0;
void setup()
{
pinMode(n,INPUT);
pinMode(m,INPUT);

  • Serial.begin(250000);*

}
void loop()
{

  • while (millis()<2000)*

  • {*

  • lefti=digitalRead(n);*

  • righti=digitalRead(m);*

  • }*

  • left=digitalRead(n);*

  • right=digitalRead(m);*

  • if ((left == HIGH && right==HIGH ) && ( lefti==LOW))*

  • {*
    _ counter=counter+2.8125*2;_

  • righti=right;*

  • lefti=left;*

  • }*

  • if ((left == HIGH && right==LOW ) && ( righti==HIGH))*

  • {*
    _ counter=counter+2.8125*2;_

  • righti=right;*

  • lefti=left;*

  • }*

  • if ((left == LOW && right==LOW ) && ( lefti==HIGH))*

  • {*
    _ counter=counter+2.8125*2;_

  • righti=right;*

  • lefti=left;*

  • }*

  • if (( left == LOW && right == HIGH ) && ( righti == LOW ))*

  • {*
    _ counter=counter+2.8125*2;_

  • righti=right;*

  • lefti=left;*

  • }*

  • if ( counter >= 360)*

  • {*

  • counter =0 ;*

  • }*

  • Serial.print(left);*

  • Serial.print(’ ');*

  • Serial.print(right);*

  • Serial.print(’ ');*

Serial.print(counter);
Serial.print(’ ');

}

  while (millis() < 2000)
  {
    lefti = digitalRead(n);
    righti = digitalRead(m);
  }[code]
This will read the 2 inputs for about the first 2 seconds that the program runs.  Is that what you meant to do ?

The proper tags for including your code are [code]... [/code], not [i]... [/i].

In addition to improperly posting the code, and forgetting to tell us which encoder you have and how you wired it, you forgot to add comments to the code to document your unusual approach.

Why would anyone want to debug that mess for you?

There are plenty of much simpler and clearer, working examples posted on how to wire and read encoders. Try one or more of them.

if  ((left == LOW && right==LOW ) && ( lefti==HIGH))
  {
    counter=counter+2.8125*2;
    righti=right;
    lefti=left;
  }
  if (( left == LOW && right == HIGH ) && ( righti == LOW ))
  {
    counter=counter+2.8125*2;

So, one encounter tick should be counted as 5.625. What universe does THAT make sense in?

You are counting ticks, NOT the distance that something might have moved when the encoder moved enough to generate another pulse.