Multitasking using Arduino

Hello

I want to build an autonomous solar tracking system and I have already 2 seperate codes , one for operating 2 servomotors, the other for controlling the charging of the battery.

are there any ways to write the 2 codes, so that the arduino will run the code interchangeably: for example the arduino microcontroller run the 1st code for 5 mins and then it run the other code for 20 minutes, and return to run the 1st code and so on.

Thanks, Abdelghani

Abdelghani08:
I want to build an autonomous solar tracking system and I have already 2 seperate codes , one for operating 2 servomotors, the other for controlling the charging of the battery.

are there any ways to write the 2 codes, so that the arduino will run the code interchangeably: for example the arduino microcontroller run the 1st code for 5 mins and then it run the other code for 20 minutes, and return to run the 1st code and so on.

That is as well possible as it would be possible to create a code that runs “several things at the same time”.

Of course, you’d have to avoid two things in your code, which beginners tend to use happily:

  • delay()
  • busy-waiting loops

Instead, your code would have to be driven by time and events.

This sketch will run cycles of 25 minutes, whith 5 minutes calling ‘firstLoop()’ and 20 minutes ‘secondLoop()’:

void firstLoop()
{
}

void secondLoop()
{
}

void setup() 
{
  Serial.begin(9600);
  Serial.println("Good night and good luck!");
}

#define CYCLEDURATION 25*60000L  // 25 minutes in milliseconds
#define FIRSTDURATION 5*60000L   // 5 minutes in milliseconds
unsigned long lastCycleStart;

void loop() 
{
  if (millis()-lastCycleStart>=CYCLEDURATION) lastCycleStart += CYCLEDURATION;
  if (millis()-lastCycleStart<FIRSTDURATION)
    firstLoop(); // loop function for the first 5 minutes of each cycle
  else
    secondLoop(); // loop function for the next 20 minutes of each cycle
}

What do you want: 5 minutes solar tracking with servos, then 20 minutes battery charging, then again 5 minuts solar tracking? That sounds very weird.

thank you Jurs for you help

Actually, I want to make the tracker , update its position in a discrete mode, each 20 minutes for 1 minute or less and in between the arduino will serve as a part of my PWM charge controller.

If you have any Suggestions, you are welcome , again, thank you for your help.

regards, Abdelghani

Abdelghani08: Actually, I want to make the tracker , update its position in a discrete mode, each 20 minutes for 1 minute or less and in between the arduino will serve as a part of my PWM charge controller.

If you have any Suggestions, you are welcome , again, thank you for your help.

Under normal operating conditions you'd create non-blocking 'tasks' which all run within the loop at the same time. Or at least within the same millisecond, or perhaps a few milliseconds. Forbidden in your programming is: - delay() of any kind - busy waiting loops of any kind All functions have to be fully "cooperative" and "non-blocking".

If all the inputs and outputs of the different tasks are independent from other tasks, you possibly might have a simple round-robin-scheduling like possibly that:

void loop() 
{
  taskOne();
  taskTwo();
  taskThreee();
  taskFour();
}

And if it's more complicated and the same inputs have consequences on the outputs of different tasks, or if inputs from several tasks have consequences on the output of one task, then possibly the programming logic might look like:

void loop() 
{
  inputTaskOne();
  inputTaskTwo();
  inputTaskThreee();
  doAllProcessing();
  outputTaskOne();
  outputTaskTwo();
  outputTaskThreee();
}

The more interaction your different tasks need, the more sophisticated programming will be needed in your sketch to create a smooth running "cooperative multitasking".

The demo several things at a time should help.

...R