absolute encoder BCD input

so i have this

this is a mega board
i want to be able to add up the binary position and store it in a variable to do things at certain counts
does the while add in the value when true and if not true then does not add in the value?
so far i have this but it just counts 100?
i can image how short a pro could code this but i like the long hand for ease of learning and viewing it.

                         //  encoder wires 
int red    = 31;         
int black  = 33;
int brown  = 35;
int orange = 37;
int yellow = 39;
int green  = 41;
int blue   = 43;
int purple = 45;
int gray   = 47;
int white  = 49;

volatile int degree;

void setup() {

  pinMode(red,    INPUT);
  pinMode(black,  INPUT);
  pinMode(brown,  INPUT);
  pinMode(orange, INPUT);
  pinMode(yellow, INPUT);
  pinMode(green,  INPUT);
  pinMode(blue,   INPUT);
  pinMode(purple, INPUT);
  pinMode(gray,   INPUT);
  pinMode(white,  INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  while  (digitalRead(red) == HIGH) {
    degree += 1;
  }

  while (digitalRead(black) == HIGH) {
    degree += 2;
  }

  while (digitalRead(brown) == HIGH) {
    degree += 4;
  }

  while (digitalRead(orange) == HIGH) {
    degree += 8;
  }

  while (digitalRead(yellow) == HIGH) {
    degree += 10;
  }

  while (digitalRead(green) == HIGH) {
    degree += 20;
  }

  while (digitalRead(blue) == HIGH) {
    degree += 40;
  }

  while (digitalRead(purple) == HIGH) {
    degree += 80;
  }

  while (digitalRead(gray) == HIGH) {
    degree += 100;
  }

  while (digitalRead(white) == HIGH) {
    degree += 200;
  }
  if (degree = 100) digitalWrite(LED_BUILTIN, HIGH);
  delay (100);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println (degree);

}

hmmmm, using the analog multi meter on the encoder i should be able to see a change between the ground and any of the output wires. i try them all at any position and get nothing. might be dead encoder?

turbothis:
hmmmm, using the analog multi meter on the encoder i should be able to see a change between the ground and any of the output wires. i try them all at any position and get nothing. might be dead encoder?

You have open collector outputs. Try the measurement after connecting a 4.7K pullup resistor from the encoder output to +5.

ok ya that seems to work!
so now how do i use the encoder?
i need a 4.7k on each output to 5V?

EDIT

on second check i get 5V just off the end of the resistor not connected to any encoder output

turbothis:
i need a 4.7k on each output to 5V?

No, you can use the Arduino's internal pullups with INPUT_PULLUP in pinMode().

so then the encoder would be LOW active instead of HIGH?

so then the encoder would be LOW active instead of HIGH?

Yes.

I don't understand your entire approach with the while() loops.

I think that the way to read this encoder is to read all the outputs in a for() loop and determine the current position.

well i am pretty terrible at this so that might clear this up
i thought the while would be pretty straight forward as WHILE the condition is true then add in the amount to the total. and WHILE the condition is false then do not add in the amount

turbothis:
i thought the while would be pretty straight forward as WHILE the condition is true then add in the amount to the total. and WHILE the condition is false then do not add in the amount

That's exactly what will happen. Imagine the encoder is at position five ( binary 0101 ). WHILE bit zero is true the code will continually add a '1' to degree. It'll never get to add the four, or even check bit position 2.

i was afraid of that
is there an example some where on how to continuously add up things? or monitor the state
i swear every time i want to do something the only examples of i find are of LED codes wishing you happy birthday or flashing the star spangle banner in green blue and red. lol

turbothis:
is there an example some where on how to continuously add up things?

First, before checking the encoder bits set your accumulator to zero - to wipe the previous reading. Even better would be to do this in a separate 'scratchpad' variable then transfer the scratchpad to the working variable once all the relevant bits have been collected. Most important, change all the while()s to if()s.

Try this untested code.
It uses INPUT_PULLUP so active is LOW. The delay() when degrees == 100 will make the reading less responsive. How fast is the encoder turning?

int red    = 31;
int black  = 33;
int brown  = 35;
int orange = 37;
int yellow = 39;
int green  = 41;
int blue   = 43;
int purple = 45;
int gray   = 47;
int white  = 49;

int degrees = 0;
int previousDegrees = 0;

void setup() {
  pinMode(red,    INPUT_PULLUP);
  pinMode(black,  INPUT_PULLUP);
  pinMode(brown,  INPUT_PULLUP);
  pinMode(orange, INPUT_PULLUP);
  pinMode(yellow, INPUT_PULLUP);
  pinMode(green,  INPUT_PULLUP);
  pinMode(blue,   INPUT_PULLUP);
  pinMode(purple, INPUT_PULLUP);
  pinMode(gray,   INPUT_PULLUP);
  pinMode(white,  INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  degrees = readPosition();
  if (degrees != previousDegrees)
  {
    previousDegrees = degrees;
    //do something with the value
    Serial.println(degrees);
    if (degrees == 100)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay (100);
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

int readPosition()
{
  int position = 0;
  //!digitalRead() invert for INPUT_PULLUP 
  position += !digitalRead(red);
  position += !digitalRead(black) * 2;
  position += !digitalRead(brown) * 4;
  position += !digitalRead(orange) * 8;
  position += !digitalRead(yellow) * 10;
  position += !digitalRead(green) * 20;
  position += !digitalRead(blue) * 40;
  position += !digitalRead(purple) * 80;
  position += !digitalRead(gray) * 100;
  position += !digitalRead(white) * 200;
  return position;
}

i put in the delay and led function to see something happen on the board
i run cattledog's code and still get nothing on the serial monitor or blinking LED on the board :frowning:
i think i bought a dead used encoder
sucks they are 300$ new. lol
might get another used one......

Have you applied the test suggested in reply #2 to all of the encoder outputs? If so, what was the result?

well with the resistor in the 5V, i get 5V on the other end.
tried a couple outputs and all are 5V
5V everywhere!

Your code shows wire colours that generally match the datasheet but you have red and black (power and ground) as inputs but no pink (2^0100) and light-blue (2^1100).

Without those two inputs you only have two BCD encodings and will have a count range from 0 to 99.

Is your wiring correct? If so, why are you confusing the matter with wrong color names?

Referring to R1 in this drawing:

Turn off INPUT_PULLUP. Substitute the encoder output for S2 so you have encoder output x to Arduino input *and *R1. Also, power the encoder separately, don't use UNO (assumed) power for that and don't use a supply greater than +5V. Connect the encoder ground to the Arduino ground. The same way the meter has to be connected to Arduino ground to take voltage readings, the encoder has to be connected to Arduino ground so they have a *common *reference.

Put your meter on the encoder/Arduino/R1 junction and test the voltage while turning the encoder slowly. If the voltage fluctuates work up a basic sketch to read the input and display it on the built-in LED or do a serial print of its status.

Your code shows wire colours that generally match the datasheet but you have red and black (power and ground) as inputs but no pink (2^0100) and light-blue (2^1100).

Without those two inputs you only have two BCD encodings and will have a count range from 0 to 99.

Is your wiring correct? If so, why are you confusing the matter with wrong color names?

@turbothis show an image of an E6F-AB3C-C

The posted spec sheet for that encoder shows the colors and output values as the OP's code. For that model, the pink and light blue are ground and power.

cattledog:
@turbothis show an image of an E6F-AB3C-C

The posted spec sheet for that encoder shows the colors and output values as the OP's code. For that model, the pink and light blue are ground and power.

Interesting.

Page 4 of 8 of this datasheet:

https://www.mouser.com/datasheet/2/307/e6f-a_ds_csm498-1189540.pdf

shows the following for the pre-wired model:

but the 'C' variant just shows connector pins and no wire colors:

Are the wire colours really different between the two? That seems really messed up...

man, i cant get any sign of life out of this thing! :frowning: