Hi,
yeah, Length-2 won't cut it.
if you need a counter that goes 0, 1, 2, ... N-1, 0, 1, 2 etc simply use:
counter=(counter+1)%N;
it clearly increments/wraps and stays in the interval [0, N-1].
If you would need a counter that goes 1, 2, ... N, 1, 2 (I don't expect you would need this) then use:
counter=(counter%N)+1;
which also increments/wraps and stays in the interval [1, N].
BTW: using a list of images with duplicates makes debugging somewhat more difficult; you should use a simple list of images that you can recognize right away.
Why don't you have a look at something that already works?
Carousel · Bootstrap[
^] is an example, and you can see all of its code using "View Source" or whatever the command is called on your browser.
:)