Skip to main content

Arduino millis() vs delay() - what is the difference?

Arduino millis() vs delay()

Arduino is a hardware & software based platform, which gives a great opportunity to the hobbyist, students to learn and create interesting projects.

However, by being expert with Arduino you can make advance projects as well.

 

In this article I am going to discuss about delay and millis() functions.

When we can use these functions to generate delay.

 

What is the millis() & delay() function in Arduino?

This function gives the time value in milliseconds since board was started.

Advantage of using this function is program does not stop during delay.

Other task continue to take place.

While if you use ‘delay’ function, then program is paused for the time set in delay function.

For example. If I use delay (5000)

Then program will be paused for 5000 milliseconds or 5 seconds.

To save the value of millis(), you should use variables of ‘unsigned long’ type because the value is in milliseconds and it increases very fast.

For example.

unsigned long current_time = millis();


While, to use the ‘delay()’ function, you just need to put a value as argument in delay() function parenthesis.

For example

delay(1000);

It creates delay of 1000 ms or 1 second.

You can use delay() function for delay when, the delay is very little or only single task is being performed, so that other task cannot be impacted due to this delay or pause.

Comments

Popular posts from this blog

How to Make Automatic Room Light Controller Without Microcontroller

You must have noticed in some offices or hotels, when nobody is in gallery or washroom, the light remains OFF but when somebody enters the place, light switches ON automatically. In this post I am going to teach you how to make this circuit. Before going ahead I would like to tell you that this is VERY EASY circuit. For this circuit the material we need is… PIR Motion sensor General Purpose PCB - 5x5 cm. Transistor 2222N – 1 No. Relay 5V – 1 No. 1K/0.250W – 2 Nos. 10K/0.250W – 1 No. IN4007 – 2 Nos. LED 3mm – 1 No. Connector – 4 Nos. Few wires. Relay Circuit Concept : We can use any relay of 12V, 24V, 5V etc. but we have to consider power supply or battery we will use. Since 5V power supply is easily available and 9V battery can also be used for 5V output (after using 7805 regulator if needed). So I am using 5V relay. PIR sensor has three terminals, One for 5Vdc Second for Gnd (0V). Third for ...

AND Logic Gate by Transistors

Creating an AND gate using transistors is a fundamental exercise in digital electronics. In this post, I will guide you through the process of building an AND gate with transistors, explaining each step in detail.   What is an AND Gate? An AND gate is a basic digital logic gate that outputs TRUE or HIGH (1) only when all its inputs are true or high. If any of the inputs are false or low (0), the output is false or low (0). The truth table for a two-input AND gate is as follows: Input A Input B Output 0 0 0 0 1 0 1 0 0 1 1 1   Components Needed To build an AND gate, you will need the following components: NPN Transistors: BC547 (2 Nos.); Q1, Q2. Resistors : 10K (2 Nos) - R1, R2. Resistors : 1K (1 No) - R3. LED: 5mm Red (1 No) – L1, for output indication. Switches: ...

Difference between Bootloader & Firmware Update

Updating the bootloader and updating the firmware in a microcontroller are two distinct processes, each serving different purposes and involving different steps. Here's a breakdown of the differences: Bootloader Update Purpose : The bootloader is a small program that runs before the main application. It is responsible for initializing the microcontroller and loading the main application firmware. Updating the bootloader might be necessary to add new features, fix bugs, or improve the boot process.   Process : 1.       Enter Bootloader Mode : To update the bootloader, the microcontroller must be put into a special mode. This often involves setting specific pins, pressing buttons, or using a specific command through an existing firmware interface. 2.      Communication Interface : The bootloader update typically uses a specific communication interface such as UART, SPI, I2C, or USB. 3.   ...