Question about RTOS Program Structure


I’m struggling a bit to understand where do I have to write each function and split the code in threads

That is the key point here. Basically you have to split the code into small chunks, which do something relevant and which wait until they are asked to do something - in NilRtos they may wait on a semaphore (signaled from other task) or based on a timing. Mind the 2k ram is not much, so 5-8 smaller tasks would be a maximum with 328p.
Before you start to work with ie. Nilrtos I would recommend you to toy with all the examples - they cover almost everything you can do with it…