Independent functions without using RTOS

I want to make two functions that are independent of each other and will run at the same time. for example: in func1() : i am blinking an led with a delay of 1msec and in func 2(): i am asking user to enter some value on serial port and waiting for that value

but while waiting for that value i want my led should blink regularly with 1 msec of delay.

Can any one suggest me how to implement this. I dnt want to use RTOS.


The Arduino can do one thing at a time, with or without an RTOS.

The only way to blink LEDs at different rates is to NOT use delay(). Look at the blink without delay example.

Blocking waiting for serial input is not a good idea. Not using the data until it is complete is.

Can any one suggest me how to implement this.

You can implement a rtos with a switch/case statement.

I use an interrupt to do so multitasking in my current design. If you have a task that needs to run in a certain interval, you can utilize a timer.
As long as your timer task is shorter than the timer timeout; or alternatively you you break the task up into short parts that run over multiple iterations.

The benefit of this design is you aren’t wasting processing time checking if the task actually needs to run, however it is harder to manage and you need to beware of the consequences of using shared memory.

A 1msec delay is not going to be visible as a blink. If you're really trying to fade the LED, you could use PWM output to do that and hand the problem over to the hardware.

When you need your Arduino to do several things concurrently, you need to design your sketch to work asynchronously so that it does not ever block (stop) and wait for something to happen. The 'blink without delay' sketch shows you how to achieve this for a simple blink function. The non-blocking approach it demonstrates (check whether something needs to be done yet, then do it if necessary) can be extended to cover a large number of actions that can all be performed concurrently. For example: blink an LED on and off at a timed interval; process data received over a serial connection; handle button presses; read from sensors. You can do all these things concurrently and largely independently, as long as none of them block and wait for something to happen.

It's easy to roll your own. For implementation ideas you might check out the Run library, a tiny library that calls functions on a schedule you specify:


Arduino software does set timers running, is that what you mean by RTOS? Otherwise you have to set those up and start them yourself to read millis() or micros().

Your function 1 and 2 combined with a few sensors to watch as well should not tax an UNO. You can run through loop() checking first time to make the led blink --- visibly ---, 100+ms cycles and check/process serial faster than a millisecond. You have 16,000 cycles to do it which is more than enough, just not if you use 10-bit A/D.

Don't use delay and you won't hang your multi-function up. Every time through loop() it checks time and then checks serial and handles whichever is ready, the great majority of loop passes will be with both checks negative in maybe less than 2 microseconds. That's the response. That's why I say "with 7 more pairs to watch". Could run multiple timers too.