Hi, folks, First of all I'm sorry if its a stupid question but I have to ask after trying to find an aswer on internet....
Is there a way for call a function and keep code running whether the called function is finished or not?
example:
void loop()
{
my_2_seconds_function();
Serial.print("another text");
}
void my_2_seconds_function()
{
Serial.println("hi");
delay(1000);
Serial.println("how are you");
delay(1000);
}
Yes there is, it's called multi-tasking or multi-threading, however it requires significantly more programming skills to do it reliably, i.e. without the various tasks conficting or deadlocking. The shortage of SRAM in an Arduino also makes it harder, since a typical implementation would have a separate stack for each worker task.
Another possibility is to implement each task as a state machine, and switch between them round-robin style. That way each task requires a minimum of memory. It's a complex subject to tackle in a forum... however I'll try a quick summary.
A state machine is where you break a function into steps, give each step a number, and have a single static or global number variable record the number of the next step to be performed when the function is re-entered. The function would relinquish control whenever it completes a step. All significant waits would be implemented by relinquishing control and having the next step check for completion. In an ideal case the function has no variables, the "memory" is entirely encapsulated in the state variable. The function would be implemented in C/C++ as a switch statement.
A state machine is closely related to something called a Turing Machine. It has been mathematically proven that every algorithm can be implemented on a memoryless state machine... but that doesn't mean you'd always want to! 
If you do this then you will need an "executive" that calls each of the state machine functions in a cycle. The obvious way is to have the "Loop" function call the next SM() in a list.
cabecinhas:
Is there a way for call a function and keep code running whether the called function is finished or not?
Nope. An AVR has one CPU with one core - in otherwords it can only do one thing at a time. A desktop with a dual core processor can do two things at once as it has two CPUs.
Wait I hear you cry, I've got 10 programs open on my desktop right now, but only two processors, so how can it be doing 10 things at once?
Well, its not, its only doing two things, but... Windows/Linux/etc. are called Real Time Operating Systems. They work by having a so called 'Task Scheduler' which splits the time each function gets on the CPU based on priority.
There are several RTOS for an Arduino, each designed in a different way, and who knows which is best.
However one thing you will find is that ALL of these task schedulers have to wait for any function to return. Which gets me by to my original statement of 'Nope'.
The simple funtion written can be re-done using blink-without-delay tho.
Print Hi, check the time you printed it (millis), keep checking how much time has gone by, after a second print the next line.
Do other stuff while waiting for time to pass.
So something like this:
void loop(){
currentMillis = millis();
elapsedMillis = currentMillis - previousMillis; // all time variables are unsigned long
if (elapsedMillis >=oneSecond){
previousMillis = previousMillis + oneSecond;
messageNo = messageNo +1;
if (messageNo == messageCount){messageNo = 0;}
switch (messageNo){
case 0:
Serial.println("message 0");
break;
case 1:
Serial.println("message 1");
break;
case 2:
Serial.println("message 2");
break;
} // end switch
} // end time check
// do other stuff while waiting
} // end loop
A multi-core CPU can make preemptive multitasking run smoother, but it isn't a requirement, and I wouldn't really recommend preemptive tasking on an small AVR anyway, as you would then definitely need a separate stack for each task. You also don't need an RTOS to do multitasking. An RTOS guarantees (or tries to) a certain maximum latency between an event and an action. That isn't necessary for lightweight threads, which are an everyday thing in small embedded projects. And just FYI: Windows and Linux (most versions) are multi-tasking, but they are not RTOSes.
IMHO, the state machine approach is the most practical on the Arduino, given its limitations (which are a lot of fun to work within: I'm reliving my youth!
).