Can one do multitasking in programming?

Say you ask the Arduino to do multiple things at once? I am imagining the way code is carried out is line by line. Is there a way around this linear way so I can ask more than one sections of code to be carried out at the same time? Or do i just use different microcontrollers for each task...

Is there a way around this linear way so I can ask more than one sections of code to be carried out at the same time?

No. However, the time it takes to execute a line varies, from 62.5 nanoseconds to days, depending on what is on the line. i++; is fast. delay(108000000UL) is not.

You can structure your code so that there is nothing blocking (like delay() or parseInt()) and make it appear as though the Arduino is doing several things "at once".

Is all programming like this btw?

Is all programming like this btw?

On a device with no operating system and one processor, yes.

In a simplified way, I would guess that the OS would tell which chunks of code to execute and the multiple processors do the concurrent executing commanded by the OS?

In a simplified way, I would guess that the OS would tell which chunks of code to execute and the multiple processors do the concurrent executing commanded by the OS?

If the application is written in such a way as to take advantage of multiprocessors, yes. A word-processing application, for instance, that accepts user input and displays it, while also doing spell checking and grammar checking, needs to be written to support threads. Then, the OS can (but does not have to) assign different threads to different processors.

Pluto1: Say you ask the Arduino to do multiple things at once? I am imagining the way code is carried out is line by line. Is there a way around this linear way so I can ask more than one sections of code to be carried out at the same time? Or do i just use different microcontrollers for each task...

One Arduino board @16MHz can execute 16 million machine instructions per second.

It's your decision and program design what you are doing in one second.

For example you could do "delay(1000)" and block the program execution with doing 16 million do-nothing-instructions.

Or you could do a million useful things in the same time.

Your decision. Your programming logic.

Normally and when strictly avoiding "delay()" and "busy waiting" in your sketch, a single Arduino board can handle dozens of tasks at the same time with a "cooperative multitasking" type of program.

Have a look at several things at the same time

...R

Pluto1: Say you ask the Arduino to do multiple things at once? I am imagining the way code is carried out is line by line. Is there a way around this linear way so I can ask more than one sections of code to be carried out at the same time? Or do i just use different microcontrollers for each task...

if you are new to coding and most examples use the "for" command it does look like the arduino can only do one task at a time. In real life coding you don't stop to repeat a action 200 times especially with a delay built in.

Beginning code examples are kept simple and use "for" and "delay" a lot as you get use to programming you will probably never use "delay" unless you are troubleshooting. The for command you will use just not to make a simple code repeat so many times. Its used more to set something up or do something at full speed.

Technically, the arduino is usually doing more than one thing at a time, because you can think of the peripherals as being special-purpose processing units. So at one time, the chip can be sending a serial character, receiving a serial character, generating waveforms on all 6 PWM outputs, and other stuff, all at the same time that some statement in your sketch is executing. But those usually don't count (till you find a processor that doesn't have those peripherals!)

Pluto1: Say you ask the Arduino to do multiple things at once? I am imagining the way code is carried out is line by line. Is there a way around this linear way so I can ask more than one sections of code to be carried out at the same time? Or do i just use different microcontrollers for each task...

You can have code that gets something started like turn a led ON, do a bunch of other things and then turn the led OFF. In that case, the blink happens while the bunch of other things do too.

While my code watches Serial to read and process characters there is time between those to check and do other things even at 115200 baud.