Rotory encoder with analog pin

Hello All

You all have been helpful alot with my previous coding problems,
I am not very good at C++, and stuck at another problem. I am sure you guys can help me with this.

I am trying to read rotary encoder with one analog pin.
I know there are another probably easy ways like potentiometer and rotary switch, but based on availability i went this route.


Image: https://ibb.co/rxmwrLZ

The analog pin outputs two different square waves based on direction of turning it.

The code looks for what current position the sensor is at and what is the next position it went to, based on that var speed either goes up or down, and it needs to be between 1 and 4.

For some reason the speed gets stuck at 4 and only goes one way.

Please advise any improvements in code, or any questions you have.

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
const byte ledPin1 = 2;
const byte ledPin2 = 3;
const byte ledPin3 = 4;
const byte ledPin4 = 5;
byte Speed = 1;

const long interval = 50;
unsigned long previousMillis = 0;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

void loop() {
  ReadEncoder();
  SetSpeeds();
  UpdateLeds();
  sensorValue = analogRead(analogInPin);
}

void ReadEncoder() {

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    int NewsensorValue = analogRead(analogInPin);

    if (0 < sensorValue < 200) {
      if (NewsensorValue > 1000) {
        Speed = ++Speed;
      }
      if (1000 > NewsensorValue > 200) {
        Speed  = --Speed ;
      }
    }
    if (200 < sensorValue < 1000) {
      if (NewsensorValue < 200) {
        Speed = ++Speed;
      }
      if (NewsensorValue > 1000) {
        Speed = --Speed;
      }
    }
    else {
      if (200 < NewsensorValue < 1000) {
        Speed = ++Speed;
      }
      if (NewsensorValue < 200) {
        Speed = --Speed;
      }
    }
    sensorValue = NewsensorValue;
  }
}

void SetSpeeds() {

}
void UpdateLeds() {
  if (Speed < 1) {
    Speed = 1;
  }
  if (Speed == 1) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
  if (Speed == 2) {
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
  if (Speed == 3) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin4, LOW);
  }
  if (Speed == 4) {
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }
  if (Speed > 4) {
    Speed = 4;
  }
}
  Speed = ++Speed;

Don't do that.

Speed++; is all it takes.

1000 > NewsensorValue > 200And don't so that either value > 200 and value < 1000

Thanks a lot AWOL, I'll update on it, once I get to the computer.

Does this statement:

  • if (0 < sensorValue < 200)*

do what you think it does?

You might find it easier to read your code if you changed the UpdateLeds() function:

byte ledPins[]        = {2, 3, 4, 5};
int pinState[4];
// m code left out...

void setup()
{
  for (int i = 0; i < ELEMENTS(ledPins); i++) {
    pinMode(ledPins[i], OUTPUT);                     // Set pins to OUTPUT
    pinState[i] = 0;                                 // Set their state to LOW
  }
}

// more code left out...

void UpdateLeds() {
  if (Speed < 1) {
    Speed = 1;
  }
  memset(pinState, LOW, sizeof(pinState));    // Clear all states to 0
  switch (Speed) {
    case 1:
      pinState[0] = HIGH;
      break;
    case 2:
      pinState[1] = HIGH;
      break;
    case 3:
      pinState[2] = HIGH;
      break;
    case 4:
    default:
      pinState[3] = HIGH;
      break;      
  }
  for (int i = 0; i < ELEMENTS(ledPins); i++) {
    digitalWrite(ledPins[i], pinState[i]); 

  }
}

I can't test the code as I don't have what you're using for a sensor.

Thanks a lot econjack. Looks like you have used arrays and state machines, i am always looking to learn more ways to do things. I real situation, i will have atleast 12 leds, and it'll be really helpful then. I will test it out and let you know.