Scale-N

Scale-N

Arduino

Componenten

Rollend Materieel

Naslag

Onderdelen

Wissels

Basis Electronica

Symbols Electronica

Programming Arduino

DCC++

DR5000

Products

Link

Stats













































millis()
The millis function returns the number of milliseconds that your Arduino board has been powered up.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println( millis() );
}


Blinking led, Nodemcu Esp8266 with millis().


#define LED D1 

const unsigned long eventInterval = 500;
unsigned long previousTime = 0;
bool isOn = false;

void setup() {
  pinMode(LED, OUTPUT); 
  digitalWrite(LED, LOW);
}

void loop() {
    unsigned long currentTime = millis();
  
    if (currentTime - previousTime >= eventInterval) {
      digitalWrite(LED, isOn=!isOn);
      previousTime = currentTime;
    }
}


Live

Delay vs Millis (function)
The first difference you can see is that millis() has no parameter but returns the amount of time that has passed; while the delay() will require the number of milliseconds we want to pause the program but will not return anything.
Even though both functions will work with milliseconds and could be used to control time, unlike millis(), the delay() is a blocking function. A blocking function is a function that prevents a program from doing anything else until that task has been completed. This means that by using delay() you cannot execute any other tasks during the time that you have specified.