A problem with measuring time in a for loop
Old time and new time are wrong way round on a part where the LED'd are switched off

else if(oldTime-newTime &gt;= dt){
          oldTime=newTime;
          digitalWrite(led1,LOW);
         }



When simple processors reach the max or min value and continue counting, they overflow and start counting from far end: So 5-8=maxint(largest number)-3
Result is that the difference is a massive number greater then 2000 except every few weeks when millis() overflows and become larger then Unsigned large int can store and starts counting from zero. (many codes fail working on that occasion, your would only works then ;-)
Correct version:

else if(newTime-oldTime &gt;= dt){
          oldTime=newTime;
          digitalWrite(led1,LOW);
         }


Although I have noticed that testrange (both) only returns its value onto true on limited circumstance if input from encoder increased considerably very quickly to catch-up with growing inputs, so the next values will never pass condition to switch them on

if(encoder>=array1[i] &amp;&amp; encoder&lt;=(array1[i]+tolerance) &amp;&amp; testRange)

will never return true because testRange stays false. Add

testRange=true;
testRange2=true; 

at the end of the loop otherwise it does return into true for next loop()iteration. I guess question is if you want other functions to run while LEDs are on or not.

It is a bad habit to make unnecessary variables like i,j,k,l global as they waste memory even when not needed you can define them within structure itself Eg: for(int i=1;... this way as soon as for, while etc loop (brackets close)finishes you get your memory back and you can use variable again