For loop into a function

I put the whole code in #13 correctly this time :), Everything worked perfectly for button 1. so i think the above should be correct. After i started with buttonstate 2 it went wrong