A function-definition is not allowed here before '{' token

My bad. This is the code:

Please modify your post to use the code tags.

Use auto format on the code Ctrl +T or find it under tools in your sketch. See if you can see the brackets issue.