Turn on a led everytime a rotary encoder rotates

I want to light up a led everytime a rotary encoder turns left or right.

If the encoder turns left “leftLed” goes high.
If the encoder turns right “rightLed” goes high.
And if you press the rotary encoders built in button “midLed” goes high.

The problem is that the leds for left and right only blink very faintly and very short because the out is not on HIGH long enough.
I cant add a delay to the part of the code because then the rotary encoder would ignore some rotations.

Can anybody help me out here?

 const int leftLed = 10;
const int rightLed = 9;
const int midLed = 8;
const int pinA = 5;
const int pinB = 4;
const int buttonPin = 6;

int positionCounter = 0; 
int pinALast;  
int aValue;
int buttonState;


 void setup() { 
  
  pinMode (pinA, INPUT);
  pinMode (pinB, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  
  pinMode (leftLed, OUTPUT);
  pinMode (rightLed, OUTPUT);
  pinMode(midLed, OUTPUT);
  
  pinALast = digitalRead(pinA);
      
  Serial.begin (9600);
   
} 

void loop() { 

  buttonState = digitalRead(buttonPin);
  aValue = digitalRead(pinA);

  if (buttonState == LOW){
     digitalWrite(midLed, HIGH);
     positionCounter = 0;
     Serial.println("");
     delay(500);
     digitalWrite(midLed, LOW);
        
  } else 
    
    if (aValue != pinALast){
    
    if (digitalRead(pinB) != aValue) { 
      digitalWrite(rightLed, HIGH);
       positionCounter ++;
       digitalWrite(rightLed, LOW);

    } else {
      digitalWrite(leftLed, HIGH);
       positionCounter--;
      digitalWrite(leftLed, LOW);
    }

    Serial.print("(");
    Serial.print(positionCounter);
    Serial.print(")");
    
    }
    
  pinALast = aValue;

R.ino (1.14 KB)

Save the value from millis() when you turn the LED on. Then each time through loop check the millis() value. When the required interval has passed turn off the LED.

Might want to use ports as well instead of digital write to turn on the leds, just to make sur the ISR will be quick and not miss ticks - depending what's turning the encoder (manual or fast motor shaft?)

Might want to use ports as well instead of digital write to turn on the leds

Hi,
this would be interesting for me, too.
is there somewhere a tutorial on this?

AdmiralCrunch: Hi, this would be interesting for me, too. is there somewhere a tutorial on this?

check out Port Registers article