In every for loop inside the loop function you access pin+1, you cannot do this because at the top of the loop you will try to access an item outside your array.
I found this a cool challenge so I fixed it for you, and removed most of your for loops at the same time