Demonstration code for several things at the same time

Robin2:
I have prepared the attached example sketch in the hope that it will be a
suitable model. I have tested it on an Uno and a Mega. I have called it
"SeveralThingsAtTheSameTime.ino". (It seems to be too long to show the
code directly in this post)
...
It also uses the "state machine" concept to manage the various activities
and enable the different functions to determine what to do.
...
Comments are welcome.

Thank you for this, I will be studying it.

I'm new to Arduino coding and learning what and how can be fit in Arduino
IDE code. My starting approach was to leverage this SimpleTimer code:

http://playground.arduino.cc/Code/SimpleTimer#.UyIPih_f9ic

Here is a code fragment where I do periodic Serial writes. This not
really equivalent to what your code is doing, but this is my starting
point.

...
#include <SimpleTimer.h>
...
SimpleTimer timer;
...
void setup()
{
...
  timer.setInterval((5*1000), timerWrite);
...
}
void loop()
{
...
  timer.run();
...
}
// Periodic write to Serial to test RPi <-> Arduino
void timerWrite()
{
  // Code here that queues up some text to write on Serial
}