Replicate Length Counter

Hi,
here is my problem i want to replicate the working of length counter
actually i was trying to take the reading from rotary encoder(i think its called that) attached picture.
and i was able to do that as i find a pulse of 12V in that decoder box(which was displaying the calculated length) attached picture and give it to arduino after making it 5V and calculate its 5v blinks as it was in sequence with counter(3 blinks = 1 meter length) it was going fine.
but i find that this rotation goes in backward direction too making the counter go back even in negative too (attached picture).
should i take direct input from 4 wires coming from rotatory wheels (Green, Black , Red , White) and make my own rotation calculation…???

Thanks alot
looking forward to get some help here.

hussain_sheikh: but i find that this rotation goes in backward direction too making the counter go back even in negative too

Because I don't understand the above I cannot comment on the below

should i take direct input from 4 wires coming from rotatory wheels (Green, Black , Red , White) and make my own rotation calculation...???

Robin2: Because I don't understand the above I cannot comment on the below

Above quote means that when wheel moves in a clockwise direction the counter moves ahead like 1,2,3,4,5,6,7..... but as the wheel start moving in anticlockwise direction the counter goes like...3, 2,1, 0, -1, -2, -3. Now in both case my pulse of 5 volts coming making my counter of arduino moves ahead on Serial Monitor. no backward counter.

Plz let me know if you have queries

Thanks

Duplicate thread deleted.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

Well the way most encoders work is by means of a pair of switches. Looking at a table of the states they go through, It would look as follows

00
01
11
10
00
etc…

Going the other direction this sequence is reversed ie
00
10
11
01
00
etc…

So the current state of the switches doesn’t tell us much. We need to compare the current state against the last state.

At any point in time we can represent the state of the two outputs as a binary value. If we combine this with the previous state we get 16 possible values. We can then represent the implications of every one of those states in a string 16 characters long.

So thinking along these lines I’ve come up with this to play with.

#define pinA 2
#define pinB 3

byte lastEncState;
int count=0;

void setup()
{
pinMode(pinA,INPUT_PULLUP);
pinMode(pinB,INPUT_PULLUP);
Serial.begin(115200);
lastEncState=digitalRead(pinA) + (2 * digitalRead(pinB)); 
Serial.println("Ready");
}

void loop()
{
char change[17]={"NBFXFNXBBXNFXFBN"};
//N=no movement, B=backwards, F=forewards, X = error
byte encState=digitalRead(pinA) + (2 * digitalRead(pinB));
byte fullState = (4 * lastEncState) + encState;

byte newCount=count;

switch(change[fullState])
  {case 'F': newCount ++; break;
   case 'B': newCount --; break;
   case 'X':Serial.println(fullState); 
            Serial.println("Error, we've missed a Pulse");
   case 'N': break;//no movement;
  }
  
if(newCount != count)
   Serial.println(newCount);
     
count=newCount;
lastEncState = encState;
}

It seems to work for me but obviously you’d have to make the necessary alterations to see if it does anything for you.

FINAL WARNING. DO NOT CROSS-POST.

hussain_sheikh: Now in both case my pulse of 5 volts coming making my counter of arduino moves ahead on Serial Monitor. no backward counter.

Thank you. That description is much clearer. In that case if you need to be able to count forwards and backwards you will need to detect the pulses directly from the encoder.

...R

AWOL: FINAL WARNING. DO NOT CROSS-POST.

Have you the authority to merge them?

...R

AWOL i have post this question in 2 different topics as i thought that it belongs to both of these topics(it was my mistake i agreed) as my other post is removed so stop sending me scary warnings please.

Thanks

Robin2: Have you the authority to merge them?

I do, but not the will.