Arduino - delay () function


The way the delay() function works is pretty simple. It accepts a single integer (or number) argument. This number represents the time (measured in milliseconds). The program should wait until moving on to the next line of code when it encounters this function. However, the problem is, the delay() function is not a good way to make your program wait, because it is known as a “blocking” function.

delay() function Syntax

delay (ms) ;

where, ms is the time in milliseconds to pause (unsigned long).


/* Flashing LED
   * ------------
   * Turns on and off a light emitting diode(LED) connected to a digital
   * pin, in intervals of 2 seconds. *

int ledPin = 13; // LED connected to digital pin 13

void setup() {
   pinMode(ledPin, OUTPUT); // sets the digital pin as output

void loop() {
   digitalWrite(ledPin, HIGH); // sets the LED on
   delay(1000); // waits for a second
   digitalWrite(ledPin, LOW); // sets the LED off
   delay(1000); // waits for a second