The function I wrote is not the one I talked about in my earlier post. I've since examined your function more closely and your response to my question, then scribbled the demo.
try() is designed to be called once, and will return true or false.
true means it was successful.
false means it took four tries, or took too long.
It will block for five seconds, or longer if the called function takes longer to succeed or fail.
fails is a local non-static variable. It is initialised to zero on every call. On purpose. Every time you try to <whatever> you get five seconds or four tries.
If you started coding recently, you've got quite the sketch propped up there. I encourage you to find better learning sources that can get at some of what frustrates you.
Also, read read read these fora, and I do mean read the code also, closely.
And when it comes to things like doing something four times or for five seconds that you build the tiniest little sketches you can to explore and investigate different ideas about how to do them.
So you don't wonder why simple things aren't working… everything in that function "works". But that does not always add up to doing what you want.
The function I posted is a skeleton, you can decorate it however you want with real stuff. I could see returning zero for success and other values for what the caused the failure.
If you strip down your function to its essence, I believe you will see that it does not do what you want, and would be hard to fix. I did not do that.
I haven't read your entire sketch, but it looks like there are repeated patterns that could be exploited to make the code shorter, easier to understand, fix, modify and enhance.
There are sources for basic program design; as @gcjr says you can speak the language perfectly and it won't help you write a coherent letter to the editor, or whatever.
No good writer is not also widely read. No matter what she's writing.
a7