# Need some help understanding the basics of multitasking for a microcontroller

Hello!
I'd like to make a quadrocopter. I know you hear this phrase just too often and it probably isn't interesting for you, but I need to understand a few things before actually starting the project.
So, the basic components:
-4 motors, 4 esc(controllers, accept pwm as input)
-gyro/accelerometer
-Rx module, for up to 8 pwm inputs via a RC module.

The pwm used here is more specific, 50Hz rate with duty cycle of 1 to 2ms.
This scheme shows it all(servos and esc share the same pwm type, 1ms being 0% throttle, 2ms being 100% throttle):

There will be 4 basic functions:

1. Get the raw acc/gyro data every few ms and recompute the position.
2. Get the input from Rx, figure out what the user wants, etc. lift-off, lock position, go up/down...
3. Send pwm signals to the 4 esc-s, thus controlling the motors.
4. Get everything to work together, take care of stabilizing the system using the data from 1. via 3., apply wanted changes from 2. via 3.

The problem is, I dont know how to sync all this. You see, I must send every 20ms a signal(3). Meanwhile, I must compute the position including complex math functions(1), and read pwm too(2).
I am afraid that my time isn't enough, plus I don't know to switch between these actions.

I'd appreciate if you throw some light on those matters.

The pwm used here is more specific, 50Hz rate with duty cycle of 1 to 2ms

More commonly known as PPM (Pulse-Position Modulation)
Check the Servo library.

You are totally right, but I cant figure this out, if I use servo.writeMicroseconds(1500), does this repeats the signal regularly or just once? Because if the ESC loses signal, it turns off as a safety measure(not like the servo). I guess I need a loop to send the line every 20ms?

if I use servo.writeMicroseconds(1500), does this repeats the signal regularly or just once? B

It repeats until you write a new value.

I'm glad it is made so easy. And it doesn't take the valuable time in between?
For example, time chart:
free time - set pin high at 0ms - free time - set pin low after 1.5ms - free time for 18ms - set pin high and so on....
Where I can do other things in the free time? (Like reading inputs?)

The handling of the servo pulse timing is taken care of in interrupt context.

One more question: As I mentioned, I have 3 sensors with 9 readings, 6 of which will be vector computed and 3 of them used to add linear values every few milliseconds. Will it work with all those interruptions and input readings? Or maybe it will be better to get a second arduino, maybe one of the small ones, so it can compute everything without problems?
I have yet no idea about code structuring for a micro controller for real, so I am a little confused...