Optimizing code on arduino due

@PaulS - Thanks a lot for your explanations. I admit I didn't understand everything you wrote, probably because I am quite beginner but I do my best to rewrite the code following your indications.

I will take it again step by step since I discovered some other issues. I will post here when do something.

Thanks a lot again, I really appreciate .