problem facing in running the three function in arduino

I am making and Arduino program for a 3 phase inverter. I have designed 3 functions for each individual phase.

In 3 phase, the 2nd phase has to start at 120 degree with respect to first phase and similarly for 3rd phase.

I calculated a delay for it and I got about 6.67 ms delay. After 6.67 ms delay, function 2 should be start while function 1 should continue executing and after 13.33 ms delay function 3 should start while function 1 and function 2 are executing.

Is the above logic possible? If possible, then how?

• That works for 50 Hz. – Andy aka Jan 1 at 13:06
• yes it work for 50hz – ElectronicGuy Jan 1 at 13:07
• You can't start and run 3 functions in parallel that simply have delays. You need a differet approach, and that is a programming question, not an electronics one. Make them state machines to do their job as seprate functions or simply combine them to one. Better yet, simply update the outputs from an array in a timer interrupt. – Justme Jan 1 at 13:08

You don't do it that way.

You have a function to calculate the sine function based on the time.

All you do is add your delay to the time.

• D is the time difference between the phases.
• t is the current time
• Phase 1 is sin(t)
• Phase 2 is sin(t+D)
• Phase 3 is sin(t+2D)
• thanks for responding but i am using this code paste.ofcode.org/38JZ7e5TRbesejzt2LF9s4B – ElectronicGuy Jan 1 at 13:45
• @ElectronicGuy So what is the problem with the code then? It has no 3 functions. – Justme Jan 1 at 14:07
• this code is only for one phase i want to convert this it into three – ElectronicGuy Jan 1 at 14:36
• @ElectronicGuy the pasteofcode code is not suitable for using multiple times. It is written as a single thread. You need to write a new function that can be called in a timed way. – Neil_UK Jan 1 at 14:50

One solution, which more experienced people would hesitate to suggest because it perpetrates the dead-end practice of using delays, but it's close to what you already have, is to modify slightly the single thread you have, to update all three phases.

Use a phase increment of 120+delta (degrees)

DELTA = 10 # degrees
SOME_TIME = 1.67 ms - processing_time # choose to get 50 Hz
phase_index = 0
phase = 0
while forever:
phase_index = modulo(phase_index+1, 3)
phase = modulo(phase + 120 + DELTA, 360)
write sin(phase), phase_index
delay SOME_TIME


There's no need to implement the pseudocode exactly like this. You could simply index through a table of sine values. Or phase and sine(phase) could be rolled into one by implementing a complex vector, and rotating it by multiplication each pass through the loop.

Choose DELTA and SOME_TIME to rotate through all three phases in one period of the underlying waveform. Here, the three phases get written with 30 degrees being advanced, so we've got 12 samples per cycle of each output.

Now without much change, we can have a very much better solution

setup_timer_interrupt(1.67 ms, output_a_sample)

DELTA = 10 # degrees
SOME_TIME = 1.6 ms - processing_time # choose to get 50 Hz
phase_index = 0
phase = 0

function volatile output_a_sample():
phase_index = modulo(phase_index+1, 3)
phase = modulo(phase + 120 + DELTA, 360)
write sin(phase), phase_index


Now you can have a main() function running completely independently of the timer interrupt.