2

How to implement three functions, which can execute in the same time (function of input random variables)?

2
  • 3
    You can't. An Arduino only does one thing at a time. The trick is to write your program so it interleaves your functions together.
    – Majenko
    Mar 10, 2017 at 21:10
  • 1
    See my post about How to do multiple things at once
    – Nick Gammon
    Mar 11, 2017 at 6:40

5 Answers 5

4

No single processor can do more than one thing at a time. The best you can hope for is that each function runs for a short time only and gives the processor to the next one, giving the illusion that there is more than one thing happening at once. This is called multi-tasking or multi-threading.

There are some libraries that will do this for the Arduino (search "RTOS Arduino" or "Arduino multitasking"), or you can write each functions using the principles of Finite State Machine programming to achieve the same effect.

5

You can implement three functions but you can't able to execute them same time.

4

How to implement three functions, which can execute in the same time (function of input random variables)?

Micro-controllers such as the AVR used on Arduino boards have only one processing core but there are many hardware modules (ADC, UART, SPI, TWI, Timers, etc). Using these it is possible to achieve several "functions" at the same time, i.e. pure concurrency.

Some examples, while waiting for ...

  1. ADC (analogRead) there are over 100 cycles
  2. SPI (spi.transfer) there are 16 cycles
  3. UART (Serial) is actually already concurrent with buffering and ISR's.
  4. The same goes for I2C (twi).

These cycles can be used to interleave different functions or improve performance (e.g. pre-fetching, pipe-lining).

Another example; using direct port read it is possible to read the state of several pins at the same time.

With simple collaborative multi-tasking it is possible to context switch between several functions (tasks) on delay or yield. This can be done with a scheduler.

2

How to implement three functions, which can execute in the same time (function of input random variables)?

have three executors / arduinos, one for each function.

2

If the result of each function can be expressed as a single byte (i.e. an integer between 0 and 255) then you could:

  • Connect 3 shift registers (e.g. 74595 ic) to 3 pins on your Arduino
  • Assign one pin to each function
  • Use the shiftOut function to output the result of each function one bit at a time on its relevant pin
  • Use a 4th pin connected to all 3 shift registers as a common latch - when this pin is set HIGH the outputs from the 3 shift registers will all change at the same time.
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.