Recognise encoder movment

i would like to create a device so that anytime the arduino recognises that the encoder has moved it triggers an led on for 5 seconds but i cant use delays.

can anyone suggest how this could be done? i'm quite new to arduinos and code and am struggling a bit.

thankyou

wire the encoder to the hardware interrupt pins. set the interrupts for CHANGE. If you can't use delay() look into "blink without delay" It explains how to use micros() for timing while other processes can execute.

The basic principle of how to do this is every time the encode moves turn on the led and make a not of the current millis time. At some point in your loop() check if current millis time against the time the encoder last moved and turn the led off if it greater than 5000 (5 seconds).

thankyou, that looks good.

i added some code from example i found using a button and millis but my led just stays on when i move my encoder, could you give me an example of some code. i'm really new to this so i'm apologies for all my questions.

really appreciate the help

benjaminwalk:
i added some code from example i found using a button and millis but my led just stays on when i move my encoder, could you give me an example of some code. i'm really new to this so i'm apologies for all my questions.

really appreciate the help

This is un-tested and not elegant but you should be able to bend it to your needs

const int encoder0PinA = 6;             // Rotary Encoder A pin (12)
const int encoder0PinB = 7;             // Rotary Encoder B pin (13)
const int LED = 13;                     // LED

unsigned long lastEncoder = 0;

void setup(){
  pinMode (encoder0PinA,INPUT_PULLUP);                // Enable pullup resistor
  pinMode (encoder0PinB,INPUT_PULLUP);                // Enable pullup resistor
  pinMode (LED,OUTPUT);                               // LED
  readEncoder();  // Prime encoder routine
}

void loop(){
  int turned = readEncoder();                         // Check if encoder moved since last call
  if (turned != 0){                                   // Anything other than 0 means it changed
    digitalWrite(LED,HIGH);                           // Turn LED on
    lastEncoder = millis();                           // Note the time
    
    // React to encoder change
  }
  
  // Do other stuff here

  if (millis() > (lastEncoder + 5000)){
    digitalWrite(LED,LOW);
  }
}

// Polled encoder, returns 1, 0 or -1
int readEncoder(){ 
  static int encoder0PinALast = LOW;
  int eDir = 0;
  int n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)){
    if (digitalRead(encoder0PinB) == LOW){
      eDir = -1;
    } 
    else {
      eDir = 1;
    }
  } 
  encoder0PinALast = n;
  return eDir;
}